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