]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/strv.c
Merge pull request #2495 from heftig/master
[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 free(l);
91 return NULL;
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 * (const char*) -1. 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 == (const char*) -1 ? 0 : 1;
148
149 va_copy(aq, ap);
150 while ((s = va_arg(aq, const char*))) {
151 if (s == (const char*) -1)
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 != (const char*) -1) {
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 == (const char*) -1)
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 (n != 0)
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 (e != r)
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 free(buf);
430 return NULL;
431 }
432
433 int strv_push(char ***l, char *value) {
434 char **c;
435 unsigned n, m;
436
437 if (!value)
438 return 0;
439
440 n = strv_length(*l);
441
442 /* Increase and check for overflow */
443 m = n + 2;
444 if (m < n)
445 return -ENOMEM;
446
447 c = realloc_multiply(*l, sizeof(char*), m);
448 if (!c)
449 return -ENOMEM;
450
451 c[n] = value;
452 c[n+1] = NULL;
453
454 *l = c;
455 return 0;
456 }
457
458 int strv_push_pair(char ***l, char *a, char *b) {
459 char **c;
460 unsigned n, m;
461
462 if (!a && !b)
463 return 0;
464
465 n = strv_length(*l);
466
467 /* increase and check for overflow */
468 m = n + !!a + !!b + 1;
469 if (m < n)
470 return -ENOMEM;
471
472 c = realloc_multiply(*l, sizeof(char*), m);
473 if (!c)
474 return -ENOMEM;
475
476 if (a)
477 c[n++] = a;
478 if (b)
479 c[n++] = b;
480 c[n] = NULL;
481
482 *l = c;
483 return 0;
484 }
485
486 int strv_push_prepend(char ***l, char *value) {
487 char **c;
488 unsigned n, m, i;
489
490 if (!value)
491 return 0;
492
493 n = strv_length(*l);
494
495 /* increase and check for overflow */
496 m = n + 2;
497 if (m < n)
498 return -ENOMEM;
499
500 c = new(char*, m);
501 if (!c)
502 return -ENOMEM;
503
504 for (i = 0; i < n; i++)
505 c[i+1] = (*l)[i];
506
507 c[0] = value;
508 c[n+1] = NULL;
509
510 free(*l);
511 *l = c;
512
513 return 0;
514 }
515
516 int strv_consume(char ***l, char *value) {
517 int r;
518
519 r = strv_push(l, value);
520 if (r < 0)
521 free(value);
522
523 return r;
524 }
525
526 int strv_consume_pair(char ***l, char *a, char *b) {
527 int r;
528
529 r = strv_push_pair(l, a, b);
530 if (r < 0) {
531 free(a);
532 free(b);
533 }
534
535 return r;
536 }
537
538 int strv_consume_prepend(char ***l, char *value) {
539 int r;
540
541 r = strv_push_prepend(l, value);
542 if (r < 0)
543 free(value);
544
545 return r;
546 }
547
548 int strv_extend(char ***l, const char *value) {
549 char *v;
550
551 if (!value)
552 return 0;
553
554 v = strdup(value);
555 if (!v)
556 return -ENOMEM;
557
558 return strv_consume(l, v);
559 }
560
561 char **strv_uniq(char **l) {
562 char **i;
563
564 /* Drops duplicate entries. The first identical string will be
565 * kept, the others dropped */
566
567 STRV_FOREACH(i, l)
568 strv_remove(i+1, *i);
569
570 return l;
571 }
572
573 bool strv_is_uniq(char **l) {
574 char **i;
575
576 STRV_FOREACH(i, l)
577 if (strv_find(i+1, *i))
578 return false;
579
580 return true;
581 }
582
583 char **strv_remove(char **l, const char *s) {
584 char **f, **t;
585
586 if (!l)
587 return NULL;
588
589 assert(s);
590
591 /* Drops every occurrence of s in the string list, edits
592 * in-place. */
593
594 for (f = t = l; *f; f++)
595 if (streq(*f, s))
596 free(*f);
597 else
598 *(t++) = *f;
599
600 *t = NULL;
601 return l;
602 }
603
604 char **strv_parse_nulstr(const char *s, size_t l) {
605 const char *p;
606 unsigned c = 0, i = 0;
607 char **v;
608
609 assert(s || l <= 0);
610
611 if (l <= 0)
612 return new0(char*, 1);
613
614 for (p = s; p < s + l; p++)
615 if (*p == 0)
616 c++;
617
618 if (s[l-1] != 0)
619 c++;
620
621 v = new0(char*, c+1);
622 if (!v)
623 return NULL;
624
625 p = s;
626 while (p < s + l) {
627 const char *e;
628
629 e = memchr(p, 0, s + l - p);
630
631 v[i] = strndup(p, e ? e - p : s + l - p);
632 if (!v[i]) {
633 strv_free(v);
634 return NULL;
635 }
636
637 i++;
638
639 if (!e)
640 break;
641
642 p = e + 1;
643 }
644
645 assert(i == c);
646
647 return v;
648 }
649
650 char **strv_split_nulstr(const char *s) {
651 const char *i;
652 char **r = NULL;
653
654 NULSTR_FOREACH(i, s)
655 if (strv_extend(&r, i) < 0) {
656 strv_free(r);
657 return NULL;
658 }
659
660 if (!r)
661 return strv_new(NULL, NULL);
662
663 return r;
664 }
665
666 int strv_make_nulstr(char **l, char **p, size_t *q) {
667 size_t n_allocated = 0, n = 0;
668 _cleanup_free_ char *m = NULL;
669 char **i;
670
671 assert(p);
672 assert(q);
673
674 STRV_FOREACH(i, l) {
675 size_t z;
676
677 z = strlen(*i);
678
679 if (!GREEDY_REALLOC(m, n_allocated, n + z + 1))
680 return -ENOMEM;
681
682 memcpy(m + n, *i, z + 1);
683 n += z + 1;
684 }
685
686 if (!m) {
687 m = new0(char, 1);
688 if (!m)
689 return -ENOMEM;
690 n = 0;
691 }
692
693 *p = m;
694 *q = n;
695
696 m = NULL;
697
698 return 0;
699 }
700
701 bool strv_overlap(char **a, char **b) {
702 char **i;
703
704 STRV_FOREACH(i, a)
705 if (strv_contains(b, *i))
706 return true;
707
708 return false;
709 }
710
711 static int str_compare(const void *_a, const void *_b) {
712 const char **a = (const char**) _a, **b = (const char**) _b;
713
714 return strcmp(*a, *b);
715 }
716
717 char **strv_sort(char **l) {
718
719 if (strv_isempty(l))
720 return l;
721
722 qsort(l, strv_length(l), sizeof(char*), str_compare);
723 return l;
724 }
725
726 bool strv_equal(char **a, char **b) {
727
728 if (strv_isempty(a))
729 return strv_isempty(b);
730
731 if (strv_isempty(b))
732 return false;
733
734 for ( ; *a || *b; ++a, ++b)
735 if (!streq_ptr(*a, *b))
736 return false;
737
738 return true;
739 }
740
741 void strv_print(char **l) {
742 char **s;
743
744 STRV_FOREACH(s, l)
745 puts(*s);
746 }
747
748 int strv_extendf(char ***l, const char *format, ...) {
749 va_list ap;
750 char *x;
751 int r;
752
753 va_start(ap, format);
754 r = vasprintf(&x, format, ap);
755 va_end(ap);
756
757 if (r < 0)
758 return -ENOMEM;
759
760 return strv_consume(l, x);
761 }
762
763 char **strv_reverse(char **l) {
764 unsigned n, i;
765
766 n = strv_length(l);
767 if (n <= 1)
768 return l;
769
770 for (i = 0; i < n / 2; i++) {
771 char *t;
772
773 t = l[i];
774 l[i] = l[n-1-i];
775 l[n-1-i] = t;
776 }
777
778 return l;
779 }
780
781 char **strv_shell_escape(char **l, const char *bad) {
782 char **s;
783
784 /* Escapes every character in every string in l that is in bad,
785 * edits in-place, does not roll-back on error. */
786
787 STRV_FOREACH(s, l) {
788 char *v;
789
790 v = shell_escape(*s, bad);
791 if (!v)
792 return NULL;
793
794 free(*s);
795 *s = v;
796 }
797
798 return l;
799 }
800
801 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
802 char* const* p;
803
804 STRV_FOREACH(p, patterns)
805 if (fnmatch(*p, s, 0) == 0)
806 return true;
807
808 return false;
809 }
810
811 char ***strv_free_free(char ***l) {
812 char ***i;
813
814 if (!l)
815 return NULL;
816
817 for (i = l; *i; i++)
818 strv_free(*i);
819
820 free(l);
821 return NULL;
822 }
823
824 char **strv_skip(char **l, size_t n) {
825
826 while (n > 0) {
827 if (strv_isempty(l))
828 return l;
829
830 l++, n--;
831 }
832
833 return l;
834 }
835
836 int strv_extend_n(char ***l, const char *value, size_t n) {
837 size_t i, j, k;
838 char **nl;
839
840 assert(l);
841
842 if (!value)
843 return 0;
844 if (n == 0)
845 return 0;
846
847 /* Adds the value value n times to l */
848
849 k = strv_length(*l);
850
851 nl = realloc(*l, sizeof(char*) * (k + n + 1));
852 if (!nl)
853 return -ENOMEM;
854
855 *l = nl;
856
857 for (i = k; i < k + n; i++) {
858 nl[i] = strdup(value);
859 if (!nl[i])
860 goto rollback;
861 }
862
863 nl[i] = NULL;
864 return 0;
865
866 rollback:
867 for (j = k; j < i; j++)
868 free(nl[j]);
869
870 nl[k] = NULL;
871 return -ENOMEM;
872 }
873
874 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
875 bool b = false;
876 char **s;
877 int r;
878
879 /* Like fputs(), but for strv, and with a less stupid argument order */
880
881 if (!space)
882 space = &b;
883
884 STRV_FOREACH(s, l) {
885 r = fputs_with_space(f, *s, separator, space);
886 if (r < 0)
887 return r;
888 }
889
890 return 0;
891 }