chiark / gitweb /
basic/strv: fix strv_join for first empty argument
[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          * (const char*) -1. 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 == (const char*) -1 ? 0 : 1;
148
149                 va_copy(aq, ap);
150                 while ((s = va_arg(aq, const char*))) {
151                         if (s == (const char*) -1)
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 != (const char*) -1) {
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 == (const char*) -1)
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 char **strv_uniq(char **l) {
570         char **i;
571
572         /* Drops duplicate entries. The first identical string will be
573          * kept, the others dropped */
574
575         STRV_FOREACH(i, l)
576                 strv_remove(i+1, *i);
577
578         return l;
579 }
580
581 #if 0 /// UNNEEDED by elogind
582 bool strv_is_uniq(char **l) {
583         char **i;
584
585         STRV_FOREACH(i, l)
586                 if (strv_find(i+1, *i))
587                         return false;
588
589         return true;
590 }
591 #endif // 0
592
593 char **strv_remove(char **l, const char *s) {
594         char **f, **t;
595
596         if (!l)
597                 return NULL;
598
599         assert(s);
600
601         /* Drops every occurrence of s in the string list, edits
602          * in-place. */
603
604         for (f = t = l; *f; f++)
605                 if (streq(*f, s))
606                         free(*f);
607                 else
608                         *(t++) = *f;
609
610         *t = NULL;
611         return l;
612 }
613
614 char **strv_parse_nulstr(const char *s, size_t l) {
615         const char *p;
616         unsigned c = 0, i = 0;
617         char **v;
618
619         assert(s || l <= 0);
620
621         if (l <= 0)
622                 return new0(char*, 1);
623
624         for (p = s; p < s + l; p++)
625                 if (*p == 0)
626                         c++;
627
628         if (s[l-1] != 0)
629                 c++;
630
631         v = new0(char*, c+1);
632         if (!v)
633                 return NULL;
634
635         p = s;
636         while (p < s + l) {
637                 const char *e;
638
639                 e = memchr(p, 0, s + l - p);
640
641                 v[i] = strndup(p, e ? e - p : s + l - p);
642                 if (!v[i]) {
643                         strv_free(v);
644                         return NULL;
645                 }
646
647                 i++;
648
649                 if (!e)
650                         break;
651
652                 p = e + 1;
653         }
654
655         assert(i == c);
656
657         return v;
658 }
659
660 char **strv_split_nulstr(const char *s) {
661         const char *i;
662         char **r = NULL;
663
664         NULSTR_FOREACH(i, s)
665                 if (strv_extend(&r, i) < 0) {
666                         strv_free(r);
667                         return NULL;
668                 }
669
670         if (!r)
671                 return strv_new(NULL, NULL);
672
673         return r;
674 }
675
676 #if 0 /// UNNEEDED by elogind
677 int strv_make_nulstr(char **l, char **p, size_t *q) {
678         size_t n_allocated = 0, n = 0;
679         _cleanup_free_ char *m = NULL;
680         char **i;
681
682         assert(p);
683         assert(q);
684
685         STRV_FOREACH(i, l) {
686                 size_t z;
687
688                 z = strlen(*i);
689
690                 if (!GREEDY_REALLOC(m, n_allocated, n + z + 1))
691                         return -ENOMEM;
692
693                 memcpy(m + n, *i, z + 1);
694                 n += z + 1;
695         }
696
697         if (!m) {
698                 m = new0(char, 1);
699                 if (!m)
700                         return -ENOMEM;
701                 n = 0;
702         }
703
704         *p = m;
705         *q = n;
706
707         m = NULL;
708
709         return 0;
710 }
711
712 bool strv_overlap(char **a, char **b) {
713         char **i;
714
715         STRV_FOREACH(i, a)
716                 if (strv_contains(b, *i))
717                         return true;
718
719         return false;
720 }
721 #endif // 0
722
723 static int str_compare(const void *_a, const void *_b) {
724         const char **a = (const char**) _a, **b = (const char**) _b;
725
726         return strcmp(*a, *b);
727 }
728
729 char **strv_sort(char **l) {
730
731         if (strv_isempty(l))
732                 return l;
733
734         qsort(l, strv_length(l), sizeof(char*), str_compare);
735         return l;
736 }
737
738 #if 0 /// UNNEEDED by elogind
739 bool strv_equal(char **a, char **b) {
740
741         if (strv_isempty(a))
742                 return strv_isempty(b);
743
744         if (strv_isempty(b))
745                 return false;
746
747         for ( ; *a || *b; ++a, ++b)
748                 if (!streq_ptr(*a, *b))
749                         return false;
750
751         return true;
752 }
753
754 void strv_print(char **l) {
755         char **s;
756
757         STRV_FOREACH(s, l)
758                 puts(*s);
759 }
760
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         unsigned 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                 char *t;
785
786                 t = l[i];
787                 l[i] = l[n-1-i];
788                 l[n-1-i] = t;
789         }
790
791         return l;
792 }
793
794 char **strv_shell_escape(char **l, const char *bad) {
795         char **s;
796
797         /* Escapes every character in every string in l that is in bad,
798          * edits in-place, does not roll-back on error. */
799
800         STRV_FOREACH(s, l) {
801                 char *v;
802
803                 v = shell_escape(*s, bad);
804                 if (!v)
805                         return NULL;
806
807                 free(*s);
808                 *s = v;
809         }
810
811         return l;
812 }
813
814 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
815         char* const* p;
816
817         STRV_FOREACH(p, patterns)
818                 if (fnmatch(*p, s, 0) == 0)
819                         return true;
820
821         return false;
822 }
823
824 char ***strv_free_free(char ***l) {
825         char ***i;
826
827         if (!l)
828                 return NULL;
829
830         for (i = l; *i; i++)
831                 strv_free(*i);
832
833         free(l);
834         return NULL;
835 }
836
837 char **strv_skip(char **l, size_t n) {
838
839         while (n > 0) {
840                 if (strv_isempty(l))
841                         return l;
842
843                 l++, n--;
844         }
845
846         return l;
847 }
848
849 int strv_extend_n(char ***l, const char *value, size_t n) {
850         size_t i, j, k;
851         char **nl;
852
853         assert(l);
854
855         if (!value)
856                 return 0;
857         if (n == 0)
858                 return 0;
859
860         /* Adds the value value n times to l */
861
862         k = strv_length(*l);
863
864         nl = realloc(*l, sizeof(char*) * (k + n + 1));
865         if (!nl)
866                 return -ENOMEM;
867
868         *l = nl;
869
870         for (i = k; i < k + n; i++) {
871                 nl[i] = strdup(value);
872                 if (!nl[i])
873                         goto rollback;
874         }
875
876         nl[i] = NULL;
877         return 0;
878
879 rollback:
880         for (j = k; j < i; j++)
881                 free(nl[j]);
882
883         nl[k] = NULL;
884         return -ENOMEM;
885 }
886
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