1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
13 #include "alloc-util.h"
15 #include "extract-word.h"
17 #include "string-util.h"
21 char *strv_find(char **l, const char *name) {
33 char *strv_find_prefix(char **l, const char *name) {
39 if (startswith(*i, name))
45 char *strv_find_startswith(char **l, const char *name) {
50 /* Like strv_find_prefix, but actually returns only the
51 * suffix, not the whole item */
54 e = startswith(*i, name);
62 void strv_clear(char **l) {
74 char **strv_free(char **l) {
79 char **strv_free_erase(char **l) {
88 char **strv_copy(char * const *l) {
91 k = r = new(char*, strv_length(l) + 1);
96 for (; *l; k++, l++) {
108 size_t strv_length(char * const *l) {
120 char **strv_new_ap(const char *x, va_list ap) {
122 _cleanup_strv_free_ char **a = NULL;
126 /* As a special trick we ignore all listed strings that equal
127 * STRV_IGNORE. This is supposed to be used with the
128 * STRV_IFNOTNULL() macro to include possibly NULL strings in
129 * the string list. */
132 n = x == STRV_IGNORE ? 0 : 1;
135 while ((s = va_arg(aq, const char*))) {
136 if (s == STRV_IGNORE)
150 if (x != STRV_IGNORE) {
157 while ((s = va_arg(ap, const char*))) {
159 if (s == STRV_IGNORE)
175 char **strv_new(const char *x, ...) {
180 r = strv_new_ap(x, ap);
186 #if 0 /// UNNEEDED by elogind
187 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
189 size_t p, q, i = 0, j;
199 t = reallocarray(*a, p + q + 1, sizeof(char *));
208 if (filter_duplicates && strv_contains(t, *s))
224 for (j = 0; j < i; j++)
231 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
238 v = strappend(*s, suffix);
253 char **strv_split(const char *s, const char *separator) {
254 const char *word, *state;
262 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
270 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
271 r[i] = strndup(word, l);
284 #if 0 /// UNNEEDED by elogind
285 char **strv_split_newlines(const char *s) {
291 /* Special version of strv_split() that splits on newlines and
292 * suppresses an empty string at the end */
294 l = strv_split(s, NEWLINE);
302 if (isempty(l[n - 1]))
303 l[n - 1] = mfree(l[n - 1]);
309 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
310 _cleanup_strv_free_ char **l = NULL;
311 size_t n = 0, allocated = 0;
318 _cleanup_free_ char *word = NULL;
320 r = extract_first_word(&s, &word, separators, flags);
326 if (!GREEDY_REALLOC(l, allocated, n + 2))
329 l[n++] = TAKE_PTR(word);
345 char *strv_join(char **l, const char *separator) {
353 k = strlen(separator);
369 e = stpcpy(e, separator);
379 #if 0 /// UNNEEDED by elogind
381 int strv_push(char ***l, char *value) {
390 /* Increase and check for overflow */
395 c = reallocarray(*l, m, sizeof(char*));
406 int strv_push_pair(char ***l, char *a, char *b) {
415 /* increase and check for overflow */
416 m = n + !!a + !!b + 1;
420 c = reallocarray(*l, m, sizeof(char*));
434 int strv_insert(char ***l, size_t position, char *value) {
442 position = MIN(position, n);
444 /* increase and check for overflow */
453 for (i = 0; i < position; i++)
456 for (i = position; i < n; i++)
467 int strv_consume(char ***l, char *value) {
470 r = strv_push(l, value);
477 #if 0 /// UNNEEDED by elogind
478 int strv_consume_pair(char ***l, char *a, char *b) {
481 r = strv_push_pair(l, a, b);
491 int strv_consume_prepend(char ***l, char *value) {
494 r = strv_push_prepend(l, value);
501 int strv_extend(char ***l, const char *value) {
511 return strv_consume(l, v);
514 int strv_extend_front(char ***l, const char *value) {
520 /* Like strv_extend(), but prepends rather than appends the new entry */
527 /* Increase and overflow check. */
536 c = reallocarray(*l, m, sizeof(char*));
542 memmove(c+1, c, n * sizeof(char*));
550 char **strv_uniq(char **l) {
553 /* Drops duplicate entries. The first identical string will be
554 * kept, the others dropped */
557 strv_remove(i+1, *i);
562 #if 0 /// UNNEEDED by elogind
563 bool strv_is_uniq(char **l) {
567 if (strv_find(i+1, *i))
574 char **strv_remove(char **l, const char *s) {
582 /* Drops every occurrence of s in the string list, edits
585 for (f = t = l; *f; f++)
595 char **strv_parse_nulstr(const char *s, size_t l) {
596 /* l is the length of the input data, which will be split at NULs into
597 * elements of the resulting strv. Hence, the number of items in the resulting strv
598 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
599 * unless s[l-1] is NUL, in which case the final empty string is not stored in
600 * the resulting strv, and length is equal to the number of NUL bytes.
602 * Note that contrary to a normal nulstr which cannot contain empty strings, because
603 * the input data is terminated by any two consequent NUL bytes, this parser accepts
604 * empty strings in s.
614 return new0(char*, 1);
616 for (p = s; p < s + l; p++)
623 v = new0(char*, c+1);
631 e = memchr(p, 0, s + l - p);
633 v[i] = strndup(p, e ? e - p : s + l - p);
652 char **strv_split_nulstr(const char *s) {
657 if (strv_extend(&r, i) < 0) {
663 return strv_new(NULL, NULL);
668 #if 0 /// UNNEEDED by elogind
669 int strv_make_nulstr(char **l, char **p, size_t *q) {
670 /* A valid nulstr with two NULs at the end will be created, but
671 * q will be the length without the two trailing NULs. Thus the output
672 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
673 * and can also be parsed by strv_parse_nulstr as long as the length
674 * is provided separately.
677 size_t n_allocated = 0, n = 0;
678 _cleanup_free_ char *m = NULL;
689 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
692 memcpy(m + n, *i, z + 1);
702 /* make sure there is a second extra NUL at the end of resulting nulstr */
714 bool strv_overlap(char **a, char **b) {
718 if (strv_contains(b, *i))
725 static int str_compare(const void *_a, const void *_b) {
726 const char **a = (const char**) _a, **b = (const char**) _b;
728 return strcmp(*a, *b);
731 char **strv_sort(char **l) {
732 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
736 bool strv_equal(char **a, char **b) {
739 return strv_isempty(b);
744 for ( ; *a || *b; ++a, ++b)
745 if (!streq_ptr(*a, *b))
751 void strv_print(char **l) {
758 #if 0 /// UNNEEDED by elogind
759 int strv_extendf(char ***l, const char *format, ...) {
764 va_start(ap, format);
765 r = vasprintf(&x, format, ap);
771 return strv_consume(l, x);
774 char **strv_reverse(char **l) {
781 for (i = 0; i < n / 2; i++)
782 SWAP_TWO(l[i], l[n-1-i]);
787 char **strv_shell_escape(char **l, const char *bad) {
790 /* Escapes every character in every string in l that is in bad,
791 * edits in-place, does not roll-back on error. */
796 v = shell_escape(*s, bad);
807 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
810 STRV_FOREACH(p, patterns)
811 if (fnmatch(*p, s, flags) == 0)
817 char ***strv_free_free(char ***l) {
829 char **strv_skip(char **l, size_t n) {
842 int strv_extend_n(char ***l, const char *value, size_t n) {
853 /* Adds the value n times to l */
857 nl = reallocarray(*l, k + n + 1, sizeof(char *));
863 for (i = k; i < k + n; i++) {
864 nl[i] = strdup(value);
873 for (j = k; j < i; j++)
880 #if 0 /// UNNEEDED by elogind
881 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
886 /* Like fputs(), but for strv, and with a less stupid argument order */
892 r = fputs_with_space(f, *s, separator, space);