]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/env-util: add strv_env_assign() helper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 15 Feb 2021 13:52:39 +0000 (14:52 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 16 Feb 2021 15:10:14 +0000 (16:10 +0100)
src/basic/env-util.c
src/basic/env-util.h
src/hostname/hostnamed.c
src/locale/keymap-util.c

index 5335c6784dbcbae20839c4b325969c150f89fd17..5f2b29d7b5b351350bdc4f998a498fbd6e425ebd 100644 (file)
@@ -406,7 +406,30 @@ int strv_env_replace_strdup(char ***l, const char *assignment) {
         r = strv_env_replace(l, p);
         if (r < 0)
                 return r;
+        TAKE_PTR(p);
+        return r;
+}
+
+int strv_env_assign(char ***l, const char *key, const char *value) {
+        int r;
 
+        if (!env_name_is_valid(key))
+                return -EINVAL;
+
+        /* NULL removes assignment, "" creates an empty assignment. */
+
+        if (!value) {
+                strv_env_unset(*l, key);
+                return 0;
+        }
+
+        char *p = strjoin(key, "=", value);
+        if (!p)
+                return -ENOMEM;
+
+        r = strv_env_replace(l, p);
+        if (r < 0)
+                return r;
         TAKE_PTR(p);
         return r;
 }
index 922cfe1c44a7e585d3d0e832b95bac8a3b882005..3f8995bf3db8b99adc2bdf6ceb176b0f78099f10 100644 (file)
@@ -47,6 +47,7 @@ char **strv_env_unset(char **l, const char *p); /* In place ... */
 char **strv_env_unset_many(char **l, ...) _sentinel_;
 int strv_env_replace(char ***l, char *p); /* In place ... */
 int strv_env_replace_strdup(char ***l, const char *assignment);
+int strv_env_assign(char ***l, const char *key, const char *value);
 
 char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) _pure_;
 char *strv_env_get(char **x, const char *n) _pure_;
index 3b2350ed0d4a739d602458031d4bed087bcce57b..34333a65307f193dc66eb9d99610022c5e457ec3 100644 (file)
@@ -423,25 +423,11 @@ static int context_write_data_machine_info(Context *c) {
                 return r;
 
         for (int p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) {
-                _cleanup_free_ char *t = NULL;
-                char **u;
-
                 assert(name[p]);
 
-                if (isempty(c->data[p]))  {
-                        strv_env_unset(l, name[p]);
-                        continue;
-                }
-
-                t = strjoin(name[p], "=", c->data[p]);
-                if (!t)
-                        return -ENOMEM;
-
-                u = strv_env_set(l, t);
-                if (!u)
-                        return -ENOMEM;
-
-                strv_free_and_replace(l, u);
+                r = strv_env_assign(&l, name[p], empty_to_null(c->data[p]));
+                if (r < 0)
+                        return r;
         }
 
         if (strv_isempty(l)) {
index d0d2d30a346cf5156851429ac6860ce66c225449..e8de1b789a0e3f90a864a1786bc9249089bb659e 100644 (file)
@@ -292,26 +292,12 @@ int locale_write_data(Context *c, char ***settings) {
 
         /* Set values will be returned as strv in *settings on success. */
 
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
-                _cleanup_free_ char *t = NULL;
-                char **u;
-                const char *name;
-
-                name = locale_variable_to_string(p);
-                assert(name);
-
-                if (isempty(c->locale[p]))
-                        continue;
-
-                if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
-                        return -ENOMEM;
-
-                u = strv_env_set(l, t);
-                if (!u)
-                        return -ENOMEM;
-
-                strv_free_and_replace(l, u);
-        }
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
+                if (!isempty(c->locale[p])) {
+                        r = strv_env_assign(&l, locale_variable_to_string(p), c->locale[p]);
+                        if (r < 0)
+                                return r;
+                }
 
         if (strv_isempty(l)) {
                 if (unlink("/etc/locale.conf") < 0)
@@ -342,39 +328,13 @@ int vconsole_write_data(Context *c) {
         if (r < 0 && r != -ENOENT)
                 return r;
 
-        if (isempty(c->vc_keymap))
-                l = strv_env_unset(l, "KEYMAP");
-        else {
-                _cleanup_free_ char *s = NULL;
-                char **u;
-
-                s = strjoin("KEYMAP=", c->vc_keymap);
-                if (!s)
-                        return -ENOMEM;
-
-                u = strv_env_set(l, s);
-                if (!u)
-                        return -ENOMEM;
-
-                strv_free_and_replace(l, u);
-        }
-
-        if (isempty(c->vc_keymap_toggle))
-                l = strv_env_unset(l, "KEYMAP_TOGGLE");
-        else  {
-                _cleanup_free_ char *s = NULL;
-                char **u;
-
-                s = strjoin("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
-                if (!s)
-                        return -ENOMEM;
-
-                u = strv_env_set(l, s);
-                if (!u)
-                        return -ENOMEM;
+        r = strv_env_assign(&l, "KEYMAP", empty_to_null(c->vc_keymap));
+        if (r < 0)
+                return r;
 
-                strv_free_and_replace(l, u);
-        }
+        r = strv_env_assign(&l, "KEYMAP_TOGGLE", empty_to_null(c->vc_keymap_toggle));
+        if (r < 0)
+                return r;
 
         if (strv_isempty(l)) {
                 if (unlink("/etc/vconsole.conf") < 0)