chiark / gitweb /
localed: remove free_and_copy
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Sep 2014 02:53:23 +0000 (22:53 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Sep 2014 02:53:23 +0000 (22:53 -0400)
It was mostly a duplicate of free_and_strdup().

src/locale/localed.c
src/shared/util.c
src/shared/util.h

index c9f7105bb3713350e34153090be88a03a6f4dc54..ac8477ad39575301020a6a71eef58ae99bfaf43d 100644 (file)
@@ -92,20 +92,8 @@ typedef struct Context {
         Hashmap *polkit_registry;
 } Context;
 
-static int free_and_copy(char **s, const char *v) {
-        int r;
-        char *t;
-
-        assert(s);
-
-        r = strdup_or_null(isempty(v) ? NULL : v, &t);
-        if (r < 0)
-                return r;
-
-        free(*s);
-        *s = t;
-
-        return 0;
+static const char* nonempty(const char *s) {
+        return isempty(s) ? NULL : s;
 }
 
 static void free_and_replace(char **s, char *v) {
@@ -144,10 +132,8 @@ static void locale_simplify(Context *c) {
         int p;
 
         for (p = LOCALE_LANG+1; p < _LOCALE_MAX; p++)
-                if (isempty(c->locale[p]) || streq_ptr(c->locale[LOCALE_LANG], c->locale[p])) {
-                        free(c->locale[p]);
-                        c->locale[p] = NULL;
-                }
+                if (isempty(c->locale[p]) || streq_ptr(c->locale[LOCALE_LANG], c->locale[p]))
+                        free_and_replace(&c->locale[p], NULL);
 }
 
 static int locale_read_data(Context *c) {
@@ -179,7 +165,8 @@ static int locale_read_data(Context *c) {
                 for (p = 0; p < _LOCALE_MAX; p++) {
                         assert(names[p]);
 
-                        r = free_and_copy(&c->locale[p], getenv(names[p]));
+                        r = free_and_strdup(&c->locale[p],
+                                            nonempty(getenv(names[p])));
                         if (r < 0)
                                 return r;
                 }
@@ -503,8 +490,8 @@ static int vconsole_reload(sd_bus *bus) {
         return r;
 }
 
-static char *strnulldash(const char *s) {
-        return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s;
+static const char* strnulldash(const char *s) {
+        return isempty(s) || streq(s, "-") ? NULL : s;
 }
 
 static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
@@ -588,10 +575,10 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
                             !streq_ptr(c->x11_variant, strnulldash(a[3])) ||
                             !streq_ptr(c->x11_options, strnulldash(a[4]))) {
 
-                                if (free_and_copy(&c->x11_layout, strnulldash(a[1])) < 0 ||
-                                    free_and_copy(&c->x11_model, strnulldash(a[2])) < 0 ||
-                                    free_and_copy(&c->x11_variant, strnulldash(a[3])) < 0 ||
-                                    free_and_copy(&c->x11_options, strnulldash(a[4])) < 0)
+                                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)
                                         return -ENOMEM;
 
                                 modified = true;
@@ -713,10 +700,9 @@ static int find_legacy_keymap(Context *c, char **new_keymap) {
                 if (matching > best_matching) {
                         best_matching = matching;
 
-                        free(*new_keymap);
-                        *new_keymap = strdup(a[0]);
-                        if (!*new_keymap)
-                                return -ENOMEM;
+                        r = free_and_strdup(new_keymap, a[0]);
+                        if (r < 0)
+                                return r;
                 }
         }
 
@@ -868,14 +854,9 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_
 
                                 k = strlen(names[p]);
                                 if (startswith(*i, names[p]) && (*i)[k] == '=') {
-                                        char *t;
-
-                                        t = strdup(*i + k + 1);
-                                        if (!t)
-                                                return -ENOMEM;
-
-                                        free(c->locale[p]);
-                                        c->locale[p] = t;
+                                        r = free_and_strdup(&c->locale[p], *i + k + 1);
+                                        if (r < 0)
+                                                return r;
                                         break;
                                 }
                         }
@@ -937,8 +918,8 @@ static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata
                 if (r == 0)
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
-                if (free_and_copy(&c->vc_keymap, keymap) < 0 ||
-                    free_and_copy(&c->vc_keymap_toggle, keymap_toggle) < 0)
+                if (free_and_strdup(&c->vc_keymap, keymap) < 0 ||
+                    free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0)
                         return -ENOMEM;
 
                 r = vconsole_write_data(c);
@@ -1007,10 +988,10 @@ static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdat
                 if (r == 0)
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
-                if (free_and_copy(&c->x11_layout, layout) < 0 ||
-                    free_and_copy(&c->x11_model, model) < 0 ||
-                    free_and_copy(&c->x11_variant, variant) < 0 ||
-                    free_and_copy(&c->x11_options, options) < 0)
+                if (free_and_strdup(&c->x11_layout, layout) < 0 ||
+                    free_and_strdup(&c->x11_model, model) < 0 ||
+                    free_and_strdup(&c->x11_variant, variant) < 0 ||
+                    free_and_strdup(&c->x11_options, options) < 0)
                         return -ENOMEM;
 
                 r = write_data_x11(c);
index cf9d487b829b67087691a43da7959d0637020a10..502b3675b1d543fec6b082b46f317b4ffeb5f58e 100644 (file)
@@ -4981,24 +4981,6 @@ bool kexec_loaded(void) {
        return loaded;
 }
 
-int strdup_or_null(const char *a, char **b) {
-        char *c;
-
-        assert(b);
-
-        if (!a) {
-                *b = NULL;
-                return 0;
-        }
-
-        c = strdup(a);
-        if (!c)
-                return -ENOMEM;
-
-        *b = c;
-        return 0;
-}
-
 int prot_from_flags(int flags) {
 
         switch (flags & O_ACCMODE) {
index 3401280d0952a4481e8a7e0ff37ffa68a92c9a26..08d556fc924a47aabe4c86d0554c49f3f39a40b0 100644 (file)
@@ -583,8 +583,6 @@ int block_get_whole_disk(dev_t d, dev_t *ret);
 
 int file_is_priv_sticky(const char *p);
 
-int strdup_or_null(const char *a, char **b);
-
 #define NULSTR_FOREACH(i, l)                                    \
         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)