X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fstrv.c;h=d44a72fc484d09966ac745066499a71cedd05c65;hp=fc6104ffeaaba438065c3a9e594f923a429e21b1;hb=d8d5781c1eb031477670facc132e198f8fddd56c;hpb=7c2d80944afb4196f2eff614e8da1450dffcbeaa diff --git a/src/shared/strv.c b/src/shared/strv.c index fc6104ffe..d44a72fc4 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -19,7 +19,6 @@ along with systemd; If not, see . ***/ -#include #include #include #include @@ -52,7 +51,24 @@ char *strv_find_prefix(char **l, const char *name) { return NULL; } -void strv_free(char **l) { +char *strv_find_startswith(char **l, const char *name) { + char **i, *e; + + assert(name); + + /* Like strv_find_prefix, but actually returns only the + * suffix, not the whole item */ + + STRV_FOREACH(i, l) { + e = startswith(*i, name); + if (e) + return e; + } + + return NULL; +} + +void strv_clear(char **l) { char **k; if (!l) @@ -61,18 +77,16 @@ void strv_free(char **l) { for (k = l; *k; k++) free(*k); - free(l); + *l = NULL; } -void strv_freep(char ***l) { - if (!l) - return; - - strv_free(*l); - *l = NULL; +char **strv_free(char **l) { + strv_clear(l); + free(l); + return NULL; } -char **strv_copy(char **l) { +char **strv_copy(char * const *l) { char **r, **k; k = r = new(char*, strv_length(l) + 1); @@ -92,7 +106,7 @@ char **strv_copy(char **l) { return r; } -unsigned strv_length(char **l) { +unsigned strv_length(char * const *l) { unsigned n = 0; if (!l) @@ -174,77 +188,42 @@ char **strv_new(const char *x, ...) { return r; } -char **strv_merge(char **a, char **b) { - char **r, **k; - - if (!a) - return strv_copy(b); - - if (!b) - return strv_copy(a); - - r = new(char*, strv_length(a) + strv_length(b) + 1); - if (!r) - return NULL; - - for (k = r; *a; k++, a++) { - *k = strdup(*a); - if (!*k) - goto fail; - } +int strv_extend_strv(char ***a, char **b) { + int r; + char **s; - for (; *b; k++, b++) { - *k = strdup(*b); - if (!*k) - goto fail; + STRV_FOREACH(s, b) { + r = strv_extend(a, *s); + if (r < 0) + return r; } - *k = NULL; - return r; - -fail: - strv_free(r); - return NULL; + return 0; } -char **strv_merge_concat(char **a, char **b, const char *suffix) { - char **r, **k; - - /* Like strv_merge(), but appends suffix to all strings in b, before adding */ +int strv_extend_strv_concat(char ***a, char **b, const char *suffix) { + int r; + char **s; - if (!b) - return strv_copy(a); + STRV_FOREACH(s, b) { + char *v; - r = new(char*, strv_length(a) + strv_length(b) + 1); - if (!r) - return NULL; + v = strappend(*s, suffix); + if (!v) + return -ENOMEM; - k = r; - if (a) - for (; *a; k++, a++) { - *k = strdup(*a); - if (!*k) - goto fail; + r = strv_push(a, v); + if (r < 0) { + free(v); + return r; } - - for (; *b; k++, b++) { - *k = strappend(*b, suffix); - if (!*k) - goto fail; } - *k = NULL; - return r; - -fail: - strv_free(r); - return NULL; - + return 0; } char **strv_split(const char *s, const char *separator) { - char *state; - char *w; + const char *word, *state; size_t l; unsigned n, i; char **r; @@ -252,7 +231,7 @@ char **strv_split(const char *s, const char *separator) { assert(s); n = 0; - FOREACH_WORD_SEPARATOR(w, l, s, separator, state) + FOREACH_WORD_SEPARATOR(word, l, s, separator, state) n++; r = new(char*, n+1); @@ -260,8 +239,8 @@ char **strv_split(const char *s, const char *separator) { return NULL; i = 0; - FOREACH_WORD_SEPARATOR(w, l, s, separator, state) { - r[i] = strndup(w, l); + FOREACH_WORD_SEPARATOR(word, l, s, separator, state) { + r[i] = strndup(word, l); if (!r[i]) { strv_free(r); return NULL; @@ -274,35 +253,64 @@ char **strv_split(const char *s, const char *separator) { return r; } -char **strv_split_quoted(const char *s) { - char *state; - char *w; - size_t l; - unsigned n, i; - char **r; +char **strv_split_newlines(const char *s) { + char **l; + unsigned n; assert(s); - n = 0; - FOREACH_WORD_QUOTED(w, l, s, state) - n++; + /* Special version of strv_split() that splits on newlines and + * suppresses an empty string at the end */ - r = new(char*, n+1); - if (!r) + l = strv_split(s, NEWLINE); + if (!l) return NULL; - i = 0; - FOREACH_WORD_QUOTED(w, l, s, state) { - r[i] = cunescape_length(w, l); - if (!r[i]) { - strv_free(r); - return NULL; - } - i++; + n = strv_length(l); + if (n <= 0) + return l; + + if (isempty(l[n-1])) { + free(l[n-1]); + l[n-1] = NULL; } - r[i] = NULL; - return r; + return l; +} + +int strv_split_quoted(char ***t, const char *s, UnquoteFlags flags) { + size_t n = 0, allocated = 0; + _cleanup_strv_free_ char **l = NULL; + int r; + + assert(t); + assert(s); + + for (;;) { + _cleanup_free_ char *word = NULL; + + r = unquote_first_word(&s, &word, flags); + if (r < 0) + return r; + if (r == 0) + break; + + if (!GREEDY_REALLOC(l, allocated, n + 2)) + return -ENOMEM; + + l[n++] = word; + word = NULL; + + l[n] = NULL; + } + + if (!l) + l = new0(char*, 1); + + *t = l; + l = NULL; + + return 0; } char *strv_join(char **l, const char *separator) { @@ -339,366 +347,212 @@ char *strv_join(char **l, const char *separator) { return r; } -char **strv_append(char **l, const char *s) { - char **r, **k; - - if (!l) - return strv_new(s, NULL); - - if (!s) - return strv_copy(l); - - r = new(char*, strv_length(l)+2); - if (!r) - return NULL; +char *strv_join_quoted(char **l) { + char *buf = NULL; + char **s; + size_t allocated = 0, len = 0; - for (k = r; *l; k++, l++) { - *k = strdup(*l); - if (!*k) - goto fail; + STRV_FOREACH(s, l) { + /* assuming here that escaped string cannot be more + * than twice as long, and reserving space for the + * separator and quotes. + */ + _cleanup_free_ char *esc = NULL; + size_t needed; + + if (!GREEDY_REALLOC(buf, allocated, + len + strlen(*s) * 2 + 3)) + goto oom; + + esc = cescape(*s); + if (!esc) + goto oom; + + needed = snprintf(buf + len, allocated - len, "%s\"%s\"", + len > 0 ? " " : "", esc); + assert(needed < allocated - len); + len += needed; } - k[0] = strdup(s); - if (!k[0]) - goto fail; + if (!buf) + buf = malloc0(1); - k[1] = NULL; - return r; + return buf; -fail: - strv_free(r); + oom: + free(buf); return NULL; } -int strv_extend(char ***l, const char *value) { +int strv_push(char ***l, char *value) { char **c; - char *v; - unsigned n; + unsigned n, m; if (!value) return 0; - v = strdup(value); - if (!v) + n = strv_length(*l); + + /* Increase and check for overflow */ + m = n + 2; + if (m < n) return -ENOMEM; - n = strv_length(*l); - c = realloc(*l, sizeof(char*) * (n + 2)); - if (!c) { - free(v); + c = realloc_multiply(*l, sizeof(char*), m); + if (!c) return -ENOMEM; - } - c[n] = v; + c[n] = value; c[n+1] = NULL; *l = c; return 0; } -char **strv_uniq(char **l) { - char **i; - - /* Drops duplicate entries. The first identical string will be - * kept, the others dropped */ - - STRV_FOREACH(i, l) - strv_remove(i+1, *i); - - return l; -} - -char **strv_remove(char **l, const char *s) { - char **f, **t; - - if (!l) - return NULL; - - assert(s); - - /* Drops every occurrence of s in the string list, edits - * in-place. */ - - for (f = t = l; *f; f++) { - - if (streq(*f, s)) { - free(*f); - continue; - } - - *(t++) = *f; - } - - *t = NULL; - return l; -} - -char **strv_remove_prefix(char **l, const char *s) { - char **f, **t; - - if (!l) - return NULL; +int strv_push_pair(char ***l, char *a, char *b) { + char **c; + unsigned n, m; - assert(s); + if (!a && !b) + return 0; - /* Drops every occurrence of a string prefixed with s in the - * string list, edits in-place. */ + n = strv_length(*l); - for (f = t = l; *f; f++) { + /* increase and check for overflow */ + m = n + !!a + !!b + 1; + if (m < n) + return -ENOMEM; - if (startswith(*f, s)) { - free(*f); - continue; - } + c = realloc_multiply(*l, sizeof(char*), m); + if (!c) + return -ENOMEM; - *(t++) = *f; - } + if (a) + c[n++] = a; + if (b) + c[n++] = b; + c[n] = NULL; - *t = NULL; - return l; + *l = c; + return 0; } -static int env_append(char **r, char ***k, char **a) { - assert(r); - assert(k); +int strv_push_prepend(char ***l, char *value) { + char **c; + unsigned n, m, i; - if (!a) + if (!value) return 0; - /* Add the entries of a to *k unless they already exist in *r - * in which case they are overridden instead. This assumes - * there is enough space in the r array. */ - - for (; *a; a++) { - char **j; - size_t n; + n = strv_length(*l); - n = strcspn(*a, "="); + /* increase and check for overflow */ + m = n + 2; + if (m < n) + return -ENOMEM; - if ((*a)[n] == '=') - n++; + c = new(char*, m); + if (!c) + return -ENOMEM; - for (j = r; j < *k; j++) - if (strncmp(*j, *a, n) == 0) - break; + for (i = 0; i < n; i++) + c[i+1] = (*l)[i]; - if (j >= *k) - (*k)++; - else - free(*j); + c[0] = value; + c[n+1] = NULL; - *j = strdup(*a); - if (!*j) - return -ENOMEM; - } + free(*l); + *l = c; return 0; } -char **strv_env_merge(unsigned n_lists, ...) { - size_t n = 0; - char **l, **k, **r; - va_list ap; - unsigned i; - - /* Merges an arbitrary number of environment sets */ - - va_start(ap, n_lists); - for (i = 0; i < n_lists; i++) { - l = va_arg(ap, char**); - n += strv_length(l); - } - va_end(ap); - - r = new(char*, n+1); - if (!r) - return NULL; - - k = r; +int strv_consume(char ***l, char *value) { + int r; - va_start(ap, n_lists); - for (i = 0; i < n_lists; i++) { - l = va_arg(ap, char**); - if (env_append(r, &k, l) < 0) - goto fail; - } - va_end(ap); - - *k = NULL; + r = strv_push(l, value); + if (r < 0) + free(value); return r; - -fail: - va_end(ap); - strv_free(r); - - 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); +int strv_consume_pair(char ***l, char *a, char *b) { + int r; - return strncmp(t, pattern, l) == 0 && t[l] == '='; + r = strv_push_pair(l, a, b); + if (r < 0) { + free(a); + free(b); } - return false; + return r; } -char **strv_env_delete(char **x, unsigned n_lists, ...) { - size_t n, i = 0; - char **k, **r; - va_list ap; - - /* Deletes every entry from x that is mentioned in the other - * string lists */ - - n = strv_length(x); - - r = new(char*, n+1); - if (!r) - return NULL; - - STRV_FOREACH(k, x) { - unsigned v; - - va_start(ap, n_lists); - for (v = 0; v < n_lists; v++) { - char **l, **j; - - l = va_arg(ap, char**); - STRV_FOREACH(j, l) - if (env_match(*k, *j)) - goto skip; - } - va_end(ap); - - r[i] = strdup(*k); - if (!r[i]) { - strv_free(r); - return NULL; - } - - i++; - continue; - - skip: - va_end(ap); - } - - r[i] = NULL; +int strv_consume_prepend(char ***l, char *value) { + int r; - assert(i <= n); + r = strv_push_prepend(l, value); + if (r < 0) + free(value); 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++) { +int strv_extend(char ***l, const char *value) { + char *v; - if (env_match(*f, p)) { - free(*f); - continue; - } + if (!value) + return 0; - *(t++) = *f; - } + v = strdup(value); + if (!v) + return -ENOMEM; - *t = NULL; - return l; + return strv_consume(l, v); } -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 */ - - r = new(char*, strv_length(x)+2); - if (!r) - return NULL; - - k = r; - if (env_append(r, &k, x) < 0) - goto fail; - - if (env_append(r, &k, m) < 0) - goto fail; - - *k = NULL; +char **strv_uniq(char **l) { + char **i; - return r; + /* Drops duplicate entries. The first identical string will be + * kept, the others dropped */ -fail: - strv_free(r); - return NULL; + STRV_FOREACH(i, l) + strv_remove(i+1, *i); + return l; } -char *strv_env_get_with_length(char **l, const char *name, size_t k) { +bool strv_is_uniq(char **l) { char **i; - assert(name); - STRV_FOREACH(i, l) - if (strncmp(*i, name, k) == 0 && - (*i)[k] == '=') - return *i + k + 1; - - return NULL; -} + if (strv_find(i+1, *i)) + return false; -char *strv_env_get(char **l, const char *name) { - return strv_env_get_with_length(l, name, strlen(name)); + return true; } -char **strv_env_clean(char **l) { - char **r, **ret; - - for (r = ret = l; *l; l++) { - const char *equal; +char **strv_remove(char **l, const char *s) { + char **f, **t; - equal = strchr(*l, '='); + if (!l) + return NULL; - if (equal && equal[1] == 0) { - free(*l); - continue; - } + assert(s); - *(r++) = *l; - } + /* Drops every occurrence of s in the string list, edits + * in-place. */ - *r = NULL; + for (f = t = l; *f; f++) + if (streq(*f, s)) + free(*f); + else + *(t++) = *f; - return ret; + *t = NULL; + return l; } char **strv_parse_nulstr(const char *s, size_t l) { @@ -709,7 +563,7 @@ char **strv_parse_nulstr(const char *s, size_t l) { assert(s || l <= 0); if (l <= 0) - return strv_new(NULL, NULL); + return new0(char*, 1); for (p = s; p < s + l; p++) if (*p == 0) @@ -747,15 +601,28 @@ char **strv_parse_nulstr(const char *s, size_t l) { return v; } -bool strv_overlap(char **a, char **b) { - char **i, **j; +char **strv_split_nulstr(const char *s) { + const char *i; + char **r = NULL; - STRV_FOREACH(i, a) { - STRV_FOREACH(j, b) { - if (streq(*i, *j)) - return true; + NULSTR_FOREACH(i, s) + if (strv_extend(&r, i) < 0) { + strv_free(r); + return NULL; } - } + + if (!r) + return strv_new(NULL, NULL); + + return r; +} + +bool strv_overlap(char **a, char **b) { + char **i; + + STRV_FOREACH(i, a) + if (strv_contains(b, *i)) + return true; return false; } @@ -775,12 +642,63 @@ char **strv_sort(char **l) { return l; } +bool strv_equal(char **a, char **b) { + if (!a || !b) + return a == b; + + for ( ; *a || *b; ++a, ++b) + if (!streq_ptr(*a, *b)) + return false; + + return true; +} + void strv_print(char **l) { char **s; - if (!l) - return; - STRV_FOREACH(s, l) puts(*s); } + +int strv_extendf(char ***l, const char *format, ...) { + va_list ap; + char *x; + int r; + + va_start(ap, format); + r = vasprintf(&x, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return strv_consume(l, x); +} + +char **strv_reverse(char **l) { + unsigned n, i; + + n = strv_length(l); + if (n <= 1) + return l; + + for (i = 0; i < n / 2; i++) { + char *t; + + t = l[i]; + l[i] = l[n-1-i]; + l[n-1-i] = t; + } + + return l; +} + +bool strv_fnmatch(char* const* patterns, const char *s, int flags) { + char* const* p; + + STRV_FOREACH(p, patterns) + if (fnmatch(*p, s, 0) == 0) + return true; + + return false; +}