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 size_t strv_length(char * const *l) {
122 char **strv_new_ap(const char *x, va_list ap) {
124 _cleanup_strv_free_ char **a = NULL;
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)
177 char **strv_new(const char *x, ...) {
182 r = strv_new_ap(x, ap);
188 #if 0 /// UNNEEDED by elogind
189 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
191 size_t p, q, i = 0, j;
201 t = reallocarray(*a, p + q + 1, sizeof(char *));
210 if (filter_duplicates && strv_contains(t, *s))
226 for (j = 0; j < i; j++)
233 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
240 v = strappend(*s, suffix);
255 char **strv_split(const char *s, const char *separator) {
256 const char *word, *state;
264 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
272 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
273 r[i] = strndup(word, l);
286 #if 0 /// UNNEEDED by elogind
287 char **strv_split_newlines(const char *s) {
293 /* Special version of strv_split() that splits on newlines and
294 * suppresses an empty string at the end */
296 l = strv_split(s, NEWLINE);
304 if (isempty(l[n - 1]))
305 l[n - 1] = mfree(l[n - 1]);
311 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
312 _cleanup_strv_free_ char **l = NULL;
313 size_t n = 0, allocated = 0;
320 _cleanup_free_ char *word = NULL;
322 r = extract_first_word(&s, &word, separators, flags);
328 if (!GREEDY_REALLOC(l, allocated, n + 2))
331 l[n++] = TAKE_PTR(word);
347 char *strv_join(char **l, const char *separator) {
355 k = strlen(separator);
371 e = stpcpy(e, separator);
381 #if 0 /// UNNEEDED by elogind
383 int strv_push(char ***l, char *value) {
392 /* Increase and check for overflow */
397 c = reallocarray(*l, m, sizeof(char*));
408 int strv_push_pair(char ***l, char *a, char *b) {
417 /* increase and check for overflow */
418 m = n + !!a + !!b + 1;
422 c = reallocarray(*l, m, sizeof(char*));
436 int strv_insert(char ***l, size_t position, char *value) {
444 position = MIN(position, n);
446 /* increase and check for overflow */
455 for (i = 0; i < position; i++)
458 for (i = position; i < n; i++)
469 int strv_consume(char ***l, char *value) {
472 r = strv_push(l, value);
479 #if 0 /// UNNEEDED by elogind
480 int strv_consume_pair(char ***l, char *a, char *b) {
483 r = strv_push_pair(l, a, b);
493 int strv_consume_prepend(char ***l, char *value) {
496 r = strv_push_prepend(l, value);
503 int strv_extend(char ***l, const char *value) {
513 return strv_consume(l, v);
516 int strv_extend_front(char ***l, const char *value) {
522 /* Like strv_extend(), but prepends rather than appends the new entry */
529 /* Increase and overflow check. */
538 c = reallocarray(*l, m, sizeof(char*));
544 memmove(c+1, c, n * sizeof(char*));
552 char **strv_uniq(char **l) {
555 /* Drops duplicate entries. The first identical string will be
556 * kept, the others dropped */
559 strv_remove(i+1, *i);
564 #if 0 /// UNNEEDED by elogind
565 bool strv_is_uniq(char **l) {
569 if (strv_find(i+1, *i))
576 char **strv_remove(char **l, const char *s) {
584 /* Drops every occurrence of s in the string list, edits
587 for (f = t = l; *f; f++)
597 char **strv_parse_nulstr(const char *s, size_t l) {
598 /* l is the length of the input data, which will be split at NULs into
599 * elements of the resulting strv. Hence, the number of items in the resulting strv
600 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
601 * unless s[l-1] is NUL, in which case the final empty string is not stored in
602 * the resulting strv, and length is equal to the number of NUL bytes.
604 * Note that contrary to a normal nulstr which cannot contain empty strings, because
605 * the input data is terminated by any two consequent NUL bytes, this parser accepts
606 * empty strings in s.
616 return new0(char*, 1);
618 for (p = s; p < s + l; p++)
625 v = new0(char*, c+1);
633 e = memchr(p, 0, s + l - p);
635 v[i] = strndup(p, e ? e - p : s + l - p);
654 char **strv_split_nulstr(const char *s) {
659 if (strv_extend(&r, i) < 0) {
665 return strv_new(NULL, NULL);
670 #if 0 /// UNNEEDED by elogind
671 int strv_make_nulstr(char **l, char **p, size_t *q) {
672 /* A valid nulstr with two NULs at the end will be created, but
673 * q will be the length without the two trailing NULs. Thus the output
674 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
675 * and can also be parsed by strv_parse_nulstr as long as the length
676 * is provided separately.
679 size_t n_allocated = 0, n = 0;
680 _cleanup_free_ char *m = NULL;
691 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
694 memcpy(m + n, *i, z + 1);
704 /* make sure there is a second extra NUL at the end of resulting nulstr */
716 bool strv_overlap(char **a, char **b) {
720 if (strv_contains(b, *i))
727 static int str_compare(const void *_a, const void *_b) {
728 const char **a = (const char**) _a, **b = (const char**) _b;
730 return strcmp(*a, *b);
733 char **strv_sort(char **l) {
734 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
738 bool strv_equal(char **a, char **b) {
741 return strv_isempty(b);
746 for ( ; *a || *b; ++a, ++b)
747 if (!streq_ptr(*a, *b))
753 void strv_print(char **l) {
760 #if 0 /// UNNEEDED by elogind
761 int strv_extendf(char ***l, const char *format, ...) {
766 va_start(ap, format);
767 r = vasprintf(&x, format, ap);
773 return strv_consume(l, x);
776 char **strv_reverse(char **l) {
783 for (i = 0; i < n / 2; i++)
784 SWAP_TWO(l[i], l[n-1-i]);
789 char **strv_shell_escape(char **l, const char *bad) {
792 /* Escapes every character in every string in l that is in bad,
793 * edits in-place, does not roll-back on error. */
798 v = shell_escape(*s, bad);
809 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
812 STRV_FOREACH(p, patterns)
813 if (fnmatch(*p, s, flags) == 0)
819 char ***strv_free_free(char ***l) {
831 char **strv_skip(char **l, size_t n) {
844 int strv_extend_n(char ***l, const char *value, size_t n) {
855 /* Adds the value n times to l */
859 nl = reallocarray(*l, k + n + 1, sizeof(char *));
865 for (i = k; i < k + n; i++) {
866 nl[i] = strdup(value);
875 for (j = k; j < i; j++)
882 #if 0 /// UNNEEDED by elogind
883 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
888 /* Like fputs(), but for strv, and with a less stupid argument order */
894 r = fputs_with_space(f, *s, separator, space);