X Tutup
Skip to content

Commit f08231f

Browse files
committed
basic/env-util: add strv_env_assign() helper
1 parent 6f8f868 commit f08231f

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

src/basic/env-util.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,30 @@ int strv_env_replace_strdup(char ***l, const char *assignment) {
406406
r = strv_env_replace(l, p);
407407
if (r < 0)
408408
return r;
409+
TAKE_PTR(p);
410+
return r;
411+
}
412+
413+
int strv_env_assign(char ***l, const char *key, const char *value) {
414+
int r;
409415

416+
if (!env_name_is_valid(key))
417+
return -EINVAL;
418+
419+
/* NULL removes assignment, "" creates an empty assignment. */
420+
421+
if (!value) {
422+
strv_env_unset(*l, key);
423+
return 0;
424+
}
425+
426+
char *p = strjoin(key, "=", value);
427+
if (!p)
428+
return -ENOMEM;
429+
430+
r = strv_env_replace(l, p);
431+
if (r < 0)
432+
return r;
410433
TAKE_PTR(p);
411434
return r;
412435
}

src/basic/env-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ char **strv_env_unset(char **l, const char *p); /* In place ... */
4747
char **strv_env_unset_many(char **l, ...) _sentinel_;
4848
int strv_env_replace(char ***l, char *p); /* In place ... */
4949
int strv_env_replace_strdup(char ***l, const char *assignment);
50+
int strv_env_assign(char ***l, const char *key, const char *value);
5051

5152
char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) _pure_;
5253
char *strv_env_get(char **x, const char *n) _pure_;

src/hostname/hostnamed.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,25 +423,11 @@ static int context_write_data_machine_info(Context *c) {
423423
return r;
424424

425425
for (int p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) {
426-
_cleanup_free_ char *t = NULL;
427-
char **u;
428-
429426
assert(name[p]);
430427

431-
if (isempty(c->data[p])) {
432-
strv_env_unset(l, name[p]);
433-
continue;
434-
}
435-
436-
t = strjoin(name[p], "=", c->data[p]);
437-
if (!t)
438-
return -ENOMEM;
439-
440-
u = strv_env_set(l, t);
441-
if (!u)
442-
return -ENOMEM;
443-
444-
strv_free_and_replace(l, u);
428+
r = strv_env_assign(&l, name[p], empty_to_null(c->data[p]));
429+
if (r < 0)
430+
return r;
445431
}
446432

447433
if (strv_isempty(l)) {

src/locale/keymap-util.c

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,12 @@ int locale_write_data(Context *c, char ***settings) {
292292

293293
/* Set values will be returned as strv in *settings on success. */
294294

295-
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
296-
_cleanup_free_ char *t = NULL;
297-
char **u;
298-
const char *name;
299-
300-
name = locale_variable_to_string(p);
301-
assert(name);
302-
303-
if (isempty(c->locale[p]))
304-
continue;
305-
306-
if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
307-
return -ENOMEM;
308-
309-
u = strv_env_set(l, t);
310-
if (!u)
311-
return -ENOMEM;
312-
313-
strv_free_and_replace(l, u);
314-
}
295+
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
296+
if (!isempty(c->locale[p])) {
297+
r = strv_env_assign(&l, locale_variable_to_string(p), c->locale[p]);
298+
if (r < 0)
299+
return r;
300+
}
315301

316302
if (strv_isempty(l)) {
317303
if (unlink("/etc/locale.conf") < 0)
@@ -342,39 +328,13 @@ int vconsole_write_data(Context *c) {
342328
if (r < 0 && r != -ENOENT)
343329
return r;
344330

345-
if (isempty(c->vc_keymap))
346-
l = strv_env_unset(l, "KEYMAP");
347-
else {
348-
_cleanup_free_ char *s = NULL;
349-
char **u;
350-
351-
s = strjoin("KEYMAP=", c->vc_keymap);
352-
if (!s)
353-
return -ENOMEM;
354-
355-
u = strv_env_set(l, s);
356-
if (!u)
357-
return -ENOMEM;
358-
359-
strv_free_and_replace(l, u);
360-
}
361-
362-
if (isempty(c->vc_keymap_toggle))
363-
l = strv_env_unset(l, "KEYMAP_TOGGLE");
364-
else {
365-
_cleanup_free_ char *s = NULL;
366-
char **u;
367-
368-
s = strjoin("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
369-
if (!s)
370-
return -ENOMEM;
371-
372-
u = strv_env_set(l, s);
373-
if (!u)
374-
return -ENOMEM;
331+
r = strv_env_assign(&l, "KEYMAP", empty_to_null(c->vc_keymap));
332+
if (r < 0)
333+
return r;
375334

376-
strv_free_and_replace(l, u);
377-
}
335+
r = strv_env_assign(&l, "KEYMAP_TOGGLE", empty_to_null(c->vc_keymap_toggle));
336+
if (r < 0)
337+
return r;
378338

379339
if (strv_isempty(l)) {
380340
if (unlink("/etc/vconsole.conf") < 0)

0 commit comments

Comments
 (0)
X Tutup