1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include "alloc-util.h"
17 #include "extract-word.h"
19 #include "string-util.h"
23 char *strv_find(char **l, const char *name) {
35 char *strv_find_prefix(char **l, const char *name) {
41 if (startswith(*i, name))
47 char *strv_find_startswith(char **l, const char *name) {
52 /* Like strv_find_prefix, but actually returns only the
53 * suffix, not the whole item */
56 e = startswith(*i, name);
64 void strv_clear(char **l) {
76 char **strv_free(char **l) {
81 char **strv_free_erase(char **l) {
90 char **strv_copy(char * const *l) {
93 k = r = new(char*, strv_length(l) + 1);
98 for (; *l; k++, l++) {
110 unsigned strv_length(char * const *l) {
122 char **strv_new_ap(const char *x, va_list ap) {
125 unsigned n = 0, i = 0;
128 /* As a special trick we ignore all listed strings that equal
129 * STRV_IGNORE. This is supposed to be used with the
130 * STRV_IFNOTNULL() macro to include possibly NULL strings in
131 * the string list. */
134 n = x == STRV_IGNORE ? 0 : 1;
137 while ((s = va_arg(aq, const char*))) {
138 if (s == STRV_IGNORE)
152 if (x != STRV_IGNORE) {
159 while ((s = va_arg(ap, const char*))) {
161 if (s == STRV_IGNORE)
181 char **strv_new(const char *x, ...) {
186 r = strv_new_ap(x, ap);
192 #if 0 /// UNNEEDED by elogind
193 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
195 size_t p, q, i = 0, j;
205 t = reallocarray(*a, p + q + 1, sizeof(char *));
214 if (filter_duplicates && strv_contains(t, *s))
230 for (j = 0; j < i; j++)
237 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
244 v = strappend(*s, suffix);
259 char **strv_split(const char *s, const char *separator) {
260 const char *word, *state;
268 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
276 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
277 r[i] = strndup(word, l);
290 #if 0 /// UNNEEDED by elogind
291 char **strv_split_newlines(const char *s) {
297 /* Special version of strv_split() that splits on newlines and
298 * suppresses an empty string at the end */
300 l = strv_split(s, NEWLINE);
308 if (isempty(l[n - 1]))
309 l[n - 1] = mfree(l[n - 1]);
315 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
316 _cleanup_strv_free_ char **l = NULL;
317 size_t n = 0, allocated = 0;
324 _cleanup_free_ char *word = NULL;
326 r = extract_first_word(&s, &word, separators, flags);
332 if (!GREEDY_REALLOC(l, allocated, n + 2))
335 l[n++] = TAKE_PTR(word);
351 char *strv_join(char **l, const char *separator) {
359 k = strlen(separator);
375 e = stpcpy(e, separator);
385 #if 0 /// UNNEEDED by elogind
387 int strv_push(char ***l, char *value) {
396 /* Increase and check for overflow */
401 c = reallocarray(*l, m, sizeof(char*));
412 int strv_push_pair(char ***l, char *a, char *b) {
421 /* increase and check for overflow */
422 m = n + !!a + !!b + 1;
426 c = reallocarray(*l, m, sizeof(char*));
440 int strv_insert(char ***l, unsigned position, char *value) {
448 position = MIN(position, n);
450 /* increase and check for overflow */
459 for (i = 0; i < position; i++)
462 for (i = position; i < n; i++)
473 int strv_consume(char ***l, char *value) {
476 r = strv_push(l, value);
483 #if 0 /// UNNEEDED by elogind
484 int strv_consume_pair(char ***l, char *a, char *b) {
487 r = strv_push_pair(l, a, b);
497 int strv_consume_prepend(char ***l, char *value) {
500 r = strv_push_prepend(l, value);
507 int strv_extend(char ***l, const char *value) {
517 return strv_consume(l, v);
520 int strv_extend_front(char ***l, const char *value) {
526 /* Like strv_extend(), but prepends rather than appends the new entry */
533 /* Increase and overflow check. */
542 c = reallocarray(*l, m, sizeof(char*));
548 memmove(c+1, c, n * sizeof(char*));
556 char **strv_uniq(char **l) {
559 /* Drops duplicate entries. The first identical string will be
560 * kept, the others dropped */
563 strv_remove(i+1, *i);
568 #if 0 /// UNNEEDED by elogind
569 bool strv_is_uniq(char **l) {
573 if (strv_find(i+1, *i))
580 char **strv_remove(char **l, const char *s) {
588 /* Drops every occurrence of s in the string list, edits
591 for (f = t = l; *f; f++)
601 char **strv_parse_nulstr(const char *s, size_t l) {
602 /* l is the length of the input data, which will be split at NULs into
603 * elements of the resulting strv. Hence, the number of items in the resulting strv
604 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
605 * unless s[l-1] is NUL, in which case the final empty string is not stored in
606 * the resulting strv, and length is equal to the number of NUL bytes.
608 * Note that contrary to a normal nulstr which cannot contain empty strings, because
609 * the input data is terminated by any two consequent NUL bytes, this parser accepts
610 * empty strings in s.
614 unsigned c = 0, i = 0;
620 return new0(char*, 1);
622 for (p = s; p < s + l; p++)
629 v = new0(char*, c+1);
637 e = memchr(p, 0, s + l - p);
639 v[i] = strndup(p, e ? e - p : s + l - p);
658 char **strv_split_nulstr(const char *s) {
663 if (strv_extend(&r, i) < 0) {
669 return strv_new(NULL, NULL);
674 #if 0 /// UNNEEDED by elogind
675 int strv_make_nulstr(char **l, char **p, size_t *q) {
676 /* A valid nulstr with two NULs at the end will be created, but
677 * q will be the length without the two trailing NULs. Thus the output
678 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
679 * and can also be parsed by strv_parse_nulstr as long as the length
680 * is provided separately.
683 size_t n_allocated = 0, n = 0;
684 _cleanup_free_ char *m = NULL;
695 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
698 memcpy(m + n, *i, z + 1);
708 /* make sure there is a second extra NUL at the end of resulting nulstr */
720 bool strv_overlap(char **a, char **b) {
724 if (strv_contains(b, *i))
731 static int str_compare(const void *_a, const void *_b) {
732 const char **a = (const char**) _a, **b = (const char**) _b;
734 return strcmp(*a, *b);
737 char **strv_sort(char **l) {
738 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
742 bool strv_equal(char **a, char **b) {
745 return strv_isempty(b);
750 for ( ; *a || *b; ++a, ++b)
751 if (!streq_ptr(*a, *b))
757 void strv_print(char **l) {
764 #if 0 /// UNNEEDED by elogind
765 int strv_extendf(char ***l, const char *format, ...) {
770 va_start(ap, format);
771 r = vasprintf(&x, format, ap);
777 return strv_consume(l, x);
780 char **strv_reverse(char **l) {
787 for (i = 0; i < n / 2; i++)
788 SWAP_TWO(l[i], l[n-1-i]);
793 char **strv_shell_escape(char **l, const char *bad) {
796 /* Escapes every character in every string in l that is in bad,
797 * edits in-place, does not roll-back on error. */
802 v = shell_escape(*s, bad);
813 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
816 STRV_FOREACH(p, patterns)
817 if (fnmatch(*p, s, flags) == 0)
823 char ***strv_free_free(char ***l) {
835 char **strv_skip(char **l, size_t n) {
848 int strv_extend_n(char ***l, const char *value, size_t n) {
859 /* Adds the value n times to l */
863 nl = reallocarray(*l, k + n + 1, sizeof(char *));
869 for (i = k; i < k + n; i++) {
870 nl[i] = strdup(value);
879 for (j = k; j < i; j++)
886 #if 0 /// UNNEEDED by elogind
887 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
892 /* Like fputs(), but for strv, and with a less stupid argument order */
898 r = fputs_with_space(f, *s, separator, space);