X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fstrv.c;h=00857e40a7a3b500865c9516a2bc69fd25a8432e;hb=b5884878a2874447b2a9f07f324a7cd909d96d48;hp=ec25755289a2c5a8e9aecc689693e5e0269ec0a4;hpb=fabe5c0e5fce730aa66e10a9c4f9fdd443d7aeda;p=elogind.git diff --git a/src/shared/strv.c b/src/shared/strv.c index ec2575528..00857e40a 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -52,6 +52,23 @@ char *strv_find_prefix(char **l, const char *name) { return NULL; } +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_free(char **l) { char **k; @@ -64,15 +81,7 @@ void strv_free(char **l) { free(l); } -void strv_freep(char ***l) { - if (!l) - return; - - strv_free(*l); - *l = 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 +101,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 +183,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 +226,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 +234,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,9 +248,8 @@ char **strv_split(const char *s, const char *separator) { return r; } -char **strv_split_quoted(const char *s) { - char *state; - char *w; +int strv_split_quoted(char ***t, const char *s) { + const char *word, *state; size_t l; unsigned n, i; char **r; @@ -284,25 +257,54 @@ char **strv_split_quoted(const char *s) { assert(s); n = 0; - FOREACH_WORD_QUOTED(w, l, s, state) + FOREACH_WORD_QUOTED(word, l, s, state) n++; + if (!isempty(state)) + /* bad syntax */ + return -EINVAL; r = new(char*, n+1); if (!r) - return NULL; + return -ENOMEM; i = 0; - FOREACH_WORD_QUOTED(w, l, s, state) { - r[i] = cunescape_length(w, l); + FOREACH_WORD_QUOTED(word, l, s, state) { + r[i] = cunescape_length(word, l); if (!r[i]) { strv_free(r); - return NULL; + return -ENOMEM; } i++; } r[i] = NULL; - return r; + *t = r; + return 0; +} + +char **strv_split_newlines(const char *s) { + char **l; + unsigned n; + + assert(s); + + /* Special version of strv_split() that splits on newlines and + * suppresses an empty string at the end */ + + l = strv_split(s, NEWLINE); + if (!l) + return NULL; + + n = strv_length(l); + if (n <= 0) + return l; + + if (isempty(l[n-1])) { + free(l[n-1]); + l[n-1] = NULL; + } + + return l; } char *strv_join(char **l, const char *separator) { @@ -339,101 +341,144 @@ 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; + c = realloc_multiply(*l, sizeof(char*), m); + if (!c) + return -ENOMEM; + + c[n] = value; + c[n+1] = NULL; + + *l = c; + return 0; +} + +int strv_push_prepend(char ***l, char *value) { + char **c; + unsigned n, m, i; + + if (!value) + return 0; + n = strv_length(*l); - c = realloc(*l, sizeof(char*) * (n + 2)); - if (!c) { - free(v); + + /* increase and check for overflow */ + m = n + 2; + if (m < n) return -ENOMEM; - } - c[n] = v; + c = new(char*, m); + if (!c) + return -ENOMEM; + + for (i = 0; i < n; i++) + c[i+1] = (*l)[i]; + + c[0] = value; c[n+1] = NULL; + free(*l); *l = c; + return 0; } -char **strv_uniq(char **l) { - char **i; +int strv_consume(char ***l, char *value) { + int r; - /* Drops duplicate entries. The first identical string will be - * kept, the others dropped */ + r = strv_push(l, value); + if (r < 0) + free(value); - STRV_FOREACH(i, l) - strv_remove(i+1, *i); + return r; +} - return l; +int strv_consume_prepend(char ***l, char *value) { + int r; + + r = strv_push_prepend(l, value); + if (r < 0) + free(value); + + return r; } -char **strv_remove(char **l, const char *s) { - char **f, **t; +int strv_extend(char ***l, const char *value) { + char *v; - if (!l) - return NULL; + if (!value) + return 0; - assert(s); + v = strdup(value); + if (!v) + return -ENOMEM; - /* Drops every occurrence of s in the string list, edits - * in-place. */ + return strv_consume(l, v); +} - for (f = t = l; *f; f++) { +char **strv_uniq(char **l) { + char **i; - if (streq(*f, s)) { - free(*f); - continue; - } + /* Drops duplicate entries. The first identical string will be + * kept, the others dropped */ - *(t++) = *f; - } + STRV_FOREACH(i, l) + strv_remove(i+1, *i); - *t = NULL; return l; } -char **strv_remove_prefix(char **l, const char *s) { +char **strv_remove(char **l, const char *s) { char **f, **t; if (!l) @@ -441,18 +486,14 @@ char **strv_remove_prefix(char **l, const char *s) { assert(s); - /* Drops every occurrence of a string prefixed with s in the - * string list, edits in-place. */ - - for (f = t = l; *f; f++) { + /* Drops every occurrence of s in the string list, edits + * in-place. */ - if (startswith(*f, s)) { + for (f = t = l; *f; f++) + if (streq(*f, s)) free(*f); - continue; - } - - *(t++) = *f; - } + else + *(t++) = *f; *t = NULL; return l; @@ -466,7 +507,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) @@ -521,14 +562,11 @@ char **strv_split_nulstr(const char *s) { } bool strv_overlap(char **a, char **b) { - char **i, **j; + char **i; - STRV_FOREACH(i, a) { - STRV_FOREACH(j, b) { - if (streq(*i, *j)) - return true; - } - } + STRV_FOREACH(i, a) + if (strv_contains(b, *i)) + return true; return false; } @@ -551,9 +589,21 @@ char **strv_sort(char **l) { 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); +}