X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fstrv.c;h=c8ff5745e82024bead8e6f997f1d1f0f4e0815b6;hp=a66369602bbbc98051fbd675628fcfdc1dea47f9;hb=35b8ca3aaf8cb044ad76675dfcad89e000dd4a5c;hpb=f60f22dfbb8cfa0eb55d1896db0e4c3f7d3cfacb diff --git a/src/strv.c b/src/strv.c index a66369602..c8ff5745e 100644 --- a/src/strv.c +++ b/src/strv.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -355,7 +355,7 @@ char **strv_remove(char **l, const char *s) { if (!l) return NULL; - /* Drops every occurence of s in the string list */ + /* Drops every occurrence of s in the string list */ for (f = t = l; *f; f++) { @@ -379,8 +379,8 @@ static int env_append(char **r, char ***k, char **a) { return 0; /* Add the entries of a to *k unless they already exist in *r - * in which case they are overriden instead. This assumes - * there is enough space in the r */ + * in which case they are overridden instead. This assumes + * there is enough space in the r array. */ for (; *a; a++) { char **j; @@ -474,7 +474,7 @@ char **strv_env_delete(char **x, unsigned n_lists, ...) { char **l, **k, **r, **j; va_list ap; - /* Deletes every entry fromx that is mentioned in the other + /* Deletes every entry from x that is mentioned in the other * string lists */ n = strv_length(x); @@ -539,3 +539,83 @@ fail: return NULL; } + +char *strv_env_get_with_length(char **l, const char *name, size_t k) { + char **i; + + assert(name); + + STRV_FOREACH(i, l) + if (strncmp(*i, name, k) == 0 && + (*i)[k] == '=') + return *i + k + 1; + + return NULL; +} + +char *strv_env_get(char **l, const char *name) { + return strv_env_get_with_length(l, name, strlen(name)); +} + +char **strv_env_clean(char **l) { + char **r, **ret; + + for (r = ret = l; *l; l++) { + const char *equal; + + equal = strchr(*l, '='); + + if (equal && equal[1] == 0) { + free(*l); + continue; + } + + *(r++) = *l; + } + + *r = NULL; + + return ret; +} + +char **strv_parse_nulstr(const char *s, size_t l) { + const char *p; + unsigned c = 0, i = 0; + char **v; + + assert(s || l <= 0); + + if (l <= 0) + return strv_new(NULL, NULL); + + for (p = s; p < s + l; p++) + if (*p == 0) + c++; + + if (s[l-1] != 0) + c++; + + if (!(v = new0(char*, c+1))) + return NULL; + + p = s; + while (p < s + l) { + const char *e; + + e = memchr(p, 0, s + l - p); + + if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) { + strv_free(v); + return NULL; + } + + if (!e) + break; + + p = e + 1; + } + + assert(i == c); + + return v; +}