udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / strv.c
blob423ad562bf13a8a2fa6dab9103552dc558a9e857
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <fnmatch.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #include "alloc-util.h"
10 #include "env-util.h"
11 #include "escape.h"
12 #include "extract-word.h"
13 #include "fileio.h"
14 #include "memory-util.h"
15 #include "nulstr-util.h"
16 #include "sort-util.h"
17 #include "string-util.h"
18 #include "strv.h"
20 char* strv_find(char * const *l, const char *name) {
21 assert(name);
23 STRV_FOREACH(i, l)
24 if (streq(*i, name))
25 return *i;
27 return NULL;
30 char* strv_find_case(char * const *l, const char *name) {
31 assert(name);
33 STRV_FOREACH(i, l)
34 if (strcaseeq(*i, name))
35 return *i;
37 return NULL;
40 char* strv_find_prefix(char * const *l, const char *name) {
41 assert(name);
43 STRV_FOREACH(i, l)
44 if (startswith(*i, name))
45 return *i;
47 return NULL;
50 char* strv_find_startswith(char * const *l, const char *name) {
51 assert(name);
53 /* Like strv_find_prefix, but actually returns only the
54 * suffix, not the whole item */
56 STRV_FOREACH(i, l) {
57 char *e;
59 e = startswith(*i, name);
60 if (e)
61 return e;
64 return NULL;
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);
70 if (value)
71 return value;
74 return NULL;
77 char** strv_free(char **l) {
78 STRV_FOREACH(k, l)
79 free(*k);
81 return mfree(l);
84 char** strv_free_erase(char **l) {
85 STRV_FOREACH(i, l)
86 erase_and_freep(i);
88 return mfree(l);
91 char** strv_copy_n(char * const *l, size_t m) {
92 _cleanup_strv_free_ char **result = NULL;
93 char **k;
95 result = new(char*, MIN(strv_length(l), m) + 1);
96 if (!result)
97 return NULL;
99 k = result;
100 STRV_FOREACH(i, l) {
101 if (m == 0)
102 break;
104 *k = strdup(*i);
105 if (!*k)
106 return NULL;
107 k++;
109 if (m != SIZE_MAX)
110 m--;
113 *k = NULL;
114 return TAKE_PTR(result);
117 int strv_copy_unless_empty(char * const *l, char ***ret) {
118 assert(ret);
120 if (strv_isempty(l)) {
121 *ret = NULL;
122 return 0;
125 char **copy = strv_copy(l);
126 if (!copy)
127 return -ENOMEM;
129 *ret = TAKE_PTR(copy);
130 return 1;
133 size_t strv_length(char * const *l) {
134 size_t n = 0;
136 STRV_FOREACH(i, l)
137 n++;
139 return n;
142 char** strv_new_ap(const char *x, va_list ap) {
143 _cleanup_strv_free_ char **a = NULL;
144 size_t n = 0, i = 0;
145 va_list aq;
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. */
152 va_copy(aq, ap);
153 for (const char *s = x; s; s = va_arg(aq, const char*)) {
154 if (s == STRV_IGNORE)
155 continue;
157 n++;
159 va_end(aq);
161 a = new(char*, n+1);
162 if (!a)
163 return NULL;
165 for (const char *s = x; s; s = va_arg(ap, const char*)) {
166 if (s == STRV_IGNORE)
167 continue;
169 a[i] = strdup(s);
170 if (!a[i])
171 return NULL;
173 i++;
176 a[i] = NULL;
178 return TAKE_PTR(a);
181 char** strv_new_internal(const char *x, ...) {
182 char **r;
183 va_list ap;
185 va_start(ap, x);
186 r = strv_new_ap(x, ap);
187 va_end(ap);
189 return r;
192 int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
193 size_t p, q, i = 0;
194 char **t;
196 assert(a);
198 if (strv_isempty(b))
199 return 0;
201 p = strv_length(*a);
202 q = strv_length(b);
204 if (p >= SIZE_MAX - q)
205 return -ENOMEM;
207 t = reallocarray(*a, GREEDY_ALLOC_ROUND_UP(p + q + 1), sizeof(char *));
208 if (!t)
209 return -ENOMEM;
211 t[p] = NULL;
212 *a = t;
214 STRV_FOREACH(s, b) {
215 if (filter_duplicates && strv_contains(t, *s))
216 continue;
218 t[p+i] = strdup(*s);
219 if (!t[p+i])
220 goto rollback;
222 i++;
223 t[p+i] = NULL;
226 assert(i <= q);
228 return (int) i;
230 rollback:
231 for (size_t j = 0; j < i; j++)
232 free(t[p + j]);
234 t[p] = NULL;
235 return -ENOMEM;
238 int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
239 int r;
241 STRV_FOREACH(s, b) {
242 char *v;
244 v = strjoin(*s, suffix);
245 if (!v)
246 return -ENOMEM;
248 r = strv_push(a, v);
249 if (r < 0) {
250 free(v);
251 return r;
255 return 0;
258 int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags) {
259 _cleanup_strv_free_ char **l = NULL;
260 size_t n;
261 int r;
263 assert(s);
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);
269 if (r < 0)
270 return r;
272 n = strv_length(l);
273 if (n > 0 && isempty(l[n - 1])) {
274 l[n - 1] = mfree(l[n - 1]);
275 n--;
278 *ret = TAKE_PTR(l);
279 return n;
282 int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags) {
283 _cleanup_strv_free_ char **l = NULL;
284 size_t n = 0;
285 int r;
287 assert(t);
288 assert(s);
290 for (;;) {
291 _cleanup_free_ char *word = NULL;
293 r = extract_first_word(&s, &word, separators, flags);
294 if (r < 0)
295 return r;
296 if (r == 0)
297 break;
299 if (!GREEDY_REALLOC(l, n + 2))
300 return -ENOMEM;
302 l[n++] = TAKE_PTR(word);
303 l[n] = NULL;
306 if (!l) {
307 l = new0(char*, 1);
308 if (!l)
309 return -ENOMEM;
312 *t = TAKE_PTR(l);
314 return (int) n;
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;
319 int r;
321 assert(t);
322 assert(s);
324 r = strv_split_full(&l, s, separators, flags);
325 if (r < 0)
326 return r;
328 r = strv_extend_strv(t, l, filter_duplicates);
329 if (r < 0)
330 return r;
332 return (int) strv_length(*t);
335 int strv_split_colon_pairs(char ***t, const char *s) {
336 _cleanup_strv_free_ char **l = NULL;
337 size_t n = 0;
338 int r;
340 assert(t);
341 assert(s);
343 for (;;) {
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);
347 if (r < 0)
348 return r;
349 if (r == 0)
350 break;
352 const char *p = tuple;
353 r = extract_many_words(&p, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS,
354 &first, &second, NULL);
355 if (r < 0)
356 return r;
357 if (r == 0)
358 continue;
359 /* Enforce that at most 2 colon-separated words are contained in each group */
360 if (!isempty(p))
361 return -EINVAL;
363 second_or_empty = strdup(strempty(second));
364 if (!second_or_empty)
365 return -ENOMEM;
367 if (!GREEDY_REALLOC(l, n + 3))
368 return -ENOMEM;
370 l[n++] = TAKE_PTR(first);
371 l[n++] = TAKE_PTR(second_or_empty);
373 l[n] = NULL;
376 if (!l) {
377 l = new0(char*, 1);
378 if (!l)
379 return -ENOMEM;
382 *t = TAKE_PTR(l);
384 return (int) n;
387 char* strv_join_full(char * const *l, const char *separator, const char *prefix, bool escape_separator) {
388 char *r, *e;
389 size_t n, k, m;
391 if (!separator)
392 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. */
398 assert(k == 1);
400 n = 0;
401 STRV_FOREACH(s, l) {
402 if (s != l)
403 n += k;
405 bool needs_escaping = escape_separator && strchr(*s, *separator);
407 n += m + strlen(*s) * (1 + needs_escaping);
410 r = new(char, n+1);
411 if (!r)
412 return NULL;
414 e = r;
415 STRV_FOREACH(s, l) {
416 if (s != l)
417 e = stpcpy(e, separator);
419 if (prefix)
420 e = stpcpy(e, prefix);
422 bool needs_escaping = escape_separator && strchr(*s, *separator);
424 if (needs_escaping)
425 for (size_t i = 0; (*s)[i]; i++) {
426 if ((*s)[i] == *separator)
427 *(e++) = '\\';
428 *(e++) = (*s)[i];
430 else
431 e = stpcpy(e, *s);
434 *e = 0;
436 return r;
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. */
445 if (!value)
446 return 0;
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)
454 return -ENOMEM;
456 char **c = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(size + 2), sizeof(char*));
457 if (!c)
458 return -ENOMEM;
460 c[size] = value;
461 c[size+1] = NULL;
463 *l = c;
464 if (n)
465 *n = size + 1;
466 return 0;
469 int strv_push_pair(char ***l, char *a, char *b) {
470 char **c;
471 size_t n;
473 if (!a && !b)
474 return 0;
476 n = strv_length(*l);
478 /* Check for overflow */
479 if (n > SIZE_MAX-3)
480 return -ENOMEM;
482 /* increase and check for overflow */
483 c = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(n + !!a + !!b + 1), sizeof(char*));
484 if (!c)
485 return -ENOMEM;
487 if (a)
488 c[n++] = a;
489 if (b)
490 c[n++] = b;
491 c[n] = NULL;
493 *l = c;
494 return 0;
497 int strv_insert(char ***l, size_t position, char *value) {
498 char **c;
499 size_t n, m;
501 if (!value)
502 return 0;
504 n = strv_length(*l);
505 position = MIN(position, n);
507 /* increase and check for overflow */
508 m = n + 2;
509 if (m < n)
510 return -ENOMEM;
512 c = new(char*, m);
513 if (!c)
514 return -ENOMEM;
516 for (size_t i = 0; i < position; i++)
517 c[i] = (*l)[i];
518 c[position] = value;
519 for (size_t i = position; i < n; i++)
520 c[i+1] = (*l)[i];
521 c[n+1] = NULL;
523 return free_and_replace(*l, c);
526 int strv_consume_with_size(char ***l, size_t *n, char *value) {
527 int r;
529 r = strv_push_with_size(l, n, value);
530 if (r < 0)
531 free(value);
533 return r;
536 int strv_consume_pair(char ***l, char *a, char *b) {
537 int r;
539 r = strv_push_pair(l, a, b);
540 if (r < 0) {
541 free(a);
542 free(b);
545 return r;
548 int strv_consume_prepend(char ***l, char *value) {
549 int r;
551 r = strv_push_prepend(l, value);
552 if (r < 0)
553 free(value);
555 return r;
558 int strv_prepend(char ***l, const char *value) {
559 char *v;
561 if (!value)
562 return 0;
564 v = strdup(value);
565 if (!v)
566 return -ENOMEM;
568 return strv_consume_prepend(l, v);
571 int strv_extend_with_size(char ***l, size_t *n, const char *value) {
572 char *v;
574 if (!value)
575 return 0;
577 v = strdup(value);
578 if (!v)
579 return -ENOMEM;
581 return strv_consume_with_size(l, n, v);
584 int strv_extend_front(char ***l, const char *value) {
585 size_t n, m;
586 char *v, **c;
588 assert(l);
590 /* Like strv_extend(), but prepends rather than appends the new entry */
592 if (!value)
593 return 0;
595 n = strv_length(*l);
597 /* Increase and overflow check. */
598 m = n + 2;
599 if (m < n)
600 return -ENOMEM;
602 v = strdup(value);
603 if (!v)
604 return -ENOMEM;
606 c = reallocarray(*l, m, sizeof(char*));
607 if (!c) {
608 free(v);
609 return -ENOMEM;
612 memmove(c+1, c, n * sizeof(char*));
613 c[0] = v;
614 c[n+1] = NULL;
616 *l = c;
617 return 0;
620 char** strv_uniq(char **l) {
621 /* Drops duplicate entries. The first identical string will be
622 * kept, the others dropped */
624 STRV_FOREACH(i, l)
625 strv_remove(i+1, *i);
627 return l;
630 bool strv_is_uniq(char * const *l) {
631 STRV_FOREACH(i, l)
632 if (strv_contains(i+1, *i))
633 return false;
635 return true;
638 char** strv_remove(char **l, const char *s) {
639 char **f, **t;
641 if (!l)
642 return NULL;
644 assert(s);
646 /* Drops every occurrence of s in the string list, edits
647 * in-place. */
649 for (f = t = l; *f; f++)
650 if (streq(*f, s))
651 free(*f);
652 else
653 *(t++) = *f;
655 *t = NULL;
656 return l;
659 bool strv_overlap(char * const *a, char * const *b) {
660 STRV_FOREACH(i, a)
661 if (strv_contains(b, *i))
662 return true;
664 return false;
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);
673 return l;
676 int strv_compare(char * const *a, char * const *b) {
677 int r;
679 if (strv_isempty(a)) {
680 if (strv_isempty(b))
681 return 0;
682 else
683 return -1;
686 if (strv_isempty(b))
687 return 1;
689 for ( ; *a || *b; ++a, ++b) {
690 r = strcmp_ptr(*a, *b);
691 if (r != 0)
692 return r;
695 return 0;
698 void strv_print_full(char * const *l, const char *prefix) {
699 STRV_FOREACH(s, l)
700 printf("%s%s\n", strempty(prefix), *s);
703 int strv_extendf(char ***l, const char *format, ...) {
704 va_list ap;
705 char *x;
706 int r;
708 va_start(ap, format);
709 r = vasprintf(&x, format, ap);
710 va_end(ap);
712 if (r < 0)
713 return -ENOMEM;
715 return strv_consume(l, x);
718 char** strv_reverse(char **l) {
719 size_t n;
721 n = strv_length(l);
722 if (n <= 1)
723 return l;
725 for (size_t i = 0; i < n / 2; i++)
726 SWAP_TWO(l[i], l[n-1-i]);
728 return l;
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. */
735 STRV_FOREACH(s, l) {
736 char *v;
738 v = shell_escape(*s, bad);
739 if (!v)
740 return NULL;
742 free_and_replace(*s, v);
745 return l;
748 bool strv_fnmatch_full(
749 char* const* patterns,
750 const char *s,
751 int flags,
752 size_t *ret_matched_pos) {
754 assert(s);
756 if (patterns)
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) {
761 if (ret_matched_pos)
762 *ret_matched_pos = i;
763 return true;
766 if (ret_matched_pos)
767 *ret_matched_pos = SIZE_MAX;
769 return false;
772 char** strv_skip(char **l, size_t n) {
774 while (n > 0) {
775 if (strv_isempty(l))
776 return l;
778 l++, n--;
781 return l;
784 int strv_extend_n(char ***l, const char *value, size_t n) {
785 size_t i, k;
786 char **nl;
788 assert(l);
790 if (!value)
791 return 0;
792 if (n == 0)
793 return 0;
795 /* Adds the value n times to l */
797 k = strv_length(*l);
798 if (n >= SIZE_MAX - k)
799 return -ENOMEM;
801 nl = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(k + n + 1), sizeof(char *));
802 if (!nl)
803 return -ENOMEM;
805 *l = nl;
807 for (i = k; i < k + n; i++) {
808 nl[i] = strdup(value);
809 if (!nl[i])
810 goto rollback;
812 nl[i] = NULL;
814 return 0;
816 rollback:
817 for (size_t j = k; j < i; j++)
818 free(nl[j]);
819 nl[k] = NULL;
821 return -ENOMEM;
824 int strv_extend_assignment(char ***l, const char *lhs, const char *rhs) {
825 char *j;
827 assert(l);
828 assert(lhs);
830 if (!rhs) /* value is optional, in which case we suppress the field */
831 return 0;
833 j = strjoin(lhs, "=", rhs);
834 if (!j)
835 return -ENOMEM;
837 return strv_consume(l, j);
840 int fputstrv(FILE *f, char * const *l, const char *separator, bool *space) {
841 bool b = false;
842 int r;
844 /* Like fputs(), but for strv, and with a less stupid argument order */
846 if (!space)
847 space = &b;
849 STRV_FOREACH(s, l) {
850 r = fputs_with_space(f, *s, separator, space);
851 if (r < 0)
852 return r;
855 return 0;
858 static int string_strv_hashmap_put_internal(Hashmap *h, const char *key, const char *value) {
859 char **l;
860 int r;
862 l = hashmap_get(h, key);
863 if (l) {
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))
866 return 0;
868 r = strv_extend(&l, value);
869 if (r < 0)
870 return r;
872 assert_se(hashmap_update(h, key, l) >= 0);
873 } else {
874 /* No list for this key exists yet, create one */
875 _cleanup_strv_free_ char **l2 = NULL;
876 _cleanup_free_ char *t = NULL;
878 t = strdup(key);
879 if (!t)
880 return -ENOMEM;
882 r = strv_extend(&l2, value);
883 if (r < 0)
884 return r;
886 r = hashmap_put(h, t, l2);
887 if (r < 0)
888 return r;
889 TAKE_PTR(t);
890 TAKE_PTR(l2);
893 return 1;
896 int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS) {
897 int r;
899 r = _hashmap_ensure_allocated(h, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS);
900 if (r < 0)
901 return r;
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) {
907 int r;
909 r = _ordered_hashmap_ensure_allocated(h, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS);
910 if (r < 0)
911 return r;
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);