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