From: Lennart Poettering Date: Wed, 10 Jan 2018 16:11:19 +0000 (+0100) Subject: cocci: use strempty() at more places X-Git-Tag: v237~126^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ad5d4b1703da43acfa69503c7101cb754066914c;p=platform%2Fupstream%2Fsystemd.git cocci: use strempty() at more places This shortens the code by a few lines. --- diff --git a/coccinelle/strempty.cocci b/coccinelle/strempty.cocci index e3bd0a1..13ceb33 100644 --- a/coccinelle/strempty.cocci +++ b/coccinelle/strempty.cocci @@ -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); diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 430851c..26d6174 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -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) diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 09a737a..d8f97e6 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -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) { diff --git a/src/core/job.c b/src/core/job.c index b046698..c6de8d2 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -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" diff --git a/src/core/kill.c b/src/core/kill.c index f438c4d..5dfcb78 100644 --- a/src/core/kill.c +++ b/src/core/kill.c @@ -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" diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c index a78aa07..b322127 100644 --- a/src/journal/journal-send.c +++ b/src/journal/journal-send.c @@ -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); diff --git a/src/shared/condition.c b/src/shared/condition.c index 7a825c3..a2fd05c 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -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", diff --git a/src/udev/udev-builtin-input_id.c b/src/udev/udev-builtin-input_id.c index b13fdb1..fa83021 100644 --- a/src/udev/udev-builtin-input_id.c +++ b/src/udev/udev-builtin-input_id.c @@ -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);