X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fstrv.c;h=71b77c9bbf97d857cce44ac0af1c350463d3ff50;hp=c8ebb648d643e9f43667d77ad00c7850e638cefa;hb=9534ce54858c67363b841cdbdc315140437bfdb4;hpb=ede27aab11cda3f55c2cd2e1e28e2385b3f64453 diff --git a/src/strv.c b/src/strv.c index c8ebb648d..71b77c9bb 100644 --- a/src/strv.c +++ b/src/strv.c @@ -358,7 +358,10 @@ char **strv_remove(char **l, const char *s) { if (!l) return NULL; - /* Drops every occurrence of s in the string list */ + assert(s); + + /* Drops every occurrence of s in the string list, edits + * in-place. */ for (f = t = l; *f; f++) { @@ -387,7 +390,12 @@ static int env_append(char **r, char ***k, char **a) { for (; *a; a++) { char **j; - size_t n = strcspn(*a, "=") + 1; + size_t n; + + n = strcspn(*a, "="); + + if ((*a)[n] == '=') + n++; for (j = r; j < *k; j++) if (strncmp(*j, *a, n) == 0) @@ -517,9 +525,38 @@ char **strv_env_delete(char **x, unsigned n_lists, ...) { return r; } +char **strv_env_unset(char **l, const char *p) { + + char **f, **t; + + if (!l) + return NULL; + + assert(p); + + /* Drops every occurrence of the env var setting p in the + * string list. edits in-place. */ + + for (f = t = l; *f; f++) { + + if (env_match(*f, p)) { + free(*f); + continue; + } + + *(t++) = *f; + } + + *t = NULL; + return l; +} + char **strv_env_set(char **x, const char *p) { char **k, **r; + char* m[2] = { (char*) p, NULL }; + + /* Overrides the env var setting of p, returns a new copy */ if (!(r = new(char*, strv_length(x)+2))) return NULL; @@ -528,7 +565,7 @@ char **strv_env_set(char **x, const char *p) { if (env_append(r, &k, x) < 0) goto fail; - if (!(*(k++) = strdup(p))) + if (env_append(r, &k, m) < 0) goto fail; *k = NULL;