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 void strv_free_many(char ***strvs
, size_t n
) {
92 assert(strvs
|| n
== 0);
94 FOREACH_ARRAY (i
, strvs
, n
)
100 char** strv_copy_n(char * const *l
, size_t m
) {
101 _cleanup_strv_free_
char **result
= NULL
;
104 result
= new(char*, MIN(strv_length(l
), m
) + 1);
123 return TAKE_PTR(result
);
126 size_t strv_length(char * const *l
) {
135 char** strv_new_ap(const char *x
, va_list ap
) {
136 _cleanup_strv_free_
char **a
= NULL
;
140 /* As a special trick we ignore all listed strings that equal
141 * STRV_IGNORE. This is supposed to be used with the
142 * STRV_IFNOTNULL() macro to include possibly NULL strings in
143 * the string list. */
146 for (const char *s
= x
; s
; s
= va_arg(aq
, const char*)) {
147 if (s
== STRV_IGNORE
)
158 for (const char *s
= x
; s
; s
= va_arg(ap
, const char*)) {
159 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 * const *b
, bool filter_duplicates
) {
197 if (p
>= SIZE_MAX
- q
)
200 t
= reallocarray(*a
, GREEDY_ALLOC_ROUND_UP(p
+ q
+ 1), sizeof(char *));
208 if (filter_duplicates
&& strv_contains(t
, *s
))
224 free_many_charp(t
+ p
, i
);
229 int strv_extend_strv_concat(char ***a
, char * const *b
, const char *suffix
) {
235 v
= strjoin(*s
, suffix
);
249 int strv_split_newlines_full(char ***ret
, const char *s
, ExtractFlags flags
) {
250 _cleanup_strv_free_
char **l
= NULL
;
256 /* Special version of strv_split_full() that splits on newlines and
257 * suppresses an empty string at the end. */
259 r
= strv_split_full(&l
, s
, NEWLINE
, flags
);
264 if (n
> 0 && isempty(l
[n
- 1])) {
265 l
[n
- 1] = mfree(l
[n
- 1]);
273 int strv_split_full(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
274 _cleanup_strv_free_
char **l
= NULL
;
282 _cleanup_free_
char *word
= NULL
;
284 r
= extract_first_word(&s
, &word
, separators
, flags
);
290 if (!GREEDY_REALLOC(l
, n
+ 2))
293 l
[n
++] = TAKE_PTR(word
);
308 int strv_split_and_extend_full(char ***t
, const char *s
, const char *separators
, bool filter_duplicates
, ExtractFlags flags
) {
309 _cleanup_strv_free_
char **l
= NULL
;
315 r
= strv_split_full(&l
, s
, separators
, flags
);
319 r
= strv_extend_strv(t
, l
, filter_duplicates
);
323 return (int) strv_length(*t
);
326 int strv_split_colon_pairs(char ***t
, const char *s
) {
327 _cleanup_strv_free_
char **l
= NULL
;
335 _cleanup_free_
char *first
= NULL
, *second
= NULL
, *tuple
= NULL
, *second_or_empty
= NULL
;
337 r
= extract_first_word(&s
, &tuple
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
343 const char *p
= tuple
;
344 r
= extract_many_words(&p
, ":", EXTRACT_CUNESCAPE
|EXTRACT_UNESCAPE_SEPARATORS
,
345 &first
, &second
, NULL
);
350 /* Enforce that at most 2 colon-separated words are contained in each group */
354 second_or_empty
= strdup(strempty(second
));
355 if (!second_or_empty
)
358 if (!GREEDY_REALLOC(l
, n
+ 3))
361 l
[n
++] = TAKE_PTR(first
);
362 l
[n
++] = TAKE_PTR(second_or_empty
);
378 char* strv_join_full(char * const *l
, const char *separator
, const char *prefix
, bool escape_separator
) {
385 k
= strlen(separator
);
386 m
= strlen_ptr(prefix
);
388 if (escape_separator
) /* If the separator was multi-char, we wouldn't know how to escape it. */
396 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
398 n
+= m
+ strlen(*s
) * (1 + needs_escaping
);
408 e
= stpcpy(e
, separator
);
411 e
= stpcpy(e
, prefix
);
413 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
416 for (size_t i
= 0; (*s
)[i
]; i
++) {
417 if ((*s
)[i
] == *separator
)
430 int strv_push_with_size(char ***l
, size_t *n
, char *value
) {
431 /* n is a pointer to a variable to store the size of l.
432 * If not given (i.e. n is NULL or *n is SIZE_MAX), size will be calculated using strv_length().
433 * If n is not NULL, the size after the push will be returned.
434 * If value is empty, no action is taken and *n is not set. */
439 size_t size
= n
? *n
: SIZE_MAX
;
440 if (size
== SIZE_MAX
)
441 size
= strv_length(*l
);
443 /* Check for overflow */
444 if (size
> SIZE_MAX
-2)
447 char **c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(size
+ 2), sizeof(char*));
460 int strv_push_pair(char ***l
, char *a
, char *b
) {
469 /* Check for overflow */
473 /* increase and check for overflow */
474 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ !!a
+ !!b
+ 1), sizeof(char*));
488 int strv_insert(char ***l
, size_t position
, char *value
) {
496 position
= MIN(position
, n
);
498 /* increase and check for overflow */
507 for (size_t i
= 0; i
< position
; i
++)
510 for (size_t i
= position
; i
< n
; i
++)
514 return free_and_replace(*l
, c
);
517 int strv_consume_with_size(char ***l
, size_t *n
, char *value
) {
520 r
= strv_push_with_size(l
, n
, value
);
527 int strv_consume_pair(char ***l
, char *a
, char *b
) {
530 r
= strv_push_pair(l
, a
, b
);
539 int strv_consume_prepend(char ***l
, char *value
) {
542 r
= strv_push_prepend(l
, value
);
549 int strv_prepend(char ***l
, const char *value
) {
559 return strv_consume_prepend(l
, v
);
562 int strv_extend_with_size(char ***l
, size_t *n
, const char *value
) {
572 return strv_consume_with_size(l
, n
, v
);
575 int strv_extend_front(char ***l
, const char *value
) {
581 /* Like strv_extend(), but prepends rather than appends the new entry */
588 /* Increase and overflow check. */
597 c
= reallocarray(*l
, m
, sizeof(char*));
603 memmove(c
+1, c
, n
* sizeof(char*));
611 char** strv_uniq(char **l
) {
612 /* Drops duplicate entries. The first identical string will be
613 * kept, the others dropped */
616 strv_remove(i
+1, *i
);
621 bool strv_is_uniq(char * const *l
) {
623 if (strv_contains(i
+1, *i
))
629 char** strv_remove(char **l
, const char *s
) {
637 /* Drops every occurrence of s in the string list, edits
640 for (f
= t
= l
; *f
; f
++)
650 bool strv_overlap(char * const *a
, char * const *b
) {
652 if (strv_contains(b
, *i
))
658 static int str_compare(char * const *a
, char * const *b
) {
659 return strcmp(*a
, *b
);
662 char** strv_sort(char **l
) {
663 typesafe_qsort(l
, strv_length(l
), str_compare
);
667 int strv_compare(char * const *a
, char * const *b
) {
670 if (strv_isempty(a
)) {
680 for ( ; *a
|| *b
; ++a
, ++b
) {
681 r
= strcmp_ptr(*a
, *b
);
689 void strv_print_full(char * const *l
, const char *prefix
) {
691 printf("%s%s\n", strempty(prefix
), *s
);
694 int strv_extendf(char ***l
, const char *format
, ...) {
699 va_start(ap
, format
);
700 r
= vasprintf(&x
, format
, ap
);
706 return strv_consume(l
, x
);
709 char** strv_reverse(char **l
) {
716 for (size_t i
= 0; i
< n
/ 2; i
++)
717 SWAP_TWO(l
[i
], l
[n
-1-i
]);
722 char** strv_shell_escape(char **l
, const char *bad
) {
723 /* Escapes every character in every string in l that is in bad,
724 * edits in-place, does not roll-back on error. */
729 v
= shell_escape(*s
, bad
);
733 free_and_replace(*s
, v
);
739 bool strv_fnmatch_full(
740 char* const* patterns
,
743 size_t *ret_matched_pos
) {
748 for (size_t i
= 0; patterns
[i
]; i
++)
749 /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
750 * process the pattern for some reason we'll consider this equivalent to non-matching. */
751 if (fnmatch(patterns
[i
], s
, flags
) == 0) {
753 *ret_matched_pos
= i
;
758 *ret_matched_pos
= SIZE_MAX
;
763 char** strv_skip(char **l
, size_t n
) {
775 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
786 /* Adds the value n times to l */
789 if (n
>= SIZE_MAX
- k
)
792 nl
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(k
+ n
+ 1), sizeof(char *));
798 for (i
= k
; i
< k
+ n
; i
++) {
799 nl
[i
] = strdup(value
);
808 for (size_t j
= k
; j
< i
; j
++)
815 int strv_extend_assignment(char ***l
, const char *lhs
, const char *rhs
) {
821 if (!rhs
) /* value is optional, in which case we suppress the field */
824 j
= strjoin(lhs
, "=", rhs
);
828 return strv_consume(l
, j
);
831 int fputstrv(FILE *f
, char * const *l
, const char *separator
, bool *space
) {
835 /* Like fputs(), but for strv, and with a less stupid argument order */
841 r
= fputs_with_space(f
, *s
, separator
, space
);
849 static int string_strv_hashmap_put_internal(Hashmap
*h
, const char *key
, const char *value
) {
853 l
= hashmap_get(h
, key
);
855 /* A list for this key already exists, let's append to it if it is not listed yet */
856 if (strv_contains(l
, value
))
859 r
= strv_extend(&l
, value
);
863 assert_se(hashmap_update(h
, key
, l
) >= 0);
865 /* No list for this key exists yet, create one */
866 _cleanup_strv_free_
char **l2
= NULL
;
867 _cleanup_free_
char *t
= NULL
;
873 r
= strv_extend(&l2
, value
);
877 r
= hashmap_put(h
, t
, l2
);
887 int _string_strv_hashmap_put(Hashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
890 r
= _hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
894 return string_strv_hashmap_put_internal(*h
, key
, value
);
897 int _string_strv_ordered_hashmap_put(OrderedHashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
900 r
= _ordered_hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
904 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h
), key
, value
);
907 DEFINE_HASH_OPS_FULL(string_strv_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char*, strv_free
);
909 char* strv_endswith(const char *s
, char **l
) {
911 char *e
= endswith(s
, *i
);