1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #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 * const *l
, const char *name
) {
30 char* strv_find_case(char * const *l
, const char *name
) {
34 if (strcaseeq(*i
, name
))
40 char* strv_find_prefix(char * const *l
, const char *name
) {
44 if (startswith(*i
, name
))
50 char* strv_find_startswith(char * const *l
, const char *name
) {
53 /* Like strv_find_prefix, but actually returns only the
54 * suffix, not the whole item */
59 e
= startswith(*i
, name
);
67 char* strv_find_first_field(char * const *needles
, char * const *haystack
) {
68 STRV_FOREACH(k
, needles
) {
69 char *value
= strv_env_pairs_get((char **)haystack
, *k
);
77 char** strv_free(char **l
) {
84 char** strv_free_erase(char **l
) {
91 char** strv_copy_n(char * const *l
, size_t m
) {
92 _cleanup_strv_free_
char **result
= NULL
;
95 result
= new(char*, MIN(strv_length(l
), m
) + 1);
114 return TAKE_PTR(result
);
117 int strv_copy_unless_empty(char * const *l
, char ***ret
) {
120 if (strv_isempty(l
)) {
125 char **copy
= strv_copy(l
);
129 *ret
= TAKE_PTR(copy
);
133 size_t strv_length(char * const *l
) {
142 char** strv_new_ap(const char *x
, va_list ap
) {
143 _cleanup_strv_free_
char **a
= NULL
;
147 /* As a special trick we ignore all listed strings that equal
148 * STRV_IGNORE. This is supposed to be used with the
149 * STRV_IFNOTNULL() macro to include possibly NULL strings in
150 * the string list. */
153 for (const char *s
= x
; s
; s
= va_arg(aq
, const char*)) {
154 if (s
== STRV_IGNORE
)
165 for (const char *s
= x
; s
; s
= va_arg(ap
, const char*)) {
166 if (s
== STRV_IGNORE
)
181 char** strv_new_internal(const char *x
, ...) {
186 r
= strv_new_ap(x
, ap
);
192 int strv_extend_strv(char ***a
, char * const *b
, bool filter_duplicates
) {
204 if (p
>= SIZE_MAX
- q
)
207 t
= reallocarray(*a
, GREEDY_ALLOC_ROUND_UP(p
+ q
+ 1), sizeof(char *));
215 if (filter_duplicates
&& strv_contains(t
, *s
))
231 for (size_t j
= 0; j
< i
; j
++)
238 int strv_extend_strv_concat(char ***a
, char * const *b
, const char *suffix
) {
244 v
= strjoin(*s
, suffix
);
258 int strv_split_newlines_full(char ***ret
, const char *s
, ExtractFlags flags
) {
259 _cleanup_strv_free_
char **l
= NULL
;
265 /* Special version of strv_split_full() that splits on newlines and
266 * suppresses an empty string at the end. */
268 r
= strv_split_full(&l
, s
, NEWLINE
, flags
);
273 if (n
> 0 && isempty(l
[n
- 1])) {
274 l
[n
- 1] = mfree(l
[n
- 1]);
282 int strv_split_full(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
283 _cleanup_strv_free_
char **l
= NULL
;
291 _cleanup_free_
char *word
= NULL
;
293 r
= extract_first_word(&s
, &word
, separators
, flags
);
299 if (!GREEDY_REALLOC(l
, n
+ 2))
302 l
[n
++] = TAKE_PTR(word
);
317 int strv_split_and_extend_full(char ***t
, const char *s
, const char *separators
, bool filter_duplicates
, ExtractFlags flags
) {
318 _cleanup_strv_free_
char **l
= NULL
;
324 r
= strv_split_full(&l
, s
, separators
, flags
);
328 r
= strv_extend_strv(t
, l
, filter_duplicates
);
332 return (int) strv_length(*t
);
335 int strv_split_colon_pairs(char ***t
, const char *s
) {
336 _cleanup_strv_free_
char **l
= NULL
;
344 _cleanup_free_
char *first
= NULL
, *second
= NULL
, *tuple
= NULL
, *second_or_empty
= NULL
;
346 r
= extract_first_word(&s
, &tuple
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
352 const char *p
= tuple
;
353 r
= extract_many_words(&p
, ":", EXTRACT_CUNESCAPE
|EXTRACT_UNESCAPE_SEPARATORS
,
354 &first
, &second
, NULL
);
359 /* Enforce that at most 2 colon-separated words are contained in each group */
363 second_or_empty
= strdup(strempty(second
));
364 if (!second_or_empty
)
367 if (!GREEDY_REALLOC(l
, n
+ 3))
370 l
[n
++] = TAKE_PTR(first
);
371 l
[n
++] = TAKE_PTR(second_or_empty
);
387 char* strv_join_full(char * const *l
, const char *separator
, const char *prefix
, bool escape_separator
) {
394 k
= strlen(separator
);
395 m
= strlen_ptr(prefix
);
397 if (escape_separator
) /* If the separator was multi-char, we wouldn't know how to escape it. */
405 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
407 n
+= m
+ strlen(*s
) * (1 + needs_escaping
);
417 e
= stpcpy(e
, separator
);
420 e
= stpcpy(e
, prefix
);
422 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
425 for (size_t i
= 0; (*s
)[i
]; i
++) {
426 if ((*s
)[i
] == *separator
)
439 int strv_push_with_size(char ***l
, size_t *n
, char *value
) {
440 /* n is a pointer to a variable to store the size of l.
441 * If not given (i.e. n is NULL or *n is SIZE_MAX), size will be calculated using strv_length().
442 * If n is not NULL, the size after the push will be returned.
443 * If value is empty, no action is taken and *n is not set. */
448 size_t size
= n
? *n
: SIZE_MAX
;
449 if (size
== SIZE_MAX
)
450 size
= strv_length(*l
);
452 /* Check for overflow */
453 if (size
> SIZE_MAX
-2)
456 char **c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(size
+ 2), sizeof(char*));
469 int strv_push_pair(char ***l
, char *a
, char *b
) {
478 /* Check for overflow */
482 /* increase and check for overflow */
483 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ !!a
+ !!b
+ 1), sizeof(char*));
497 int strv_insert(char ***l
, size_t position
, char *value
) {
505 position
= MIN(position
, n
);
507 /* increase and check for overflow */
516 for (size_t i
= 0; i
< position
; i
++)
519 for (size_t i
= position
; i
< n
; i
++)
523 return free_and_replace(*l
, c
);
526 int strv_consume_with_size(char ***l
, size_t *n
, char *value
) {
529 r
= strv_push_with_size(l
, n
, value
);
536 int strv_consume_pair(char ***l
, char *a
, char *b
) {
539 r
= strv_push_pair(l
, a
, b
);
548 int strv_consume_prepend(char ***l
, char *value
) {
551 r
= strv_push_prepend(l
, value
);
558 int strv_prepend(char ***l
, const char *value
) {
568 return strv_consume_prepend(l
, v
);
571 int strv_extend_with_size(char ***l
, size_t *n
, const char *value
) {
581 return strv_consume_with_size(l
, n
, v
);
584 int strv_extend_front(char ***l
, const char *value
) {
590 /* Like strv_extend(), but prepends rather than appends the new entry */
597 /* Increase and overflow check. */
606 c
= reallocarray(*l
, m
, sizeof(char*));
612 memmove(c
+1, c
, n
* sizeof(char*));
620 char** strv_uniq(char **l
) {
621 /* Drops duplicate entries. The first identical string will be
622 * kept, the others dropped */
625 strv_remove(i
+1, *i
);
630 bool strv_is_uniq(char * const *l
) {
632 if (strv_contains(i
+1, *i
))
638 char** strv_remove(char **l
, const char *s
) {
646 /* Drops every occurrence of s in the string list, edits
649 for (f
= t
= l
; *f
; f
++)
659 bool strv_overlap(char * const *a
, char * const *b
) {
661 if (strv_contains(b
, *i
))
667 static int str_compare(char * const *a
, char * const *b
) {
668 return strcmp(*a
, *b
);
671 char** strv_sort(char **l
) {
672 typesafe_qsort(l
, strv_length(l
), str_compare
);
676 int strv_compare(char * const *a
, char * const *b
) {
679 if (strv_isempty(a
)) {
689 for ( ; *a
|| *b
; ++a
, ++b
) {
690 r
= strcmp_ptr(*a
, *b
);
698 void strv_print_full(char * const *l
, const char *prefix
) {
700 printf("%s%s\n", strempty(prefix
), *s
);
703 int strv_extendf(char ***l
, const char *format
, ...) {
708 va_start(ap
, format
);
709 r
= vasprintf(&x
, format
, ap
);
715 return strv_consume(l
, x
);
718 char** strv_reverse(char **l
) {
725 for (size_t i
= 0; i
< n
/ 2; i
++)
726 SWAP_TWO(l
[i
], l
[n
-1-i
]);
731 char** strv_shell_escape(char **l
, const char *bad
) {
732 /* Escapes every character in every string in l that is in bad,
733 * edits in-place, does not roll-back on error. */
738 v
= shell_escape(*s
, bad
);
742 free_and_replace(*s
, v
);
748 bool strv_fnmatch_full(
749 char* const* patterns
,
752 size_t *ret_matched_pos
) {
757 for (size_t i
= 0; patterns
[i
]; i
++)
758 /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
759 * process the pattern for some reason we'll consider this equivalent to non-matching. */
760 if (fnmatch(patterns
[i
], s
, flags
) == 0) {
762 *ret_matched_pos
= i
;
767 *ret_matched_pos
= SIZE_MAX
;
772 char** strv_skip(char **l
, size_t n
) {
784 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
795 /* Adds the value n times to l */
798 if (n
>= SIZE_MAX
- k
)
801 nl
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(k
+ n
+ 1), sizeof(char *));
807 for (i
= k
; i
< k
+ n
; i
++) {
808 nl
[i
] = strdup(value
);
817 for (size_t j
= k
; j
< i
; j
++)
824 int strv_extend_assignment(char ***l
, const char *lhs
, const char *rhs
) {
830 if (!rhs
) /* value is optional, in which case we suppress the field */
833 j
= strjoin(lhs
, "=", rhs
);
837 return strv_consume(l
, j
);
840 int fputstrv(FILE *f
, char * const *l
, const char *separator
, bool *space
) {
844 /* Like fputs(), but for strv, and with a less stupid argument order */
850 r
= fputs_with_space(f
, *s
, separator
, space
);
858 static int string_strv_hashmap_put_internal(Hashmap
*h
, const char *key
, const char *value
) {
862 l
= hashmap_get(h
, key
);
864 /* A list for this key already exists, let's append to it if it is not listed yet */
865 if (strv_contains(l
, value
))
868 r
= strv_extend(&l
, value
);
872 assert_se(hashmap_update(h
, key
, l
) >= 0);
874 /* No list for this key exists yet, create one */
875 _cleanup_strv_free_
char **l2
= NULL
;
876 _cleanup_free_
char *t
= NULL
;
882 r
= strv_extend(&l2
, value
);
886 r
= hashmap_put(h
, t
, l2
);
896 int _string_strv_hashmap_put(Hashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
899 r
= _hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
903 return string_strv_hashmap_put_internal(*h
, key
, value
);
906 int _string_strv_ordered_hashmap_put(OrderedHashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
909 r
= _ordered_hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
913 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h
), key
, value
);
916 DEFINE_HASH_OPS_FULL(string_strv_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char*, strv_free
);