X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=strv.c;h=a749096f9a35c193ea2d16c36091aac5a26ce33f;hp=ed5755a0d19dd797ca3a4ff7a627b33e16a9b345;hb=64c1b5bf9c5777261bdf4058b6cd1cb5de193d5b;hpb=257eca1a9c2d0ef110c21e74952e9723d778e267 diff --git a/strv.c b/strv.c index ed5755a0d..a749096f9 100644 --- a/strv.c +++ b/strv.c @@ -434,3 +434,70 @@ fail: return NULL; } + +static bool env_match(const char *t, const char *pattern) { + assert(t); + assert(pattern); + + /* pattern a matches string a + * a matches a= + * a matches a=b + * a= matches a= + * a=b matches a=b + * a= does not match a + * a=b does not match a= + * a=b does not match a + * a=b does not match a=c */ + + if (streq(t, pattern)) + return true; + + if (!strchr(pattern, '=')) { + size_t l = strlen(pattern); + + return strncmp(t, pattern, l) == 0 && t[l] == '='; + } + + return false; +} + +char **strv_env_delete(char **x, ...) { + size_t n = 0, i = 0; + char **l, **k, **r, **j; + va_list ap; + + /* Deletes every entry fromx that is mentioned in the other + * string lists */ + + n = strv_length(x); + + if (!(r = new(char*, n+1))) + return NULL; + + STRV_FOREACH(k, x) { + va_start(ap, x); + + while ((l = va_arg(ap, char**))) + STRV_FOREACH(j, l) + if (env_match(*k, *j)) + goto delete; + + va_end(ap); + + if (!(r[i++] = strdup(*k))) { + strv_free(r); + return NULL; + } + + continue; + + delete: + va_end(ap); + } + + r[i] = NULL; + + assert(i <= n); + + return r; +}