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