]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/strv.c
Merge pull request #2603 from poettering/drop-compat-libs
[thirdparty/systemd.git] / src / basic / strv.c
CommitLineData
a7334b09
LP
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
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
07630cea 20#include <errno.h>
11c3a366 21#include <fnmatch.h>
60918275 22#include <stdarg.h>
11c3a366 23#include <stdio.h>
07630cea 24#include <stdlib.h>
60918275
LP
25#include <string.h>
26
b5efdb8a 27#include "alloc-util.h"
4f5dd394 28#include "escape.h"
11c3a366 29#include "extract-word.h"
d390f8ef 30#include "fileio.h"
07630cea 31#include "string-util.h"
60918275 32#include "strv.h"
cf0fbc49 33#include "util.h"
60918275
LP
34
35char *strv_find(char **l, const char *name) {
5f9a22c3
LP
36 char **i;
37
60918275
LP
38 assert(name);
39
5f9a22c3
LP
40 STRV_FOREACH(i, l)
41 if (streq(*i, name))
42 return *i;
60918275
LP
43
44 return NULL;
45}
46
a4bfb399
LP
47char *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
28849dba
LP
59char *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
dd9c7723 76void strv_clear(char **l) {
60918275
LP
77 char **k;
78
79 if (!l)
80 return;
81
82 for (k = l; *k; k++)
83 free(*k);
84
dd9c7723
TG
85 *l = NULL;
86}
87
33c2ce7b 88char **strv_free(char **l) {
dd9c7723 89 strv_clear(l);
60918275 90 free(l);
33c2ce7b 91 return NULL;
60918275
LP
92}
93
ab84f5b9
ZJS
94char **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
2fd9ae2e 103char **strv_copy(char * const *l) {
60918275
LP
104 char **r, **k;
105
1fd8d04e
LP
106 k = r = new(char*, strv_length(l) + 1);
107 if (!r)
60918275
LP
108 return NULL;
109
ede27aab 110 if (l)
1fd8d04e
LP
111 for (; *l; k++, l++) {
112 *k = strdup(*l);
113 if (!*k) {
114 strv_free(r);
115 return NULL;
116 }
117 }
60918275
LP
118
119 *k = NULL;
120 return r;
60918275
LP
121}
122
2fd9ae2e 123unsigned strv_length(char * const *l) {
60918275
LP
124 unsigned n = 0;
125
126 if (!l)
127 return 0;
128
129 for (; *l; l++)
130 n++;
131
132 return n;
133}
134
257eca1a 135char **strv_new_ap(const char *x, va_list ap) {
60918275
LP
136 const char *s;
137 char **a;
138 unsigned n = 0, i = 0;
257eca1a
LP
139 va_list aq;
140
07719a21
LP
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
60918275 146 if (x) {
07719a21 147 n = x == (const char*) -1 ? 0 : 1;
60918275 148
257eca1a 149 va_copy(aq, ap);
07719a21
LP
150 while ((s = va_arg(aq, const char*))) {
151 if (s == (const char*) -1)
152 continue;
153
60918275 154 n++;
07719a21
LP
155 }
156
257eca1a 157 va_end(aq);
60918275
LP
158 }
159
07719a21
LP
160 a = new(char*, n+1);
161 if (!a)
60918275
LP
162 return NULL;
163
164 if (x) {
07719a21
LP
165 if (x != (const char*) -1) {
166 a[i] = strdup(x);
167 if (!a[i])
168 goto fail;
169 i++;
60918275
LP
170 }
171
60918275 172 while ((s = va_arg(ap, const char*))) {
07719a21
LP
173
174 if (s == (const char*) -1)
175 continue;
176
177 a[i] = strdup(s);
178 if (!a[i])
60918275
LP
179 goto fail;
180
181 i++;
182 }
60918275
LP
183 }
184
185 a[i] = NULL;
257eca1a 186
60918275
LP
187 return a;
188
189fail:
1fd8d04e 190 strv_free(a);
60918275
LP
191 return NULL;
192}
034c6ed7 193
257eca1a
LP
194char **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
e287086b
LP
205int 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;
07719a21 223
e3e45d4f 224 STRV_FOREACH(s, b) {
e287086b
LP
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;
07719a21 235 }
034c6ed7 236
e287086b
LP
237 assert(i <= q);
238
239 return (int) i;
240
241rollback:
242 for (j = 0; j < i; j++)
243 free(t[p + j]);
244
245 t[p] = NULL;
246 return -ENOMEM;
5f9a22c3
LP
247}
248
e3e45d4f
SP
249int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
250 int r;
251 char **s;
5f9a22c3 252
e3e45d4f
SP
253 STRV_FOREACH(s, b) {
254 char *v;
5f9a22c3 255
e3e45d4f
SP
256 v = strappend(*s, suffix);
257 if (!v)
258 return -ENOMEM;
5f9a22c3 259
e3e45d4f
SP
260 r = strv_push(a, v);
261 if (r < 0) {
262 free(v);
263 return r;
8ea913b2 264 }
8ea913b2 265 }
5f9a22c3 266
e3e45d4f 267 return 0;
5f9a22c3
LP
268}
269
270char **strv_split(const char *s, const char *separator) {
a2a5291b 271 const char *word, *state;
5f9a22c3
LP
272 size_t l;
273 unsigned n, i;
274 char **r;
275
276 assert(s);
277
278 n = 0;
a2a5291b 279 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
5f9a22c3
LP
280 n++;
281
1fd8d04e
LP
282 r = new(char*, n+1);
283 if (!r)
5f9a22c3
LP
284 return NULL;
285
286 i = 0;
a2a5291b
ZJS
287 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
288 r[i] = strndup(word, l);
1fd8d04e 289 if (!r[i]) {
5f9a22c3
LP
290 strv_free(r);
291 return NULL;
292 }
293
1fd8d04e
LP
294 i++;
295 }
296
5f9a22c3
LP
297 r[i] = NULL;
298 return r;
299}
300
26d04f86
LP
301char **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
ece174c5 318 if (isempty(l[n - 1]))
a1e58e8e 319 l[n - 1] = mfree(l[n - 1]);
26d04f86
LP
320
321 return l;
322}
323
8adaf7bd 324int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
f88e6be5 325 _cleanup_strv_free_ char **l = NULL;
8dd4c05b 326 size_t n = 0, allocated = 0;
f88e6be5
LP
327 int r;
328
329 assert(t);
330 assert(s);
331
332 for (;;) {
333 _cleanup_free_ char *word = NULL;
334
8adaf7bd 335 r = extract_first_word(&s, &word, separators, flags);
f88e6be5
LP
336 if (r < 0)
337 return r;
ece174c5 338 if (r == 0)
f88e6be5
LP
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
8dd4c05b 350 if (!l) {
f88e6be5 351 l = new0(char*, 1);
8dd4c05b
LP
352 if (!l)
353 return -ENOMEM;
354 }
f88e6be5
LP
355
356 *t = l;
357 l = NULL;
358
8dd4c05b 359 return (int) n;
f88e6be5
LP
360}
361
5f9a22c3
LP
362char *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
1fd8d04e
LP
379 r = new(char, n+1);
380 if (!r)
5f9a22c3
LP
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
8d49745c
LP
391 *e = 0;
392
5f9a22c3
LP
393 return r;
394}
395
a6fde353
ZJS
396char *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
4468addc 433int strv_push(char ***l, char *value) {
5926ccca 434 char **c;
97569e15 435 unsigned n, m;
5926ccca
LP
436
437 if (!value)
438 return 0;
439
82dde599 440 n = strv_length(*l);
97569e15 441
98940a3c 442 /* Increase and check for overflow */
97569e15
LP
443 m = n + 2;
444 if (m < n)
445 return -ENOMEM;
446
14f27b4e 447 c = realloc_multiply(*l, sizeof(char*), m);
4468addc 448 if (!c)
82dde599 449 return -ENOMEM;
82dde599 450
4468addc 451 c[n] = value;
82dde599
LP
452 c[n+1] = NULL;
453
5926ccca
LP
454 *l = c;
455 return 0;
456}
457
98940a3c
LP
458int 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
9a00f57a
LP
486int strv_push_prepend(char ***l, char *value) {
487 char **c;
97569e15 488 unsigned n, m, i;
9a00f57a
LP
489
490 if (!value)
491 return 0;
492
493 n = strv_length(*l);
97569e15
LP
494
495 /* increase and check for overflow */
496 m = n + 2;
497 if (m < n)
498 return -ENOMEM;
499
500 c = new(char*, m);
9a00f57a
LP
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
6e18964d
ZJS
516int strv_consume(char ***l, char *value) {
517 int r;
518
519 r = strv_push(l, value);
520 if (r < 0)
521 free(value);
522
9a00f57a
LP
523 return r;
524}
525
98940a3c
LP
526int 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
9a00f57a
LP
538int 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
6e18964d
ZJS
545 return r;
546}
547
4468addc
LP
548int strv_extend(char ***l, const char *value) {
549 char *v;
4468addc
LP
550
551 if (!value)
552 return 0;
553
554 v = strdup(value);
555 if (!v)
556 return -ENOMEM;
557
6e18964d 558 return strv_consume(l, v);
4468addc
LP
559}
560
5f9a22c3 561char **strv_uniq(char **l) {
cba8922f
LP
562 char **i;
563
5f9a22c3
LP
564 /* Drops duplicate entries. The first identical string will be
565 * kept, the others dropped */
566
cba8922f 567 STRV_FOREACH(i, l)
5f9a22c3
LP
568 strv_remove(i+1, *i);
569
570 return l;
571}
572
e1dd6790
LP
573bool 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
5f9a22c3
LP
583char **strv_remove(char **l, const char *s) {
584 char **f, **t;
585
586 if (!l)
587 return NULL;
588
5d6ab905
LP
589 assert(s);
590
591 /* Drops every occurrence of s in the string list, edits
592 * in-place. */
5f9a22c3 593
e3e45d4f
SP
594 for (f = t = l; *f; f++)
595 if (streq(*f, s))
71ecc858 596 free(*f);
e3e45d4f
SP
597 else
598 *(t++) = *f;
71ecc858
LP
599
600 *t = NULL;
601 return l;
602}
603
21bc923a
LP
604char **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)
49b832c5 612 return new0(char*, 1);
21bc923a
LP
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
1fd8d04e
LP
621 v = new0(char*, c+1);
622 if (!v)
21bc923a
LP
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
1fd8d04e
LP
631 v[i] = strndup(p, e ? e - p : s + l - p);
632 if (!v[i]) {
21bc923a
LP
633 strv_free(v);
634 return NULL;
635 }
636
1fd8d04e
LP
637 i++;
638
21bc923a
LP
639 if (!e)
640 break;
641
642 p = e + 1;
643 }
644
645 assert(i == c);
646
647 return v;
648}
0c85a4f3 649
fabe5c0e
LP
650char **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
e287086b
LP
666int 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
0c85a4f3 701bool strv_overlap(char **a, char **b) {
e3e45d4f 702 char **i;
0c85a4f3 703
e3e45d4f
SP
704 STRV_FOREACH(i, a)
705 if (strv_contains(b, *i))
706 return true;
0c85a4f3
LP
707
708 return false;
709}
857a493d
LP
710
711static 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
717char **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}
7c2d8094 725
0f84a72e 726bool strv_equal(char **a, char **b) {
e287086b
LP
727
728 if (strv_isempty(a))
729 return strv_isempty(b);
730
731 if (strv_isempty(b))
732 return false;
0f84a72e
DH
733
734 for ( ; *a || *b; ++a, ++b)
735 if (!streq_ptr(*a, *b))
736 return false;
737
738 return true;
739}
740
7c2d8094
TA
741void strv_print(char **l) {
742 char **s;
743
7c2d8094
TA
744 STRV_FOREACH(s, l)
745 puts(*s);
746}
4de33e7f
LP
747
748int 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}
e1dd6790
LP
762
763char **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}
bceccd5e 780
04c14b25
RM
781char **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
2404701e 801bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
bceccd5e
ZJS
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}
fe382237
LP
810
811char ***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}
e3ead6bb
LP
823
824char **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}
8dd4c05b
LP
835
836int 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
866rollback:
6fff8ac4 867 for (j = k; j < i; j++)
8dd4c05b
LP
868 free(nl[j]);
869
870 nl[k] = NULL;
5b700370 871 return -ENOMEM;
8dd4c05b 872}
3df9bec5
LP
873
874int 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
3df9bec5
LP
881 if (!space)
882 space = &b;
883
884 STRV_FOREACH(s, l) {
d390f8ef 885 r = fputs_with_space(f, *s, separator, space);
3df9bec5
LP
886 if (r < 0)
887 return r;
3df9bec5
LP
888 }
889
890 return 0;
891}