]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: introduce empty_or_dash() helper
authorLennart Poettering <lennart@poettering.net>
Mon, 8 Apr 2019 10:03:33 +0000 (12:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 8 Apr 2019 10:03:33 +0000 (12:03 +0200)
At quite a few places we check isempty() || streq(…, "-"), let's add a
helper to simplify that, and replace that by a single function call.

src/basic/string-util.h
src/import/export.c
src/import/import-fs.c
src/import/import.c
src/import/pull.c
src/locale/keymap-util.c
src/machine/machinectl.c
src/sysusers/sysusers.c
src/tmpfiles/tmpfiles.c

index 9cf11198b12d95e276201a2ddc9f8e892b4140fe..b27ad511bd79412d629dc45510125c723666a42b 100644 (file)
@@ -57,6 +57,12 @@ static inline const char *empty_to_dash(const char *str) {
         return isempty(str) ? "-" : str;
 }
 
+static inline bool empty_or_dash(const char *str) {
+        return !str ||
+                str[0] == 0 ||
+                (str[0] == '-' && str[1] == 0);
+}
+
 static inline char *startswith(const char *s, const char *prefix) {
         size_t l;
 
index 4566a66a15453e2003cb225be63a4bff023991ca..ffd9347e92a1f74e6df7d7dcbf793a5726855cca 100644 (file)
@@ -78,7 +78,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         determine_compression_from_filename(path);
@@ -155,7 +155,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         determine_compression_from_filename(path);
index 974c0f5536b5f0e2ca0c5c51b35b000ad1731289..9f3ef0826c0f0258e7b019e144a013a304dbaf71 100644 (file)
@@ -117,14 +117,14 @@ static int import_fs(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
index e3a1ae8a8bc92d14b124e8dab3e05e01eec46257..4c9603fbdcbfc73c20f443c716bb347e56b6ee31 100644 (file)
@@ -49,14 +49,14 @@ static int import_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
@@ -145,14 +145,14 @@ static int import_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
index 68b1221b724b5c3bc0d6de8c4b38a2b3b772a6c0..6342f7d7f1f918ad887be68294c7fb9e2844eea7 100644 (file)
@@ -64,7 +64,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
@@ -151,7 +151,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
index 17540089bafe2c822baf10595cf41ddc2a3d41d3..18affb79ec52e9364e4103e9bec17a15ba8fc43e 100644 (file)
@@ -33,7 +33,7 @@ static bool startswith_comma(const char *s, const char *prefix) {
 }
 
 static const char* strnulldash(const char *s) {
-        return isempty(s) || streq(s, "-") ? NULL : s;
+        return empty_or_dash(s) ? NULL : s;
 }
 
 static const char* systemd_kbd_model_map(void) {
index c8607bcec3eec43567fc089cb06eed2610bd5090..d6e9c8c03c9cb86d5619580e660354d9eb3a080b 100644 (file)
@@ -1994,13 +1994,7 @@ static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
 }
 
 static const char *nullify_dash(const char *p) {
-        if (isempty(p))
-                return NULL;
-
-        if (streq(p, "-"))
-                return NULL;
-
-        return p;
+        return empty_or_dash(p) ? NULL : p;
 }
 
 static int import_tar(int argc, char *argv[], void *userdata) {
@@ -2230,7 +2224,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         if (path) {
@@ -2280,7 +2274,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (isempty(path) || streq(path, "-"))
+        if (empty_or_dash(path))
                 path = NULL;
 
         if (path) {
@@ -2338,7 +2332,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
@@ -2402,7 +2396,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (isempty(local) || streq(local, "-"))
+        if (empty_or_dash(local))
                 local = NULL;
 
         if (local) {
index 9a85a20be33d389ee4bcbe71344ff4b592bef4d5..843c3837d1f45385ccb95a8c15d00c46fc00c180 100644 (file)
@@ -1411,7 +1411,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                                        "[%s:%u] Unknown command type '%c'.", fname, line, action[0]);
 
         /* Verify name */
-        if (isempty(name) || streq(name, "-"))
+        if (empty_or_dash(name))
                 name = mfree(name);
 
         if (name) {
@@ -1426,7 +1426,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         }
 
         /* Verify id */
-        if (isempty(id) || streq(id, "-"))
+        if (empty_or_dash(id))
                 id = mfree(id);
 
         if (id) {
@@ -1437,7 +1437,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         }
 
         /* Verify description */
-        if (isempty(description) || streq(description, "-"))
+        if (empty_or_dash(description))
                 description = mfree(description);
 
         if (description) {
@@ -1453,7 +1453,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         }
 
         /* Verify home */
-        if (isempty(home) || streq(home, "-"))
+        if (empty_or_dash(home))
                 home = mfree(home);
 
         if (home) {
@@ -1469,7 +1469,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         }
 
         /* Verify shell */
-        if (isempty(shell) || streq(shell, "-"))
+        if (empty_or_dash(shell))
                 shell = mfree(shell);
 
         if (shell) {
index 84e104f9b5569a5e8466afa94fb93b51cda3d6dd..d9d1cc1c1a1c6cd3b9da0c3aeadc4ccee680b4c2 100644 (file)
@@ -2507,7 +2507,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                 return -EIO;
         }
 
-        if (!isempty(buffer) && !streq(buffer, "-")) {
+        if (!empty_or_dash(buffer)) {
                 i.argument = strdup(buffer);
                 if (!i.argument)
                         return log_oom();
@@ -2704,7 +2704,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                 free_and_replace(i.path, p);
         }
 
-        if (!isempty(user) && !streq(user, "-")) {
+        if (!empty_or_dash(user)) {
                 const char *u = user;
 
                 r = get_user_creds(&u, &i.uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
@@ -2716,7 +2716,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                 i.uid_set = true;
         }
 
-        if (!isempty(group) && !streq(group, "-")) {
+        if (!empty_or_dash(group)) {
                 const char *g = group;
 
                 r = get_group_creds(&g, &i.gid, USER_CREDS_ALLOW_MISSING);
@@ -2729,7 +2729,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
                 i.gid_set = true;
         }
 
-        if (!isempty(mode) && !streq(mode, "-")) {
+        if (!empty_or_dash(mode)) {
                 const char *mm = mode;
                 unsigned m;
 
@@ -2749,7 +2749,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
         } else
                 i.mode = IN_SET(i.type, CREATE_DIRECTORY, TRUNCATE_DIRECTORY, CREATE_SUBVOLUME, CREATE_SUBVOLUME_INHERIT_QUOTA, CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
 
-        if (!isempty(age) && !streq(age, "-")) {
+        if (!empty_or_dash(age)) {
                 const char *a = age;
 
                 if (*a == '~') {