1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
12 #include "extract-word.h"
14 #include "string-util.h"
18 char *strv_find(char **l, const char *name) {
30 char *strv_find_prefix(char **l, const char *name) {
36 if (startswith(*i, name))
42 char *strv_find_startswith(char **l, const char *name) {
47 /* Like strv_find_prefix, but actually returns only the
48 * suffix, not the whole item */
51 e = startswith(*i, name);
59 void strv_clear(char **l) {
71 char **strv_free(char **l) {
76 char **strv_free_erase(char **l) {
85 char **strv_copy(char * const *l) {
88 k = r = new(char*, strv_length(l) + 1);
93 for (; *l; k++, l++) {
105 size_t strv_length(char * const *l) {
117 char **strv_new_ap(const char *x, va_list ap) {
119 _cleanup_strv_free_ char **a = NULL;
123 /* As a special trick we ignore all listed strings that equal
124 * STRV_IGNORE. This is supposed to be used with the
125 * STRV_IFNOTNULL() macro to include possibly NULL strings in
126 * the string list. */
129 n = x == STRV_IGNORE ? 0 : 1;
132 while ((s = va_arg(aq, const char*))) {
133 if (s == STRV_IGNORE)
147 if (x != STRV_IGNORE) {
154 while ((s = va_arg(ap, const char*))) {
156 if (s == STRV_IGNORE)
172 char **strv_new(const char *x, ...) {
177 r = strv_new_ap(x, ap);
183 #if 0 /// UNNEEDED by elogind
184 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
186 size_t p, q, i = 0, j;
196 t = reallocarray(*a, p + q + 1, sizeof(char *));
205 if (filter_duplicates && strv_contains(t, *s))
221 for (j = 0; j < i; j++)
228 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
235 v = strappend(*s, suffix);
250 char **strv_split(const char *s, const char *separator) {
251 const char *word, *state;
259 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
267 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
268 r[i] = strndup(word, l);
281 #if 0 /// UNNEEDED by elogind
282 char **strv_split_newlines(const char *s) {
288 /* Special version of strv_split() that splits on newlines and
289 * suppresses an empty string at the end */
291 l = strv_split(s, NEWLINE);
299 if (isempty(l[n - 1]))
300 l[n - 1] = mfree(l[n - 1]);
306 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
307 _cleanup_strv_free_ char **l = NULL;
308 size_t n = 0, allocated = 0;
315 _cleanup_free_ char *word = NULL;
317 r = extract_first_word(&s, &word, separators, flags);
323 if (!GREEDY_REALLOC(l, allocated, n + 2))
326 l[n++] = TAKE_PTR(word);
342 char *strv_join(char **l, const char *separator) {
350 k = strlen(separator);
366 e = stpcpy(e, separator);
376 #if 0 /// UNNEEDED by elogind
378 int strv_push(char ***l, char *value) {
387 /* Increase and check for overflow */
392 c = reallocarray(*l, m, sizeof(char*));
403 int strv_push_pair(char ***l, char *a, char *b) {
412 /* increase and check for overflow */
413 m = n + !!a + !!b + 1;
417 c = reallocarray(*l, m, sizeof(char*));
431 int strv_insert(char ***l, size_t position, char *value) {
439 position = MIN(position, n);
441 /* increase and check for overflow */
450 for (i = 0; i < position; i++)
453 for (i = position; i < n; i++)
464 int strv_consume(char ***l, char *value) {
467 r = strv_push(l, value);
474 #if 0 /// UNNEEDED by elogind
475 int strv_consume_pair(char ***l, char *a, char *b) {
478 r = strv_push_pair(l, a, b);
488 int strv_consume_prepend(char ***l, char *value) {
491 r = strv_push_prepend(l, value);
498 int strv_extend(char ***l, const char *value) {
508 return strv_consume(l, v);
511 int strv_extend_front(char ***l, const char *value) {
517 /* Like strv_extend(), but prepends rather than appends the new entry */
524 /* Increase and overflow check. */
533 c = reallocarray(*l, m, sizeof(char*));
539 memmove(c+1, c, n * sizeof(char*));
547 char **strv_uniq(char **l) {
550 /* Drops duplicate entries. The first identical string will be
551 * kept, the others dropped */
554 strv_remove(i+1, *i);
559 #if 0 /// UNNEEDED by elogind
560 bool strv_is_uniq(char **l) {
564 if (strv_find(i+1, *i))
571 char **strv_remove(char **l, const char *s) {
579 /* Drops every occurrence of s in the string list, edits
582 for (f = t = l; *f; f++)
592 char **strv_parse_nulstr(const char *s, size_t l) {
593 /* l is the length of the input data, which will be split at NULs into
594 * elements of the resulting strv. Hence, the number of items in the resulting strv
595 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
596 * unless s[l-1] is NUL, in which case the final empty string is not stored in
597 * the resulting strv, and length is equal to the number of NUL bytes.
599 * Note that contrary to a normal nulstr which cannot contain empty strings, because
600 * the input data is terminated by any two consequent NUL bytes, this parser accepts
601 * empty strings in s.
611 return new0(char*, 1);
613 for (p = s; p < s + l; p++)
620 v = new0(char*, c+1);
628 e = memchr(p, 0, s + l - p);
630 v[i] = strndup(p, e ? e - p : s + l - p);
649 char **strv_split_nulstr(const char *s) {
654 if (strv_extend(&r, i) < 0) {
660 return strv_new(NULL, NULL);
665 #if 0 /// UNNEEDED by elogind
666 int strv_make_nulstr(char **l, char **p, size_t *q) {
667 /* A valid nulstr with two NULs at the end will be created, but
668 * q will be the length without the two trailing NULs. Thus the output
669 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
670 * and can also be parsed by strv_parse_nulstr as long as the length
671 * is provided separately.
674 size_t n_allocated = 0, n = 0;
675 _cleanup_free_ char *m = NULL;
686 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
689 memcpy(m + n, *i, z + 1);
699 /* make sure there is a second extra NUL at the end of resulting nulstr */
711 bool strv_overlap(char **a, char **b) {
715 if (strv_contains(b, *i))
722 static int str_compare(const void *_a, const void *_b) {
723 const char **a = (const char**) _a, **b = (const char**) _b;
725 return strcmp(*a, *b);
728 char **strv_sort(char **l) {
729 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
733 bool strv_equal(char **a, char **b) {
736 return strv_isempty(b);
741 for ( ; *a || *b; ++a, ++b)
742 if (!streq_ptr(*a, *b))
748 void strv_print(char **l) {
755 #if 0 /// UNNEEDED by elogind
756 int strv_extendf(char ***l, const char *format, ...) {
761 va_start(ap, format);
762 r = vasprintf(&x, format, ap);
768 return strv_consume(l, x);
771 char **strv_reverse(char **l) {
778 for (i = 0; i < n / 2; i++)
779 SWAP_TWO(l[i], l[n-1-i]);
784 char **strv_shell_escape(char **l, const char *bad) {
787 /* Escapes every character in every string in l that is in bad,
788 * edits in-place, does not roll-back on error. */
793 v = shell_escape(*s, bad);
804 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
807 STRV_FOREACH(p, patterns)
808 if (fnmatch(*p, s, flags) == 0)
814 char ***strv_free_free(char ***l) {
826 char **strv_skip(char **l, size_t n) {
839 int strv_extend_n(char ***l, const char *value, size_t n) {
850 /* Adds the value n times to l */
854 nl = reallocarray(*l, k + n + 1, sizeof(char *));
860 for (i = k; i < k + n; i++) {
861 nl[i] = strdup(value);
870 for (j = k; j < i; j++)
877 #if 0 /// UNNEEDED by elogind
878 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
883 /* Like fputs(), but for strv, and with a less stupid argument order */
889 r = fputs_with_space(f, *s, separator, space);