]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: add new helper call empty_or_dash_to_null()
authorLennart Poettering <lennart@poettering.net>
Mon, 8 Apr 2019 10:11:11 +0000 (12:11 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 8 Apr 2019 10:11:11 +0000 (12:11 +0200)
We have a function like this at two places already. Let's unify it in
one generic location and let's port a number of users over.

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

index b27ad511bd79412d629dc45510125c723666a42b..b23f4c834140978b0e6f953721cf72917ebad5a4 100644 (file)
@@ -63,6 +63,10 @@ static inline bool empty_or_dash(const char *str) {
                 (str[0] == '-' && str[1] == 0);
 }
 
+static inline const char *empty_or_dash_to_null(const char *p) {
+        return empty_or_dash(p) ? NULL : p;
+}
+
 static inline char *startswith(const char *s, const char *prefix) {
         size_t l;
 
index ffd9347e92a1f74e6df7d7dcbf793a5726855cca..77d24b8635c0f3e14e00ec13a7b5c82d5c7a8fe8 100644 (file)
@@ -78,8 +78,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         determine_compression_from_filename(path);
 
@@ -155,8 +154,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         determine_compression_from_filename(path);
 
index 9f3ef0826c0f0258e7b019e144a013a304dbaf71..04344492c86368f2dfb79b68c5983026a26c3cc1 100644 (file)
@@ -117,15 +117,13 @@ static int import_fs(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 if (!machine_name_is_valid(local)) {
index 4c9603fbdcbfc73c20f443c716bb347e56b6ee31..cc2855745962af444b4c0c30150c1b1e47b39cb3 100644 (file)
@@ -49,15 +49,13 @@ static int import_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = tar_strip_suffixes(local, &ll);
@@ -145,15 +143,13 @@ static int import_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2)
                 path = argv[1];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         if (argc >= 3)
                 local = argv[2];
         else if (path)
                 local = basename(path);
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = raw_strip_suffixes(local, &ll);
index 6342f7d7f1f918ad887be68294c7fb9e2844eea7..7e8712493fb5412592695a0e2f4bd412172e66be 100644 (file)
@@ -64,8 +64,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = tar_strip_suffixes(local, &ll);
@@ -151,8 +150,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = raw_strip_suffixes(local, &ll);
index 18affb79ec52e9364e4103e9bec17a15ba8fc43e..b8bd181c16a85d640a21b1d2505a3b8b0f51e6b1 100644 (file)
@@ -32,10 +32,6 @@ static bool startswith_comma(const char *s, const char *prefix) {
         return IN_SET(*s, ',', '\0');
 }
 
-static const char* strnulldash(const char *s) {
-        return empty_or_dash(s) ? NULL : s;
-}
-
 static const char* systemd_kbd_model_map(void) {
         const char* s;
 
@@ -551,15 +547,15 @@ int vconsole_convert_to_x11(Context *c) {
                         if (!streq(c->vc_keymap, a[0]))
                                 continue;
 
-                        if (!streq_ptr(c->x11_layout, strnulldash(a[1])) ||
-                            !streq_ptr(c->x11_model, strnulldash(a[2])) ||
-                            !streq_ptr(c->x11_variant, strnulldash(a[3])) ||
-                            !streq_ptr(c->x11_options, strnulldash(a[4]))) {
+                        if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
+                            !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
+                            !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
+                            !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
 
-                                if (free_and_strdup(&c->x11_layout, strnulldash(a[1])) < 0 ||
-                                    free_and_strdup(&c->x11_model, strnulldash(a[2])) < 0 ||
-                                    free_and_strdup(&c->x11_variant, strnulldash(a[3])) < 0 ||
-                                    free_and_strdup(&c->x11_options, strnulldash(a[4])) < 0)
+                                if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
+                                    free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
+                                    free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
+                                    free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
                                         return -ENOMEM;
 
                                 modified = true;
index d6e9c8c03c9cb86d5619580e660354d9eb3a080b..d98027a0ca5fdd63246f8498b960d1373743910d 100644 (file)
@@ -1993,10 +1993,6 @@ static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
         return -r;
 }
 
-static const char *nullify_dash(const char *p) {
-        return empty_or_dash(p) ? NULL : p;
-}
-
 static int import_tar(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
         _cleanup_free_ char *ll = NULL, *fn = NULL;
@@ -2008,10 +2004,10 @@ static int import_tar(int argc, char *argv[], void *userdata) {
         assert(bus);
 
         if (argc >= 2)
-                path = nullify_dash(argv[1]);
+                path = empty_or_dash_to_null(argv[1]);
 
         if (argc >= 3)
-                local = nullify_dash(argv[2]);
+                local = empty_or_dash_to_null(argv[2]);
         else if (path) {
                 r = path_extract_filename(path, &fn);
                 if (r < 0)
@@ -2075,10 +2071,10 @@ static int import_raw(int argc, char *argv[], void *userdata) {
         assert(bus);
 
         if (argc >= 2)
-                path = nullify_dash(argv[1]);
+                path = empty_or_dash_to_null(argv[1]);
 
         if (argc >= 3)
-                local = nullify_dash(argv[2]);
+                local = empty_or_dash_to_null(argv[2]);
         else if (path) {
                 r = path_extract_filename(path, &fn);
                 if (r < 0)
@@ -2142,10 +2138,10 @@ static int import_fs(int argc, char *argv[], void *userdata) {
         assert(bus);
 
         if (argc >= 2)
-                path = nullify_dash(argv[1]);
+                path = empty_or_dash_to_null(argv[1]);
 
         if (argc >= 3)
-                local = nullify_dash(argv[2]);
+                local = empty_or_dash_to_null(argv[2]);
         else if (path) {
                 r = path_extract_filename(path, &fn);
                 if (r < 0)
@@ -2224,8 +2220,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         if (path) {
                 determine_compression_from_filename(path);
@@ -2274,8 +2269,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
 
         if (argc >= 3)
                 path = argv[2];
-        if (empty_or_dash(path))
-                path = NULL;
+        path = empty_or_dash_to_null(path);
 
         if (path) {
                 determine_compression_from_filename(path);
@@ -2332,8 +2326,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = tar_strip_suffixes(local, &ll);
@@ -2396,8 +2389,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
                 local = l;
         }
 
-        if (empty_or_dash(local))
-                local = NULL;
+        local = empty_or_dash_to_null(local);
 
         if (local) {
                 r = raw_strip_suffixes(local, &ll);