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