]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cocci: use strempty() at more places
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Jan 2018 16:11:19 +0000 (17:11 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 10 Jan 2018 16:11:19 +0000 (17:11 +0100)
This shortens the code by a few lines.

coccinelle/strempty.cocci
src/basic/fileio.c
src/basic/string-util.h
src/core/job.c
src/core/kill.c
src/journal/journal-send.c
src/shared/condition.c
src/udev/udev-builtin-input_id.c

index e3bd0a1f560e309205387fbe4298daa505861c9f..13ceb338f199dad900efa29f5f94d97db762a5e3 100644 (file)
@@ -8,3 +8,41 @@ expression s;
 @@
 - s ? s : ""
 + strempty(s)
+@@
+expression s;
+@@
+- if (!s)
+-         s = "";
++ s = strempty(s);
+@@
+expression s;
+@@
+- s ?: "(null)"
++ strnull(s)
+@@
+expression s;
+@@
+- s ? s : "(null)"
++ strnull(s)
+@@
+expression s;
+@@
+- if (!s)
+-         s = "(null)";
++ s = strnull(s);
+@@
+expression s;
+@@
+- s ?: "n/a"
++ strna(s)
+@@
+expression s;
+@@
+- s ? s : "n/a"
++ strna(s)
+@@
+expression s;
+@@
+- if (!s)
+-         s = "n/a";
++ s = strna(s);
index 430851cc8caaa1307dcdb62f20eda76b07b0bccb..26d6174664e6461b55c43d9a5ce46e5d6db2e52d 100644 (file)
@@ -1225,8 +1225,7 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
         if (!filename_is_valid(fn))
                 return -EINVAL;
 
-        if (!extra)
-                extra = "";
+        extra = strempty(extra);
 
         t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
         if (!t)
@@ -1259,8 +1258,7 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
         if (!filename_is_valid(fn))
                 return -EINVAL;
 
-        if (!extra)
-                extra = "";
+        extra = strempty(extra);
 
         t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
         if (!t)
@@ -1300,8 +1298,7 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
                         return r;
         }
 
-        if (!extra)
-                extra = "";
+        extra = strempty(extra);
 
         t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
         if (!t)
index 09a737ad3763a0618bd5b9b704237323f4f0bc99..d8f97e697bbf1ce7b50640bd2108d75829d4ccbb 100644 (file)
@@ -52,15 +52,15 @@ static inline bool streq_ptr(const char *a, const char *b) {
 }
 
 static inline const char* strempty(const char *s) {
-        return s ? s : "";
+        return s ?: "";
 }
 
 static inline const char* strnull(const char *s) {
-        return s ? s : "(null)";
+        return s ?: "(null)";
 }
 
 static inline const char *strna(const char *s) {
-        return s ? s : "n/a";
+        return s ?: "n/a";
 }
 
 static inline bool isempty(const char *p) {
index b046698118bdc368a3a9f735cbf9afd7abfba43e..c6de8d27e432a649e6541daaadac5b024dc03355 100644 (file)
@@ -306,8 +306,7 @@ void job_dump(Job *j, FILE*f, const char *prefix) {
         assert(j);
         assert(f);
 
-        if (!prefix)
-                prefix = "";
+        prefix = strempty(prefix);
 
         fprintf(f,
                 "%s-> Job %u:\n"
index f438c4d8fa53d6dd085d7a51b8e98b7a79efe4d9..5dfcb780fa38686d9f8532c436249913d6d06cf6 100644 (file)
@@ -34,8 +34,7 @@ void kill_context_init(KillContext *c) {
 void kill_context_dump(KillContext *c, FILE *f, const char *prefix) {
         assert(c);
 
-        if (!prefix)
-                prefix = "";
+        prefix = strempty(prefix);
 
         fprintf(f,
                 "%sKillMode: %s\n"
index a78aa07032fc34a23beaa7c3dec01a5e8b69d079..b322127a4e3359e0e95055354ec4c07f4d234737 100644 (file)
@@ -418,8 +418,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
 
         fd_inc_sndbuf(fd, SNDBUF_SIZE);
 
-        if (!identifier)
-                identifier = "";
+        identifier = strempty(identifier);
 
         l = strlen(identifier);
         header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
index 7a825c397321962270b2a7023bba2f17e5ea6f45..a2fd05c4256fd0d0e1a47251b2fc2cd4890ba92b 100644 (file)
@@ -653,8 +653,7 @@ void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_
         assert(c);
         assert(f);
 
-        if (!prefix)
-                prefix = "";
+        prefix = strempty(prefix);
 
         fprintf(f,
                 "%s\t%s: %s%s%s %s\n",
index b13fdb135beeb68658590a58cf320d2a361ef0c6..fa830213ffe14fac7948574ffe912e1aff71dca9 100644 (file)
@@ -102,8 +102,7 @@ static void get_cap_mask(struct udev_device *dev,
         unsigned long val;
 
         v = udev_device_get_sysattr_value(pdev, attr);
-        if (!v)
-                v = "";
+        v = strempty(v);
 
         xsprintf(text, "%s", v);
         log_debug("%s raw kernel attribute: %s", attr, text);