X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fshared%2Fstrv.c;h=a5ce7e9593652f9f095b1801454fd061caf69034;hb=844ec79b3c2f246114ea316ebe1f36044bdb688e;hp=ee0b71ece048bf3a4aa59823ccd654912d38038b;hpb=4d1a69043862ed979642f5688097160355d4cc81;p=elogind.git diff --git a/src/shared/strv.c b/src/shared/strv.c index ee0b71ece..a5ce7e959 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -64,15 +64,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 +84,7 @@ char **strv_copy(char **l) { return r; } -unsigned strv_length(char **l) { +unsigned strv_length(char * const *l) { unsigned n = 0; if (!l) @@ -305,6 +297,31 @@ char **strv_split_quoted(const char *s) { return r; } +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) { char *r, *e; char **s; @@ -370,32 +387,43 @@ fail: return NULL; } -int strv_extend(char ***l, const char *value) { +int strv_push(char ***l, char *value) { char **c; - char *v; unsigned n; if (!value) return 0; - v = strdup(value); - if (!v) - return -ENOMEM; - n = strv_length(*l); c = realloc(*l, sizeof(char*) * (n + 2)); - if (!c) { - free(v); + if (!c) return -ENOMEM; - } - c[n] = v; + c[n] = value; c[n+1] = NULL; *l = c; return 0; } +int strv_extend(char ***l, const char *value) { + char *v; + int r; + + if (!value) + return 0; + + v = strdup(value); + if (!v) + return -ENOMEM; + + r = strv_push(l, v); + if (r < 0) + free(v); + + return r; +} + char **strv_uniq(char **l) { char **i; @@ -504,6 +532,22 @@ char **strv_parse_nulstr(const char *s, size_t l) { return v; } +char **strv_split_nulstr(const char *s) { + const char *i; + char **r = NULL; + + 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, **j;