]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/strv.c
Merge pull request #16046 from bluca/dissect_squashfs_verity
[thirdparty/systemd.git] / src / basic / strv.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fnmatch.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "alloc-util.h"
10 #include "escape.h"
11 #include "extract-word.h"
12 #include "fileio.h"
13 #include "memory-util.h"
14 #include "nulstr-util.h"
15 #include "sort-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18
19 char *strv_find(char * const *l, const char *name) {
20 char * const *i;
21
22 assert(name);
23
24 STRV_FOREACH(i, l)
25 if (streq(*i, name))
26 return *i;
27
28 return NULL;
29 }
30
31 char *strv_find_case(char * const *l, const char *name) {
32 char * const *i;
33
34 assert(name);
35
36 STRV_FOREACH(i, l)
37 if (strcaseeq(*i, name))
38 return *i;
39
40 return NULL;
41 }
42
43 char *strv_find_prefix(char * const *l, const char *name) {
44 char * const *i;
45
46 assert(name);
47
48 STRV_FOREACH(i, l)
49 if (startswith(*i, name))
50 return *i;
51
52 return NULL;
53 }
54
55 char *strv_find_startswith(char * const *l, const char *name) {
56 char * const *i, *e;
57
58 assert(name);
59
60 /* Like strv_find_prefix, but actually returns only the
61 * suffix, not the whole item */
62
63 STRV_FOREACH(i, l) {
64 e = startswith(*i, name);
65 if (e)
66 return e;
67 }
68
69 return NULL;
70 }
71
72 char **strv_free(char **l) {
73 char **k;
74
75 if (!l)
76 return NULL;
77
78 for (k = l; *k; k++)
79 free(*k);
80
81 return mfree(l);
82 }
83
84 char **strv_free_erase(char **l) {
85 char **i;
86
87 STRV_FOREACH(i, l)
88 erase_and_freep(i);
89
90 return mfree(l);
91 }
92
93 char **strv_copy(char * const *l) {
94 char **r, **k;
95
96 k = r = new(char*, strv_length(l) + 1);
97 if (!r)
98 return NULL;
99
100 if (l)
101 for (; *l; k++, l++) {
102 *k = strdup(*l);
103 if (!*k) {
104 strv_free(r);
105 return NULL;
106 }
107 }
108
109 *k = NULL;
110 return r;
111 }
112
113 size_t strv_length(char * const *l) {
114 size_t n = 0;
115
116 if (!l)
117 return 0;
118
119 for (; *l; l++)
120 n++;
121
122 return n;
123 }
124
125 char **strv_new_ap(const char *x, va_list ap) {
126 const char *s;
127 _cleanup_strv_free_ char **a = NULL;
128 size_t n = 0, i = 0;
129 va_list aq;
130
131 /* As a special trick we ignore all listed strings that equal
132 * STRV_IGNORE. This is supposed to be used with the
133 * STRV_IFNOTNULL() macro to include possibly NULL strings in
134 * the string list. */
135
136 if (x) {
137 n = x == STRV_IGNORE ? 0 : 1;
138
139 va_copy(aq, ap);
140 while ((s = va_arg(aq, const char*))) {
141 if (s == STRV_IGNORE)
142 continue;
143
144 n++;
145 }
146
147 va_end(aq);
148 }
149
150 a = new(char*, n+1);
151 if (!a)
152 return NULL;
153
154 if (x) {
155 if (x != STRV_IGNORE) {
156 a[i] = strdup(x);
157 if (!a[i])
158 return NULL;
159 i++;
160 }
161
162 while ((s = va_arg(ap, const char*))) {
163
164 if (s == STRV_IGNORE)
165 continue;
166
167 a[i] = strdup(s);
168 if (!a[i])
169 return NULL;
170
171 i++;
172 }
173 }
174
175 a[i] = NULL;
176
177 return TAKE_PTR(a);
178 }
179
180 char **strv_new_internal(const char *x, ...) {
181 char **r;
182 va_list ap;
183
184 va_start(ap, x);
185 r = strv_new_ap(x, ap);
186 va_end(ap);
187
188 return r;
189 }
190
191 int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
192 char * const *s, **t;
193 size_t p, q, i = 0, j;
194
195 assert(a);
196
197 if (strv_isempty(b))
198 return 0;
199
200 p = strv_length(*a);
201 q = strv_length(b);
202
203 if (p >= SIZE_MAX - q)
204 return -ENOMEM;
205
206 t = reallocarray(*a, GREEDY_ALLOC_ROUND_UP(p + q + 1), sizeof(char *));
207 if (!t)
208 return -ENOMEM;
209
210 t[p] = NULL;
211 *a = t;
212
213 STRV_FOREACH(s, b) {
214
215 if (filter_duplicates && strv_contains(t, *s))
216 continue;
217
218 t[p+i] = strdup(*s);
219 if (!t[p+i])
220 goto rollback;
221
222 i++;
223 t[p+i] = NULL;
224 }
225
226 assert(i <= q);
227
228 return (int) i;
229
230 rollback:
231 for (j = 0; j < i; j++)
232 free(t[p + j]);
233
234 t[p] = NULL;
235 return -ENOMEM;
236 }
237
238 int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
239 char * const *s;
240 int r;
241
242 STRV_FOREACH(s, b) {
243 char *v;
244
245 v = strjoin(*s, suffix);
246 if (!v)
247 return -ENOMEM;
248
249 r = strv_push(a, v);
250 if (r < 0) {
251 free(v);
252 return r;
253 }
254 }
255
256 return 0;
257 }
258
259 char **strv_split_full(const char *s, const char *separator, SplitFlags flags) {
260 const char *word, *state;
261 size_t l;
262 size_t n, i;
263 char **r;
264
265 assert(s);
266
267 if (!separator)
268 separator = WHITESPACE;
269
270 s += strspn(s, separator);
271 if (isempty(s))
272 return new0(char*, 1);
273
274 n = 0;
275 _FOREACH_WORD(word, l, s, separator, flags, state)
276 n++;
277
278 r = new(char*, n+1);
279 if (!r)
280 return NULL;
281
282 i = 0;
283 _FOREACH_WORD(word, l, s, separator, flags, state) {
284 r[i] = strndup(word, l);
285 if (!r[i]) {
286 strv_free(r);
287 return NULL;
288 }
289
290 i++;
291 }
292
293 r[i] = NULL;
294 return r;
295 }
296
297 char **strv_split_newlines(const char *s) {
298 char **l;
299 size_t n;
300
301 assert(s);
302
303 /* Special version of strv_split() that splits on newlines and
304 * suppresses an empty string at the end */
305
306 l = strv_split(s, NEWLINE);
307 if (!l)
308 return NULL;
309
310 n = strv_length(l);
311 if (n <= 0)
312 return l;
313
314 if (isempty(l[n - 1]))
315 l[n - 1] = mfree(l[n - 1]);
316
317 return l;
318 }
319
320 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
321 _cleanup_strv_free_ char **l = NULL;
322 size_t n = 0, allocated = 0;
323 int r;
324
325 assert(t);
326 assert(s);
327
328 for (;;) {
329 _cleanup_free_ char *word = NULL;
330
331 r = extract_first_word(&s, &word, separators, flags);
332 if (r < 0)
333 return r;
334 if (r == 0)
335 break;
336
337 if (!GREEDY_REALLOC(l, allocated, n + 2))
338 return -ENOMEM;
339
340 l[n++] = TAKE_PTR(word);
341
342 l[n] = NULL;
343 }
344
345 if (!l) {
346 l = new0(char*, 1);
347 if (!l)
348 return -ENOMEM;
349 }
350
351 *t = TAKE_PTR(l);
352
353 return (int) n;
354 }
355
356 char *strv_join_prefix(char * const *l, const char *separator, const char *prefix) {
357 char * const *s;
358 char *r, *e;
359 size_t n, k, m;
360
361 if (!separator)
362 separator = " ";
363
364 k = strlen(separator);
365 m = strlen_ptr(prefix);
366
367 n = 0;
368 STRV_FOREACH(s, l) {
369 if (s != l)
370 n += k;
371 n += m + strlen(*s);
372 }
373
374 r = new(char, n+1);
375 if (!r)
376 return NULL;
377
378 e = r;
379 STRV_FOREACH(s, l) {
380 if (s != l)
381 e = stpcpy(e, separator);
382
383 if (prefix)
384 e = stpcpy(e, prefix);
385
386 e = stpcpy(e, *s);
387 }
388
389 *e = 0;
390
391 return r;
392 }
393
394 int strv_push(char ***l, char *value) {
395 char **c;
396 size_t n;
397
398 if (!value)
399 return 0;
400
401 n = strv_length(*l);
402
403 /* Check for overflow */
404 if (n > SIZE_MAX-2)
405 return -ENOMEM;
406
407 c = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(n + 2), sizeof(char*));
408 if (!c)
409 return -ENOMEM;
410
411 c[n] = value;
412 c[n+1] = NULL;
413
414 *l = c;
415 return 0;
416 }
417
418 int strv_push_pair(char ***l, char *a, char *b) {
419 char **c;
420 size_t n;
421
422 if (!a && !b)
423 return 0;
424
425 n = strv_length(*l);
426
427 /* Check for overflow */
428 if (n > SIZE_MAX-3)
429 return -ENOMEM;
430
431 /* increase and check for overflow */
432 c = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(n + !!a + !!b + 1), sizeof(char*));
433 if (!c)
434 return -ENOMEM;
435
436 if (a)
437 c[n++] = a;
438 if (b)
439 c[n++] = b;
440 c[n] = NULL;
441
442 *l = c;
443 return 0;
444 }
445
446 int strv_insert(char ***l, size_t position, char *value) {
447 char **c;
448 size_t n, m, i;
449
450 if (!value)
451 return 0;
452
453 n = strv_length(*l);
454 position = MIN(position, n);
455
456 /* increase and check for overflow */
457 m = n + 2;
458 if (m < n)
459 return -ENOMEM;
460
461 c = new(char*, m);
462 if (!c)
463 return -ENOMEM;
464
465 for (i = 0; i < position; i++)
466 c[i] = (*l)[i];
467 c[position] = value;
468 for (i = position; i < n; i++)
469 c[i+1] = (*l)[i];
470
471 c[n+1] = NULL;
472
473 free(*l);
474 *l = c;
475
476 return 0;
477 }
478
479 int strv_consume(char ***l, char *value) {
480 int r;
481
482 r = strv_push(l, value);
483 if (r < 0)
484 free(value);
485
486 return r;
487 }
488
489 int strv_consume_pair(char ***l, char *a, char *b) {
490 int r;
491
492 r = strv_push_pair(l, a, b);
493 if (r < 0) {
494 free(a);
495 free(b);
496 }
497
498 return r;
499 }
500
501 int strv_consume_prepend(char ***l, char *value) {
502 int r;
503
504 r = strv_push_prepend(l, value);
505 if (r < 0)
506 free(value);
507
508 return r;
509 }
510
511 int strv_extend(char ***l, const char *value) {
512 char *v;
513
514 if (!value)
515 return 0;
516
517 v = strdup(value);
518 if (!v)
519 return -ENOMEM;
520
521 return strv_consume(l, v);
522 }
523
524 int strv_extend_front(char ***l, const char *value) {
525 size_t n, m;
526 char *v, **c;
527
528 assert(l);
529
530 /* Like strv_extend(), but prepends rather than appends the new entry */
531
532 if (!value)
533 return 0;
534
535 n = strv_length(*l);
536
537 /* Increase and overflow check. */
538 m = n + 2;
539 if (m < n)
540 return -ENOMEM;
541
542 v = strdup(value);
543 if (!v)
544 return -ENOMEM;
545
546 c = reallocarray(*l, m, sizeof(char*));
547 if (!c) {
548 free(v);
549 return -ENOMEM;
550 }
551
552 memmove(c+1, c, n * sizeof(char*));
553 c[0] = v;
554 c[n+1] = NULL;
555
556 *l = c;
557 return 0;
558 }
559
560 char **strv_uniq(char **l) {
561 char **i;
562
563 /* Drops duplicate entries. The first identical string will be
564 * kept, the others dropped */
565
566 STRV_FOREACH(i, l)
567 strv_remove(i+1, *i);
568
569 return l;
570 }
571
572 bool strv_is_uniq(char * const *l) {
573 char * const *i;
574
575 STRV_FOREACH(i, l)
576 if (strv_find(i+1, *i))
577 return false;
578
579 return true;
580 }
581
582 char **strv_remove(char **l, const char *s) {
583 char **f, **t;
584
585 if (!l)
586 return NULL;
587
588 assert(s);
589
590 /* Drops every occurrence of s in the string list, edits
591 * in-place. */
592
593 for (f = t = l; *f; f++)
594 if (streq(*f, s))
595 free(*f);
596 else
597 *(t++) = *f;
598
599 *t = NULL;
600 return l;
601 }
602
603 char **strv_parse_nulstr(const char *s, size_t l) {
604 /* l is the length of the input data, which will be split at NULs into
605 * elements of the resulting strv. Hence, the number of items in the resulting strv
606 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
607 * unless s[l-1] is NUL, in which case the final empty string is not stored in
608 * the resulting strv, and length is equal to the number of NUL bytes.
609 *
610 * Note that contrary to a normal nulstr which cannot contain empty strings, because
611 * the input data is terminated by any two consequent NUL bytes, this parser accepts
612 * empty strings in s.
613 */
614
615 const char *p;
616 size_t c = 0, i = 0;
617 char **v;
618
619 assert(s || l <= 0);
620
621 if (l <= 0)
622 return new0(char*, 1);
623
624 for (p = s; p < s + l; p++)
625 if (*p == 0)
626 c++;
627
628 if (s[l-1] != 0)
629 c++;
630
631 v = new0(char*, c+1);
632 if (!v)
633 return NULL;
634
635 p = s;
636 while (p < s + l) {
637 const char *e;
638
639 e = memchr(p, 0, s + l - p);
640
641 v[i] = strndup(p, e ? e - p : s + l - p);
642 if (!v[i]) {
643 strv_free(v);
644 return NULL;
645 }
646
647 i++;
648
649 if (!e)
650 break;
651
652 p = e + 1;
653 }
654
655 assert(i == c);
656
657 return v;
658 }
659
660 char **strv_split_nulstr(const char *s) {
661 const char *i;
662 char **r = NULL;
663
664 NULSTR_FOREACH(i, s)
665 if (strv_extend(&r, i) < 0) {
666 strv_free(r);
667 return NULL;
668 }
669
670 if (!r)
671 return strv_new(NULL);
672
673 return r;
674 }
675
676 int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) {
677 /* A valid nulstr with two NULs at the end will be created, but
678 * q will be the length without the two trailing NULs. Thus the output
679 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
680 * and can also be parsed by strv_parse_nulstr as long as the length
681 * is provided separately.
682 */
683
684 size_t n_allocated = 0, n = 0;
685 _cleanup_free_ char *m = NULL;
686 char * const *i;
687
688 assert(ret);
689 assert(ret_size);
690
691 STRV_FOREACH(i, l) {
692 size_t z;
693
694 z = strlen(*i);
695
696 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
697 return -ENOMEM;
698
699 memcpy(m + n, *i, z + 1);
700 n += z + 1;
701 }
702
703 if (!m) {
704 m = new0(char, 1);
705 if (!m)
706 return -ENOMEM;
707 n = 1;
708 } else
709 /* make sure there is a second extra NUL at the end of resulting nulstr */
710 m[n] = '\0';
711
712 assert(n > 0);
713 *ret = m;
714 *ret_size = n - 1;
715
716 m = NULL;
717
718 return 0;
719 }
720
721 bool strv_overlap(char * const *a, char * const *b) {
722 char * const *i;
723
724 STRV_FOREACH(i, a)
725 if (strv_contains(b, *i))
726 return true;
727
728 return false;
729 }
730
731 static int str_compare(char * const *a, char * const *b) {
732 return strcmp(*a, *b);
733 }
734
735 char **strv_sort(char **l) {
736 typesafe_qsort(l, strv_length(l), str_compare);
737 return l;
738 }
739
740 int strv_compare(char * const *a, char * const *b) {
741 int r;
742
743 if (strv_isempty(a)) {
744 if (strv_isempty(b))
745 return 0;
746 else
747 return -1;
748 }
749
750 if (strv_isempty(b))
751 return 1;
752
753 for ( ; *a || *b; ++a, ++b) {
754 r = strcmp_ptr(*a, *b);
755 if (r != 0)
756 return r;
757 }
758
759 return 0;
760 }
761
762 void strv_print(char * const *l) {
763 char * const *s;
764
765 STRV_FOREACH(s, l)
766 puts(*s);
767 }
768
769 int strv_extendf(char ***l, const char *format, ...) {
770 va_list ap;
771 char *x;
772 int r;
773
774 va_start(ap, format);
775 r = vasprintf(&x, format, ap);
776 va_end(ap);
777
778 if (r < 0)
779 return -ENOMEM;
780
781 return strv_consume(l, x);
782 }
783
784 char **strv_reverse(char **l) {
785 size_t n, i;
786
787 n = strv_length(l);
788 if (n <= 1)
789 return l;
790
791 for (i = 0; i < n / 2; i++)
792 SWAP_TWO(l[i], l[n-1-i]);
793
794 return l;
795 }
796
797 char **strv_shell_escape(char **l, const char *bad) {
798 char **s;
799
800 /* Escapes every character in every string in l that is in bad,
801 * edits in-place, does not roll-back on error. */
802
803 STRV_FOREACH(s, l) {
804 char *v;
805
806 v = shell_escape(*s, bad);
807 if (!v)
808 return NULL;
809
810 free(*s);
811 *s = v;
812 }
813
814 return l;
815 }
816
817 bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos) {
818 for (size_t i = 0; patterns && patterns[i]; i++)
819 if (fnmatch(patterns[i], s, flags) == 0) {
820 if (matched_pos)
821 *matched_pos = i;
822 return true;
823 }
824
825 return false;
826 }
827
828 char ***strv_free_free(char ***l) {
829 char ***i;
830
831 if (!l)
832 return NULL;
833
834 for (i = l; *i; i++)
835 strv_free(*i);
836
837 return mfree(l);
838 }
839
840 char **strv_skip(char **l, size_t n) {
841
842 while (n > 0) {
843 if (strv_isempty(l))
844 return l;
845
846 l++, n--;
847 }
848
849 return l;
850 }
851
852 int strv_extend_n(char ***l, const char *value, size_t n) {
853 size_t i, j, k;
854 char **nl;
855
856 assert(l);
857
858 if (!value)
859 return 0;
860 if (n == 0)
861 return 0;
862
863 /* Adds the value n times to l */
864
865 k = strv_length(*l);
866 if (n >= SIZE_MAX - k)
867 return -ENOMEM;
868
869 nl = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(k + n + 1), sizeof(char *));
870 if (!nl)
871 return -ENOMEM;
872
873 *l = nl;
874
875 for (i = k; i < k + n; i++) {
876 nl[i] = strdup(value);
877 if (!nl[i])
878 goto rollback;
879 }
880
881 nl[i] = NULL;
882 return 0;
883
884 rollback:
885 for (j = k; j < i; j++)
886 free(nl[j]);
887
888 nl[k] = NULL;
889 return -ENOMEM;
890 }
891
892 int fputstrv(FILE *f, char * const *l, const char *separator, bool *space) {
893 bool b = false;
894 char * const *s;
895 int r;
896
897 /* Like fputs(), but for strv, and with a less stupid argument order */
898
899 if (!space)
900 space = &b;
901
902 STRV_FOREACH(s, l) {
903 r = fputs_with_space(f, *s, separator, space);
904 if (r < 0)
905 return r;
906 }
907
908 return 0;
909 }
910
911 static int string_strv_hashmap_put_internal(Hashmap *h, const char *key, const char *value) {
912 char **l;
913 int r;
914
915 l = hashmap_get(h, key);
916 if (l) {
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))
919 return 0;
920
921 r = strv_extend(&l, value);
922 if (r < 0)
923 return r;
924
925 assert_se(hashmap_update(h, key, l) >= 0);
926 } else {
927 /* No list for this key exists yet, create one */
928 _cleanup_strv_free_ char **l2 = NULL;
929 _cleanup_free_ char *t = NULL;
930
931 t = strdup(key);
932 if (!t)
933 return -ENOMEM;
934
935 r = strv_extend(&l2, value);
936 if (r < 0)
937 return r;
938
939 r = hashmap_put(h, t, l2);
940 if (r < 0)
941 return r;
942 TAKE_PTR(t);
943 TAKE_PTR(l2);
944 }
945
946 return 1;
947 }
948
949 int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS) {
950 int r;
951
952 r = _hashmap_ensure_allocated(h, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS);
953 if (r < 0)
954 return r;
955
956 return string_strv_hashmap_put_internal(*h, key, value);
957 }
958
959 int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS) {
960 int r;
961
962 r = _ordered_hashmap_ensure_allocated(h, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS);
963 if (r < 0)
964 return r;
965
966 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h), key, value);
967 }
968
969 DEFINE_HASH_OPS_FULL(string_strv_hash_ops, char, string_hash_func, string_compare_func, free, char*, strv_free);