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_biconcat(char ***a
, const char *prefix
, const char* const *b
, const char *suffix
) {
251 v
= strjoin(strempty(prefix
), *s
, suffix
);
255 r
= strv_consume(a
, v
);
263 int strv_split_newlines_full(char ***ret
, const char *s
, ExtractFlags flags
) {
264 _cleanup_strv_free_
char **l
= NULL
;
270 /* Special version of strv_split_full() that splits on newlines and
271 * suppresses an empty string at the end. */
273 r
= strv_split_full(&l
, s
, NEWLINE
, flags
);
278 if (n
> 0 && isempty(l
[n
- 1])) {
279 l
[n
- 1] = mfree(l
[n
- 1]);
287 int strv_split_full(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
288 _cleanup_strv_free_
char **l
= NULL
;
296 _cleanup_free_
char *word
= NULL
;
298 r
= extract_first_word(&s
, &word
, separators
, flags
);
304 if (!GREEDY_REALLOC(l
, n
+ 2))
307 l
[n
++] = TAKE_PTR(word
);
322 int strv_split_and_extend_full(char ***t
, const char *s
, const char *separators
, bool filter_duplicates
, ExtractFlags flags
) {
323 _cleanup_strv_free_
char **l
= NULL
;
329 r
= strv_split_full(&l
, s
, separators
, flags
);
333 r
= strv_extend_strv(t
, l
, filter_duplicates
);
337 return (int) strv_length(*t
);
340 int strv_split_colon_pairs(char ***t
, const char *s
) {
341 _cleanup_strv_free_
char **l
= NULL
;
349 _cleanup_free_
char *first
= NULL
, *second
= NULL
, *tuple
= NULL
, *second_or_empty
= NULL
;
351 r
= extract_first_word(&s
, &tuple
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
357 const char *p
= tuple
;
358 r
= extract_many_words(&p
, ":", EXTRACT_CUNESCAPE
|EXTRACT_UNESCAPE_SEPARATORS
,
364 /* Enforce that at most 2 colon-separated words are contained in each group */
368 second_or_empty
= strdup(strempty(second
));
369 if (!second_or_empty
)
372 if (!GREEDY_REALLOC(l
, n
+ 3))
375 l
[n
++] = TAKE_PTR(first
);
376 l
[n
++] = TAKE_PTR(second_or_empty
);
392 char* strv_join_full(char * const *l
, const char *separator
, const char *prefix
, bool escape_separator
) {
399 k
= strlen(separator
);
400 m
= strlen_ptr(prefix
);
402 if (escape_separator
) /* If the separator was multi-char, we wouldn't know how to escape it. */
410 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
412 n
+= m
+ strlen(*s
) * (1 + needs_escaping
);
422 e
= stpcpy(e
, separator
);
425 e
= stpcpy(e
, prefix
);
427 bool needs_escaping
= escape_separator
&& strchr(*s
, *separator
);
430 for (size_t i
= 0; (*s
)[i
]; i
++) {
431 if ((*s
)[i
] == *separator
)
444 int strv_push_with_size(char ***l
, size_t *n
, char *value
) {
445 /* n is a pointer to a variable to store the size of l.
446 * If not given (i.e. n is NULL or *n is SIZE_MAX), size will be calculated using strv_length().
447 * If n is not NULL, the size after the push will be returned.
448 * If value is empty, no action is taken and *n is not set. */
453 size_t size
= n
? *n
: SIZE_MAX
;
454 if (size
== SIZE_MAX
)
455 size
= strv_length(*l
);
457 /* Check for overflow */
458 if (size
> SIZE_MAX
-2)
461 char **c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(size
+ 2), sizeof(char*));
474 int strv_push_pair(char ***l
, char *a
, char *b
) {
483 /* Check for overflow */
487 /* increase and check for overflow */
488 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ !!a
+ !!b
+ 1), sizeof(char*));
502 int strv_insert(char ***l
, size_t position
, char *value
) {
512 position
= MIN(position
, n
);
514 /* check for overflow and increase*/
515 if (n
> SIZE_MAX
- 2)
519 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(m
), sizeof(char*));
524 memmove(c
+ position
+ 1, c
+ position
, (n
- position
) * sizeof(char*));
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_many_internal(char ***l
, const char *value
, ...) {
598 m
= n
= strv_length(*l
);
602 for (const char *s
= value
; s
!= POINTER_MAX
; s
= va_arg(ap
, const char*)) {
606 if (m
> SIZE_MAX
-1) { /* overflow */
619 char **c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(m
+1), sizeof(char*));
627 for (const char *s
= value
; s
!= POINTER_MAX
; s
= va_arg(ap
, const char*)) {
641 /* rollback on error */
642 for (size_t j
= n
; j
< i
; j
++)
651 char** strv_uniq(char **l
) {
652 /* Drops duplicate entries. The first identical string will be
653 * kept, the others dropped */
656 strv_remove(i
+1, *i
);
661 bool strv_is_uniq(char * const *l
) {
663 if (strv_contains(i
+1, *i
))
669 char** strv_remove(char **l
, const char *s
) {
677 /* Drops every occurrence of s in the string list, edits
680 for (f
= t
= l
; *f
; f
++)
690 bool strv_overlap(char * const *a
, char * const *b
) {
692 if (strv_contains(b
, *i
))
698 static int str_compare(char * const *a
, char * const *b
) {
699 return strcmp(*a
, *b
);
702 char** strv_sort(char **l
) {
703 typesafe_qsort(l
, strv_length(l
), str_compare
);
707 int strv_compare(char * const *a
, char * const *b
) {
710 if (strv_isempty(a
)) {
720 for ( ; *a
|| *b
; ++a
, ++b
) {
721 r
= strcmp_ptr(*a
, *b
);
729 void strv_print_full(char * const *l
, const char *prefix
) {
731 printf("%s%s\n", strempty(prefix
), *s
);
734 int strv_extendf(char ***l
, const char *format
, ...) {
739 va_start(ap
, format
);
740 r
= vasprintf(&x
, format
, ap
);
746 return strv_consume(l
, x
);
749 char* startswith_strv(const char *s
, char * const *l
) {
751 char *found
= startswith(s
, *i
);
759 char* endswith_strv(const char *s
, char * const *l
) {
761 char *found
= endswith(s
, *i
);
769 char** strv_reverse(char **l
) {
776 for (size_t 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
) {
783 /* Escapes every character in every string in l that is in bad,
784 * edits in-place, does not roll-back on error. */
789 v
= shell_escape(*s
, bad
);
793 free_and_replace(*s
, v
);
799 bool strv_fnmatch_full(
800 char* const* patterns
,
803 size_t *ret_matched_pos
) {
808 for (size_t i
= 0; patterns
[i
]; i
++)
809 /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
810 * process the pattern for some reason we'll consider this equivalent to non-matching. */
811 if (fnmatch(patterns
[i
], s
, flags
) == 0) {
813 *ret_matched_pos
= i
;
818 *ret_matched_pos
= SIZE_MAX
;
823 char** strv_skip(char **l
, size_t n
) {
835 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
846 /* Adds the value n times to l */
849 if (n
>= SIZE_MAX
- k
)
852 nl
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(k
+ n
+ 1), sizeof(char *));
858 for (i
= k
; i
< k
+ n
; i
++) {
859 nl
[i
] = strdup(value
);
868 for (size_t j
= k
; j
< i
; j
++)
875 int strv_extend_assignment(char ***l
, const char *lhs
, const char *rhs
) {
881 if (!rhs
) /* value is optional, in which case we suppress the field */
884 j
= strjoin(lhs
, "=", rhs
);
888 return strv_consume(l
, j
);
891 int fputstrv(FILE *f
, char * const *l
, const char *separator
, bool *space
) {
897 /* Like fputs(), but for strv, and with a less stupid argument order */
903 r
= fputs_with_separator(f
, *s
, separator
, space
);
911 static int string_strv_hashmap_put_internal(Hashmap
*h
, const char *key
, const char *value
) {
915 l
= hashmap_get(h
, key
);
917 /* A list for this key already exists, let's append to it if it is not listed yet */
918 if (strv_contains(l
, value
))
921 r
= strv_extend(&l
, value
);
925 assert_se(hashmap_update(h
, key
, l
) >= 0);
927 /* No list for this key exists yet, create one */
928 _cleanup_strv_free_
char **l2
= NULL
;
929 _cleanup_free_
char *t
= NULL
;
935 r
= strv_extend(&l2
, value
);
939 r
= hashmap_put(h
, t
, l2
);
949 int _string_strv_hashmap_put(Hashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
952 r
= _hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
956 return string_strv_hashmap_put_internal(*h
, key
, value
);
959 int _string_strv_ordered_hashmap_put(OrderedHashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
962 r
= _ordered_hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
966 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h
), key
, value
);
969 DEFINE_HASH_OPS_FULL(string_strv_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char*, strv_free
);