]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/strv.c
sysusers: allow the shell to be specified
[thirdparty/systemd.git] / src / basic / strv.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09
LP
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
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 15 Lesser General Public License for more details.
a7334b09 16
5430f7f2 17 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
07630cea 21#include <errno.h>
11c3a366 22#include <fnmatch.h>
60918275 23#include <stdarg.h>
11c3a366 24#include <stdio.h>
07630cea 25#include <stdlib.h>
60918275
LP
26#include <string.h>
27
b5efdb8a 28#include "alloc-util.h"
4f5dd394 29#include "escape.h"
11c3a366 30#include "extract-word.h"
d390f8ef 31#include "fileio.h"
07630cea 32#include "string-util.h"
60918275 33#include "strv.h"
cf0fbc49 34#include "util.h"
60918275
LP
35
36char *strv_find(char **l, const char *name) {
5f9a22c3
LP
37 char **i;
38
60918275
LP
39 assert(name);
40
5f9a22c3
LP
41 STRV_FOREACH(i, l)
42 if (streq(*i, name))
43 return *i;
60918275
LP
44
45 return NULL;
46}
47
a4bfb399
LP
48char *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
28849dba
LP
60char *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
dd9c7723 77void strv_clear(char **l) {
60918275
LP
78 char **k;
79
80 if (!l)
81 return;
82
83 for (k = l; *k; k++)
84 free(*k);
85
dd9c7723
TG
86 *l = NULL;
87}
88
33c2ce7b 89char **strv_free(char **l) {
dd9c7723 90 strv_clear(l);
6b430fdb 91 return mfree(l);
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 141 /* As a special trick we ignore all listed strings that equal
f9d14060 142 * STRV_IGNORE. This is supposed to be used with the
07719a21
LP
143 * STRV_IFNOTNULL() macro to include possibly NULL strings in
144 * the string list. */
145
60918275 146 if (x) {
f9d14060 147 n = x == STRV_IGNORE ? 0 : 1;
60918275 148
257eca1a 149 va_copy(aq, ap);
07719a21 150 while ((s = va_arg(aq, const char*))) {
f9d14060 151 if (s == STRV_IGNORE)
07719a21
LP
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) {
f9d14060 165 if (x != STRV_IGNORE) {
07719a21
LP
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 173
f9d14060 174 if (s == STRV_IGNORE)
07719a21
LP
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) {
afe773b0 374 if (s != l)
5f9a22c3
LP
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) {
afe773b0 385 if (s != l)
5f9a22c3
LP
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
4468addc 396int strv_push(char ***l, char *value) {
5926ccca 397 char **c;
97569e15 398 unsigned n, m;
5926ccca
LP
399
400 if (!value)
401 return 0;
402
82dde599 403 n = strv_length(*l);
97569e15 404
98940a3c 405 /* Increase and check for overflow */
97569e15
LP
406 m = n + 2;
407 if (m < n)
408 return -ENOMEM;
409
14f27b4e 410 c = realloc_multiply(*l, sizeof(char*), m);
4468addc 411 if (!c)
82dde599 412 return -ENOMEM;
82dde599 413
4468addc 414 c[n] = value;
82dde599
LP
415 c[n+1] = NULL;
416
5926ccca
LP
417 *l = c;
418 return 0;
419}
420
98940a3c
LP
421int strv_push_pair(char ***l, char *a, char *b) {
422 char **c;
423 unsigned n, m;
424
425 if (!a && !b)
426 return 0;
427
428 n = strv_length(*l);
429
430 /* increase and check for overflow */
431 m = n + !!a + !!b + 1;
432 if (m < n)
433 return -ENOMEM;
434
435 c = realloc_multiply(*l, sizeof(char*), m);
436 if (!c)
437 return -ENOMEM;
438
439 if (a)
440 c[n++] = a;
441 if (b)
442 c[n++] = b;
443 c[n] = NULL;
444
445 *l = c;
446 return 0;
447}
448
9a00f57a
LP
449int strv_push_prepend(char ***l, char *value) {
450 char **c;
97569e15 451 unsigned n, m, i;
9a00f57a
LP
452
453 if (!value)
454 return 0;
455
456 n = strv_length(*l);
97569e15
LP
457
458 /* increase and check for overflow */
459 m = n + 2;
460 if (m < n)
461 return -ENOMEM;
462
463 c = new(char*, m);
9a00f57a
LP
464 if (!c)
465 return -ENOMEM;
466
467 for (i = 0; i < n; i++)
468 c[i+1] = (*l)[i];
469
470 c[0] = value;
471 c[n+1] = NULL;
472
473 free(*l);
474 *l = c;
475
476 return 0;
477}
478
6e18964d
ZJS
479int strv_consume(char ***l, char *value) {
480 int r;
481
482 r = strv_push(l, value);
483 if (r < 0)
484 free(value);
485
9a00f57a
LP
486 return r;
487}
488
98940a3c
LP
489int 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
9a00f57a
LP
501int 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
6e18964d
ZJS
508 return r;
509}
510
4468addc
LP
511int strv_extend(char ***l, const char *value) {
512 char *v;
4468addc
LP
513
514 if (!value)
515 return 0;
516
517 v = strdup(value);
518 if (!v)
519 return -ENOMEM;
520
6e18964d 521 return strv_consume(l, v);
4468addc
LP
522}
523
4f4afc88
LP
524int 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
bcab914f
LP
532 if (!value)
533 return 0;
534
4f4afc88
LP
535 n = strv_length(*l);
536
537 /* Increase and overflow check. */
538 m = n + 2;
539 if (m < n)
540 return -ENOMEM;
541
bcab914f
LP
542 v = strdup(value);
543 if (!v)
544 return -ENOMEM;
4f4afc88
LP
545
546 c = realloc_multiply(*l, sizeof(char*), m);
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
5f9a22c3 560char **strv_uniq(char **l) {
cba8922f
LP
561 char **i;
562
5f9a22c3
LP
563 /* Drops duplicate entries. The first identical string will be
564 * kept, the others dropped */
565
cba8922f 566 STRV_FOREACH(i, l)
5f9a22c3
LP
567 strv_remove(i+1, *i);
568
569 return l;
570}
571
e1dd6790
LP
572bool strv_is_uniq(char **l) {
573 char **i;
574
575 STRV_FOREACH(i, l)
576 if (strv_find(i+1, *i))
577 return false;
578
579 return true;
580}
581
5f9a22c3
LP
582char **strv_remove(char **l, const char *s) {
583 char **f, **t;
584
585 if (!l)
586 return NULL;
587
5d6ab905
LP
588 assert(s);
589
590 /* Drops every occurrence of s in the string list, edits
591 * in-place. */
5f9a22c3 592
e3e45d4f
SP
593 for (f = t = l; *f; f++)
594 if (streq(*f, s))
71ecc858 595 free(*f);
e3e45d4f
SP
596 else
597 *(t++) = *f;
71ecc858
LP
598
599 *t = NULL;
600 return l;
601}
602
21bc923a 603char **strv_parse_nulstr(const char *s, size_t l) {
b60df13b
ZJS
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
21bc923a
LP
615 const char *p;
616 unsigned c = 0, i = 0;
617 char **v;
618
619 assert(s || l <= 0);
620
621 if (l <= 0)
49b832c5 622 return new0(char*, 1);
21bc923a
LP
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
1fd8d04e
LP
631 v = new0(char*, c+1);
632 if (!v)
21bc923a
LP
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
1fd8d04e
LP
641 v[i] = strndup(p, e ? e - p : s + l - p);
642 if (!v[i]) {
21bc923a
LP
643 strv_free(v);
644 return NULL;
645 }
646
1fd8d04e
LP
647 i++;
648
21bc923a
LP
649 if (!e)
650 break;
651
652 p = e + 1;
653 }
654
655 assert(i == c);
656
657 return v;
658}
0c85a4f3 659
fabe5c0e
LP
660char **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, NULL);
672
673 return r;
674}
675
e287086b 676int strv_make_nulstr(char **l, char **p, size_t *q) {
b60df13b
ZJS
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
e287086b
LP
684 size_t n_allocated = 0, n = 0;
685 _cleanup_free_ char *m = NULL;
686 char **i;
687
688 assert(p);
689 assert(q);
690
691 STRV_FOREACH(i, l) {
692 size_t z;
693
694 z = strlen(*i);
695
b60df13b 696 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
e287086b
LP
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;
b60df13b
ZJS
707 n = 1;
708 } else
709 /* make sure there is a second extra NUL at the end of resulting nulstr */
710 m[n] = '\0';
e287086b 711
b60df13b 712 assert(n > 0);
e287086b 713 *p = m;
b60df13b 714 *q = n - 1;
e287086b
LP
715
716 m = NULL;
717
718 return 0;
719}
720
0c85a4f3 721bool strv_overlap(char **a, char **b) {
e3e45d4f 722 char **i;
0c85a4f3 723
e3e45d4f
SP
724 STRV_FOREACH(i, a)
725 if (strv_contains(b, *i))
726 return true;
0c85a4f3
LP
727
728 return false;
729}
857a493d
LP
730
731static int str_compare(const void *_a, const void *_b) {
732 const char **a = (const char**) _a, **b = (const char**) _b;
733
734 return strcmp(*a, *b);
735}
736
737char **strv_sort(char **l) {
f6d703c3 738 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
857a493d
LP
739 return l;
740}
7c2d8094 741
0f84a72e 742bool strv_equal(char **a, char **b) {
e287086b
LP
743
744 if (strv_isempty(a))
745 return strv_isempty(b);
746
747 if (strv_isempty(b))
748 return false;
0f84a72e
DH
749
750 for ( ; *a || *b; ++a, ++b)
751 if (!streq_ptr(*a, *b))
752 return false;
753
754 return true;
755}
756
7c2d8094
TA
757void strv_print(char **l) {
758 char **s;
759
7c2d8094
TA
760 STRV_FOREACH(s, l)
761 puts(*s);
762}
4de33e7f
LP
763
764int strv_extendf(char ***l, const char *format, ...) {
765 va_list ap;
766 char *x;
767 int r;
768
769 va_start(ap, format);
770 r = vasprintf(&x, format, ap);
771 va_end(ap);
772
773 if (r < 0)
774 return -ENOMEM;
775
776 return strv_consume(l, x);
777}
e1dd6790
LP
778
779char **strv_reverse(char **l) {
780 unsigned n, i;
781
782 n = strv_length(l);
783 if (n <= 1)
784 return l;
785
fc549b96 786 for (i = 0; i < n / 2; i++)
8a3134b2 787 SWAP_TWO(l[i], l[n-1-i]);
e1dd6790
LP
788
789 return l;
790}
bceccd5e 791
04c14b25
RM
792char **strv_shell_escape(char **l, const char *bad) {
793 char **s;
794
795 /* Escapes every character in every string in l that is in bad,
796 * edits in-place, does not roll-back on error. */
797
798 STRV_FOREACH(s, l) {
799 char *v;
800
801 v = shell_escape(*s, bad);
802 if (!v)
803 return NULL;
804
805 free(*s);
806 *s = v;
807 }
808
809 return l;
810}
811
2404701e 812bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
bceccd5e
ZJS
813 char* const* p;
814
815 STRV_FOREACH(p, patterns)
2027927b 816 if (fnmatch(*p, s, flags) == 0)
bceccd5e
ZJS
817 return true;
818
819 return false;
820}
fe382237
LP
821
822char ***strv_free_free(char ***l) {
823 char ***i;
824
825 if (!l)
826 return NULL;
827
828 for (i = l; *i; i++)
829 strv_free(*i);
830
6b430fdb 831 return mfree(l);
fe382237 832}
e3ead6bb
LP
833
834char **strv_skip(char **l, size_t n) {
835
836 while (n > 0) {
837 if (strv_isempty(l))
838 return l;
839
840 l++, n--;
841 }
842
843 return l;
844}
8dd4c05b
LP
845
846int strv_extend_n(char ***l, const char *value, size_t n) {
847 size_t i, j, k;
848 char **nl;
849
850 assert(l);
851
852 if (!value)
853 return 0;
854 if (n == 0)
855 return 0;
856
61233823 857 /* Adds the value n times to l */
8dd4c05b
LP
858
859 k = strv_length(*l);
860
861 nl = realloc(*l, sizeof(char*) * (k + n + 1));
862 if (!nl)
863 return -ENOMEM;
864
865 *l = nl;
866
867 for (i = k; i < k + n; i++) {
868 nl[i] = strdup(value);
869 if (!nl[i])
870 goto rollback;
871 }
872
873 nl[i] = NULL;
874 return 0;
875
876rollback:
6fff8ac4 877 for (j = k; j < i; j++)
8dd4c05b
LP
878 free(nl[j]);
879
880 nl[k] = NULL;
5b700370 881 return -ENOMEM;
8dd4c05b 882}
3df9bec5
LP
883
884int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
885 bool b = false;
886 char **s;
887 int r;
888
889 /* Like fputs(), but for strv, and with a less stupid argument order */
890
3df9bec5
LP
891 if (!space)
892 space = &b;
893
894 STRV_FOREACH(s, l) {
d390f8ef 895 r = fputs_with_space(f, *s, separator, space);
3df9bec5
LP
896 if (r < 0)
897 return r;
3df9bec5
LP
898 }
899
900 return 0;
901}