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 int strv_copy_unless_empty(char * const *l
, char ***ret
) {
129 if (strv_isempty(l
)) {
134 char **copy
= strv_copy(l
);
138 *ret
= TAKE_PTR(copy
);
142 size_t strv_length(char * const *l
) {
151 char** strv_new_ap(const char *x
, va_list ap
) {
152 _cleanup_strv_free_
char **a
= NULL
;
156 /* As a special trick we ignore all listed strings that equal
157 * STRV_IGNORE. This is supposed to be used with the
158 * STRV_IFNOTNULL() macro to include possibly NULL strings in
159 * the string list. */
162 for (const char *s
= x
; s
; s
= va_arg(aq
, const char*)) {
163 if (s
== STRV_IGNORE
)
174 for (const char *s
= x
; s
; s
= va_arg(ap
, const char*)) {
175 if (s
== STRV_IGNORE
)
190 char** strv_new_internal(const char *x
, ...) {
195 r
= strv_new_ap(x
, ap
);
201 int strv_extend_strv(char ***a
, char * const *b
, bool filter_duplicates
) {
213 if (p
>= SIZE_MAX
- q
)
216 t
= reallocarray(*a
, GREEDY_ALLOC_ROUND_UP(p
+ q
+ 1), sizeof(char *));
224 if (filter_duplicates
&& strv_contains(t
, *s
))
240 free_many_charp(t
+ p
, i
);
245 int strv_extend_strv_concat(char ***a
, char * const *b
, const char *suffix
) {
251 v
= strjoin(*s
, suffix
);
265 int strv_split_newlines_full(char ***ret
, const char *s
, ExtractFlags flags
) {
266 _cleanup_strv_free_
char **l
= NULL
;
272 /* Special version of strv_split_full() that splits on newlines and
273 * suppresses an empty string at the end. */
275 r
= strv_split_full(&l
, s
, NEWLINE
, flags
);
280 if (n
> 0 && isempty(l
[n
- 1])) {
281 l
[n
- 1] = mfree(l
[n
- 1]);
289 int strv_split_full(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
290 _cleanup_strv_free_
char **l
= NULL
;
298 _cleanup_free_
char *word
= NULL
;
300 r
= extract_first_word(&s
, &word
, separators
, flags
);
306 if (!GREEDY_REALLOC(l
, n
+ 2))
309 l
[n
++] = TAKE_PTR(word
);
324 int strv_split_and_extend_full(char ***t
, const char *s
, const char *separators
, bool filter_duplicates
, ExtractFlags flags
) {
325 _cleanup_strv_free_
char **l
= NULL
;
331 r
= strv_split_full(&l
, s
, separators
, flags
);
335 r
= strv_extend_strv(t
, l
, filter_duplicates
);
339 return (int) strv_length(*t
);
342 int strv_split_colon_pairs(char ***t
, const char *s
) {
343 _cleanup_strv_free_
char **l
= NULL
;
351 _cleanup_free_
char *first
= NULL
, *second
= NULL
, *tuple
= NULL
, *second_or_empty
= NULL
;
353 r
= extract_first_word(&s
, &tuple
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
359 const char *p
= tuple
;
360 r
= extract_many_words(&p
, ":", EXTRACT_CUNESCAPE
|EXTRACT_UNESCAPE_SEPARATORS
,
361 &first
, &second
, NULL
);
366 /* Enforce that at most 2 colon-separated words are contained in each group */
370 second_or_empty
= strdup(strempty(second
));
371 if (!second_or_empty
)
374 if (!GREEDY_REALLOC(l
, n
+ 3))
377 l
[n
++] = TAKE_PTR(first
);
378 l
[n
++] = TAKE_PTR(second_or_empty
);
394 char* strv_join_full(char * const *l
, const char *separator
, const char *prefix
, bool escape_separator
) {
401 k
= strlen(separator
);
402 m
= strlen_ptr(prefix
);
404 if (escape_separator
) /* If the separator was multi-char, we wouldn't know how to escape it. */
412 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
414 n
+= m
+ strlen(*s
) * (1 + needs_escaping
);
424 e
= stpcpy(e
, separator
);
427 e
= stpcpy(e
, prefix
);
429 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
432 for (size_t i
= 0; (*s
)[i
]; i
++) {
433 if ((*s
)[i
] == *separator
)
446 int strv_push_with_size(char ***l
, size_t *n
, char *value
) {
447 /* n is a pointer to a variable to store the size of l.
448 * If not given (i.e. n is NULL or *n is SIZE_MAX), size will be calculated using strv_length().
449 * If n is not NULL, the size after the push will be returned.
450 * If value is empty, no action is taken and *n is not set. */
455 size_t size
= n
? *n
: SIZE_MAX
;
456 if (size
== SIZE_MAX
)
457 size
= strv_length(*l
);
459 /* Check for overflow */
460 if (size
> SIZE_MAX
-2)
463 char **c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(size
+ 2), sizeof(char*));
476 int strv_push_pair(char ***l
, char *a
, char *b
) {
485 /* Check for overflow */
489 /* increase and check for overflow */
490 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ !!a
+ !!b
+ 1), sizeof(char*));
504 int strv_insert(char ***l
, size_t position
, char *value
) {
512 position
= MIN(position
, n
);
514 /* increase and check for overflow */
523 for (size_t i
= 0; i
< position
; i
++)
526 for (size_t i
= position
; i
< n
; i
++)
530 return free_and_replace(*l
, c
);
533 int strv_consume_with_size(char ***l
, size_t *n
, char *value
) {
536 r
= strv_push_with_size(l
, n
, value
);
543 int strv_consume_pair(char ***l
, char *a
, char *b
) {
546 r
= strv_push_pair(l
, a
, b
);
555 int strv_consume_prepend(char ***l
, char *value
) {
558 r
= strv_push_prepend(l
, value
);
565 int strv_prepend(char ***l
, const char *value
) {
575 return strv_consume_prepend(l
, v
);
578 int strv_extend_with_size(char ***l
, size_t *n
, const char *value
) {
588 return strv_consume_with_size(l
, n
, v
);
591 int strv_extend_front(char ***l
, const char *value
) {
597 /* Like strv_extend(), but prepends rather than appends the new entry */
604 /* Increase and overflow check. */
613 c
= reallocarray(*l
, m
, sizeof(char*));
619 memmove(c
+1, c
, n
* sizeof(char*));
627 char** strv_uniq(char **l
) {
628 /* Drops duplicate entries. The first identical string will be
629 * kept, the others dropped */
632 strv_remove(i
+1, *i
);
637 bool strv_is_uniq(char * const *l
) {
639 if (strv_contains(i
+1, *i
))
645 char** strv_remove(char **l
, const char *s
) {
653 /* Drops every occurrence of s in the string list, edits
656 for (f
= t
= l
; *f
; f
++)
666 bool strv_overlap(char * const *a
, char * const *b
) {
668 if (strv_contains(b
, *i
))
674 static int str_compare(char * const *a
, char * const *b
) {
675 return strcmp(*a
, *b
);
678 char** strv_sort(char **l
) {
679 typesafe_qsort(l
, strv_length(l
), str_compare
);
683 int strv_compare(char * const *a
, char * const *b
) {
686 if (strv_isempty(a
)) {
696 for ( ; *a
|| *b
; ++a
, ++b
) {
697 r
= strcmp_ptr(*a
, *b
);
705 void strv_print_full(char * const *l
, const char *prefix
) {
707 printf("%s%s\n", strempty(prefix
), *s
);
710 int strv_extendf(char ***l
, const char *format
, ...) {
715 va_start(ap
, format
);
716 r
= vasprintf(&x
, format
, ap
);
722 return strv_consume(l
, x
);
725 char** strv_reverse(char **l
) {
732 for (size_t i
= 0; i
< n
/ 2; i
++)
733 SWAP_TWO(l
[i
], l
[n
-1-i
]);
738 char** strv_shell_escape(char **l
, const char *bad
) {
739 /* Escapes every character in every string in l that is in bad,
740 * edits in-place, does not roll-back on error. */
745 v
= shell_escape(*s
, bad
);
749 free_and_replace(*s
, v
);
755 bool strv_fnmatch_full(
756 char* const* patterns
,
759 size_t *ret_matched_pos
) {
764 for (size_t i
= 0; patterns
[i
]; i
++)
765 /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
766 * process the pattern for some reason we'll consider this equivalent to non-matching. */
767 if (fnmatch(patterns
[i
], s
, flags
) == 0) {
769 *ret_matched_pos
= i
;
774 *ret_matched_pos
= SIZE_MAX
;
779 char** strv_skip(char **l
, size_t n
) {
791 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
802 /* Adds the value n times to l */
805 if (n
>= SIZE_MAX
- k
)
808 nl
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(k
+ n
+ 1), sizeof(char *));
814 for (i
= k
; i
< k
+ n
; i
++) {
815 nl
[i
] = strdup(value
);
824 for (size_t j
= k
; j
< i
; j
++)
831 int strv_extend_assignment(char ***l
, const char *lhs
, const char *rhs
) {
837 if (!rhs
) /* value is optional, in which case we suppress the field */
840 j
= strjoin(lhs
, "=", rhs
);
844 return strv_consume(l
, j
);
847 int fputstrv(FILE *f
, char * const *l
, const char *separator
, bool *space
) {
851 /* Like fputs(), but for strv, and with a less stupid argument order */
857 r
= fputs_with_space(f
, *s
, separator
, space
);
865 static int string_strv_hashmap_put_internal(Hashmap
*h
, const char *key
, const char *value
) {
869 l
= hashmap_get(h
, key
);
871 /* A list for this key already exists, let's append to it if it is not listed yet */
872 if (strv_contains(l
, value
))
875 r
= strv_extend(&l
, value
);
879 assert_se(hashmap_update(h
, key
, l
) >= 0);
881 /* No list for this key exists yet, create one */
882 _cleanup_strv_free_
char **l2
= NULL
;
883 _cleanup_free_
char *t
= NULL
;
889 r
= strv_extend(&l2
, value
);
893 r
= hashmap_put(h
, t
, l2
);
903 int _string_strv_hashmap_put(Hashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
906 r
= _hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
910 return string_strv_hashmap_put_internal(*h
, key
, value
);
913 int _string_strv_ordered_hashmap_put(OrderedHashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
916 r
= _ordered_hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
920 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h
), key
, value
);
923 DEFINE_HASH_OPS_FULL(string_strv_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char*, strv_free
);