int vconsole_write_data(Context *c) { _cleanup_strv_free_ char **l = NULL; struct stat st; int r; r = load_env_file(NULL, "/etc/vconsole.conf", NULL, &l); 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 = strappend("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 = strappend("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); } if (strv_isempty(l)) { if (unlink("/etc/vconsole.conf") < 0) return errno == ENOENT ? 0 : -errno; c->vc_mtime = USEC_INFINITY; return 0; } r = write_env_file_label("/etc/vconsole.conf", l); if (r < 0) return r; if (stat("/etc/vconsole.conf", &st) >= 0) c->vc_mtime = timespec_load(&st.st_mtim); return 0; }
static int vconsole_write_data(Context *c) { int r; _cleanup_strv_free_ char **l = NULL; r = load_env_file("/etc/vconsole.conf", NULL, &l); if (r < 0 && r != -ENOENT) return r; if (isempty(c->vc_keymap)) l = strv_env_unset(l, "KEYMAP"); else { char *s, **u; s = strappend("KEYMAP=", c->vc_keymap); if (!s) return -ENOMEM; u = strv_env_set(l, s); free(s); strv_free(l); if (!u) return -ENOMEM; l = u; } if (isempty(c->vc_keymap_toggle)) l = strv_env_unset(l, "KEYMAP_TOGGLE"); else { char *s, **u; s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle); if (!s) return -ENOMEM; u = strv_env_set(l, s); free(s); strv_free(l); if (!u) return -ENOMEM; l = u; } if (strv_isempty(l)) { if (unlink("/etc/vconsole.conf") < 0) return errno == ENOENT ? 0 : -errno; return 0; } r = write_env_file_label("/etc/vconsole.conf", l); return r; }
static int context_write_data_machine_info(Context *c) { static const char * const name[_PROP_MAX] = { [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", [PROP_ICON_NAME] = "ICON_NAME", [PROP_CHASSIS] = "CHASSIS", [PROP_DEPLOYMENT] = "DEPLOYMENT", [PROP_LOCATION] = "LOCATION", }; _cleanup_strv_free_ char **l = NULL; int r, p; assert(c); r = load_env_file(NULL, "/etc/machine-info", NULL, &l); if (r < 0 && r != -ENOENT) return r; for (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], NULL); if (!t) return -ENOMEM; u = strv_env_set(l, t); if (!u) return -ENOMEM; strv_free(l); l = u; } if (strv_isempty(l)) { if (unlink("/etc/machine-info") < 0) return errno == ENOENT ? 0 : -errno; return 0; } return write_env_file_label("/etc/machine-info", l); }
static void test_strv_env_set(void) { _cleanup_strv_free_ char **l = NULL, **r = NULL; l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL); assert_se(l); r = strv_env_set(l, "WALDO=WALDO"); assert_se(r); assert_se(streq(r[0], "PIEP")); assert_se(streq(r[1], "SCHLUMPF=SMURFF")); assert_se(streq(r[2], "NANANANA=YES")); assert_se(streq(r[3], "WALDO=WALDO")); assert_se(strv_length(r) == 4); }
int locale_write_data(Context *c, char ***settings) { int r, p; _cleanup_strv_free_ char **l = NULL; /* Set values will be returned as strv in *settings on success. */ r = load_env_file(NULL, "/etc/locale.conf", NULL, &l); if (r < 0 && r != -ENOENT) return r; for (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])) { l = strv_env_unset(l, name); 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(l); l = u; } if (strv_isempty(l)) { if (unlink("/etc/locale.conf") < 0) return errno == ENOENT ? 0 : -errno; return 0; } r = write_env_file_label("/etc/locale.conf", l); if (r < 0) return r; *settings = TAKE_PTR(l); return 0; }
int locale_write_data(Context *c, char ***settings) { _cleanup_strv_free_ char **l = NULL; struct stat st; int r, p; /* Set values will be returned as strv in *settings on success. */ for (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); } if (strv_isempty(l)) { if (unlink("/etc/locale.conf") < 0) return errno == ENOENT ? 0 : -errno; c->locale_mtime = USEC_INFINITY; return 0; } r = write_env_file_label("/etc/locale.conf", l); if (r < 0) return r; *settings = TAKE_PTR(l); if (stat("/etc/locale.conf", &st) >= 0) c->locale_mtime = timespec_load(&st.st_mtim); return 0; }
static int locale_write_data(Context *c) { int r, p; char **l = NULL; r = load_env_file("/etc/locale.conf", NULL, &l); if (r < 0 && r != -ENOENT) return r; for (p = 0; p < _LOCALE_MAX; p++) { char *t, **u; assert(names[p]); if (isempty(c->locale[p])) { l = strv_env_unset(l, names[p]); continue; } if (asprintf(&t, "%s=%s", names[p], c->locale[p]) < 0) { strv_free(l); return -ENOMEM; } u = strv_env_set(l, t); free(t); strv_free(l); if (!u) return -ENOMEM; l = u; } if (strv_isempty(l)) { strv_free(l); if (unlink("/etc/locale.conf") < 0) return errno == ENOENT ? 0 : -errno; return 0; } r = write_env_file_label("/etc/locale.conf", l); strv_free(l); return r; }
static int write_data_vconsole(void) { int r; char **l = NULL; r = load_env_file("/etc/vconsole.conf", &l); if (r < 0 && r != -ENOENT) return r; if (isempty(state.vc_keymap)) l = strv_env_unset(l, "KEYMAP"); else { char *s, **u; s = strappend("KEYMAP=", state.vc_keymap); if (!s) { strv_free(l); return -ENOMEM; } u = strv_env_set(l, s); free(s); strv_free(l); if (!u) return -ENOMEM; l = u; } if (isempty(state.vc_keymap_toggle)) l = strv_env_unset(l, "KEYMAP_TOGGLE"); else { char *s, **u; s = strappend("KEYMAP_TOGGLE=", state.vc_keymap_toggle); if (!s) { strv_free(l); return -ENOMEM; } u = strv_env_set(l, s); free(s); strv_free(l); if (!u) return -ENOMEM; l = u; } if (strv_isempty(l)) { strv_free(l); if (unlink("/etc/vconsole.conf") < 0) return errno == ENOENT ? 0 : -errno; return 0; } r = write_env_file("/etc/vconsole.conf", l); strv_free(l); return r; }