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