basic/env-util: add strv_env_assign() helper

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-02-16 16:10:14 +01:00
parent 6f8f8688e1
commit f08231fe07
4 changed files with 39 additions and 69 deletions
+23
View 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;
}
+1
View 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_;
+3 -17
View 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)) {
+12 -52
View 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;
r = strv_env_assign(&l, "KEYMAP", empty_to_null(c->vc_keymap));
if (r < 0)
return r;
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;
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)