]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/strv.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
12 #include "extract-word.h"
14 #include "memory-util.h"
15 #include "nulstr-util.h"
16 #include "sort-util.h"
17 #include "string-util.h"
20 char *strv_find(char **l
, const char *name
) {
32 char *strv_find_prefix(char **l
, const char *name
) {
38 if (startswith(*i
, name
))
44 char *strv_find_startswith(char **l
, const char *name
) {
49 /* Like strv_find_prefix, but actually returns only the
50 * suffix, not the whole item */
53 e
= startswith(*i
, name
);
61 void strv_clear(char **l
) {
73 char **strv_free(char **l
) {
78 char **strv_free_erase(char **l
) {
87 char **strv_copy(char * const *l
) {
90 k
= r
= new(char*, strv_length(l
) + 1);
95 for (; *l
; k
++, l
++) {
107 size_t strv_length(char * const *l
) {
119 char **strv_new_ap(const char *x
, va_list ap
) {
121 _cleanup_strv_free_
char **a
= NULL
;
125 /* As a special trick we ignore all listed strings that equal
126 * STRV_IGNORE. This is supposed to be used with the
127 * STRV_IFNOTNULL() macro to include possibly NULL strings in
128 * the string list. */
131 n
= x
== STRV_IGNORE
? 0 : 1;
134 while ((s
= va_arg(aq
, const char*))) {
135 if (s
== STRV_IGNORE
)
149 if (x
!= STRV_IGNORE
) {
156 while ((s
= va_arg(ap
, const char*))) {
158 if (s
== STRV_IGNORE
)
174 char **strv_new_internal(const char *x
, ...) {
179 r
= strv_new_ap(x
, ap
);
185 int strv_extend_strv(char ***a
, char **b
, bool filter_duplicates
) {
187 size_t p
, q
, i
= 0, j
;
197 t
= reallocarray(*a
, p
+ q
+ 1, sizeof(char *));
206 if (filter_duplicates
&& strv_contains(t
, *s
))
222 for (j
= 0; j
< i
; j
++)
229 int strv_extend_strv_concat(char ***a
, char **b
, const char *suffix
) {
236 v
= strjoin(*s
, suffix
);
250 char **strv_split_full(const char *s
, const char *separator
, SplitFlags flags
) {
251 const char *word
, *state
;
259 separator
= WHITESPACE
;
261 s
+= strspn(s
, separator
);
263 return new0(char*, 1);
266 _FOREACH_WORD(word
, l
, s
, separator
, flags
, state
)
274 _FOREACH_WORD(word
, l
, s
, separator
, flags
, state
) {
275 r
[i
] = strndup(word
, l
);
288 char **strv_split_newlines(const char *s
) {
294 /* Special version of strv_split() that splits on newlines and
295 * suppresses an empty string at the end */
297 l
= strv_split(s
, NEWLINE
);
305 if (isempty(l
[n
- 1]))
306 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_prefix(char **l
, const char *separator
, const char *prefix
) {
355 k
= strlen(separator
);
356 m
= strlen_ptr(prefix
);
372 e
= stpcpy(e
, separator
);
375 e
= stpcpy(e
, prefix
);
385 int strv_push(char ***l
, char *value
) {
394 /* Increase and check for overflow */
399 c
= reallocarray(*l
, m
, sizeof(char*));
410 int strv_push_pair(char ***l
, char *a
, char *b
) {
419 /* increase and check for overflow */
420 m
= n
+ !!a
+ !!b
+ 1;
424 c
= reallocarray(*l
, m
, sizeof(char*));
438 int strv_insert(char ***l
, size_t position
, char *value
) {
446 position
= MIN(position
, n
);
448 /* increase and check for overflow */
457 for (i
= 0; i
< position
; i
++)
460 for (i
= position
; i
< n
; i
++)
471 int strv_consume(char ***l
, char *value
) {
474 r
= strv_push(l
, value
);
481 int strv_consume_pair(char ***l
, char *a
, char *b
) {
484 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 bool strv_is_uniq(char **l
) {
568 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
);
668 int strv_make_nulstr(char **l
, char **p
, size_t *q
) {
669 /* A valid nulstr with two NULs at the end will be created, but
670 * q will be the length without the two trailing NULs. Thus the output
671 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
672 * and can also be parsed by strv_parse_nulstr as long as the length
673 * is provided separately.
676 size_t n_allocated
= 0, n
= 0;
677 _cleanup_free_
char *m
= NULL
;
688 if (!GREEDY_REALLOC(m
, n_allocated
, n
+ z
+ 2))
691 memcpy(m
+ n
, *i
, z
+ 1);
701 /* make sure there is a second extra NUL at the end of resulting nulstr */
713 bool strv_overlap(char **a
, char **b
) {
717 if (strv_contains(b
, *i
))
723 static int str_compare(char * const *a
, char * const *b
) {
724 return strcmp(*a
, *b
);
727 char **strv_sort(char **l
) {
728 typesafe_qsort(l
, strv_length(l
), str_compare
);
732 bool strv_equal(char **a
, char **b
) {
735 return strv_isempty(b
);
740 for ( ; *a
|| *b
; ++a
, ++b
)
741 if (!streq_ptr(*a
, *b
))
747 void strv_print(char **l
) {
754 int strv_extendf(char ***l
, const char *format
, ...) {
759 va_start(ap
, format
);
760 r
= vasprintf(&x
, format
, ap
);
766 return strv_consume(l
, x
);
769 char **strv_reverse(char **l
) {
776 for (i
= 0; i
< n
/ 2; i
++)
777 SWAP_TWO(l
[i
], l
[n
-1-i
]);
782 char **strv_shell_escape(char **l
, const char *bad
) {
785 /* Escapes every character in every string in l that is in bad,
786 * edits in-place, does not roll-back on error. */
791 v
= shell_escape(*s
, bad
);
802 bool strv_fnmatch(char* const* patterns
, const char *s
, int flags
) {
805 STRV_FOREACH(p
, patterns
)
806 if (fnmatch(*p
, s
, flags
) == 0)
812 char ***strv_free_free(char ***l
) {
824 char **strv_skip(char **l
, size_t n
) {
836 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
847 /* Adds the value n times to l */
851 nl
= reallocarray(*l
, k
+ n
+ 1, sizeof(char *));
857 for (i
= k
; i
< k
+ n
; i
++) {
858 nl
[i
] = strdup(value
);
867 for (j
= k
; j
< i
; j
++)
874 int fputstrv(FILE *f
, char **l
, const char *separator
, bool *space
) {
879 /* Like fputs(), but for strv, and with a less stupid argument order */
885 r
= fputs_with_space(f
, *s
, separator
, space
);