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