diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-27 21:15:59 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-11-05 18:54:27 -0400 |
commit | 54ac349445341503b1651c17b21495039d818a82 (patch) | |
tree | 7e4c119befbc46043b383db48e15c5578aad5784 /src/basic/env-util.c | |
parent | 035fe294b3fdc054151eacc5a75909f2c479300c (diff) |
core/load-fragment: modify existing environment instead of copying strv over and over
Diffstat (limited to 'src/basic/env-util.c')
-rw-r--r-- | src/basic/env-util.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c index b74290d6fd..7c69ccdaf9 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -384,6 +384,28 @@ char **strv_env_unset_many(char **l, ...) { return l; } +int strv_env_replace(char ***l, char *p) { + char **f; + + assert(p); + + /* Replace first occurrence of the env var or add a new one in the + * string list. Drop other occurences. Edits in-place. Does not copy p. + */ + + for (f = *l; f && *f; f++) + if (env_match(*f, p)) { + free_and_replace(*f, p); + strv_env_unset(f + 1, p); + return 0; + } + + /* We didn't find a match, we need to append p or create a new strv */ + if (strv_push(l, p) < 0) + return -ENOMEM; + return 1; +} + char **strv_env_set(char **x, const char *p) { char **k, **r; |