env-util: modernize strv_env_replace()

This commit is contained in:
Lennart Poettering 2018-10-17 20:11:52 +02:00
parent 2d3ff1decb
commit 04effffd84

View file

@ -380,22 +380,23 @@ char **strv_env_unset_many(char **l, ...) {
}
int strv_env_replace(char ***l, char *p) {
char **f;
const char *t, *name;
char **f;
int r;
assert(p);
/* Replace first occurrence of the env var or add a new one in the
* string list. Drop other occurrences. Edits in-place. Does not copy p.
* p must be a valid key=value assignment.
/* Replace first occurrence of the env var or add a new one in the string list. Drop other occurrences. Edits
* in-place. Does not copy p. p must be a valid key=value assignment.
*/
t = strchr(p, '=');
assert(t);
if (!t)
return -EINVAL;
name = strndupa(p, t - p);
for (f = *l; f && *f; f++)
STRV_FOREACH(f, *l)
if (env_entry_has_name(*f, name)) {
free_and_replace(*f, p);
strv_env_unset(f + 1, *f);
@ -403,8 +404,10 @@ int strv_env_replace(char ***l, char *p) {
}
/* We didn't find a match, we need to append p or create a new strv */
if (strv_push(l, p) < 0)
return -ENOMEM;
r = strv_push(l, p);
if (r < 0)
return r;
return 1;
}