chiark / gitweb /
copy: adjust directory times after writing to the directory
[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 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         const char *p;
652         unsigned c = 0, i = 0;
653         char **v;
654
655         assert(s || l <= 0);
656
657         if (l <= 0)
658                 return new0(char*, 1);
659
660         for (p = s; p < s + l; p++)
661                 if (*p == 0)
662                         c++;
663
664         if (s[l-1] != 0)
665                 c++;
666
667         v = new0(char*, c+1);
668         if (!v)
669                 return NULL;
670
671         p = s;
672         while (p < s + l) {
673                 const char *e;
674
675                 e = memchr(p, 0, s + l - p);
676
677                 v[i] = strndup(p, e ? e - p : s + l - p);
678                 if (!v[i]) {
679                         strv_free(v);
680                         return NULL;
681                 }
682
683                 i++;
684
685                 if (!e)
686                         break;
687
688                 p = e + 1;
689         }
690
691         assert(i == c);
692
693         return v;
694 }
695
696 char **strv_split_nulstr(const char *s) {
697         const char *i;
698         char **r = NULL;
699
700         NULSTR_FOREACH(i, s)
701                 if (strv_extend(&r, i) < 0) {
702                         strv_free(r);
703                         return NULL;
704                 }
705
706         if (!r)
707                 return strv_new(NULL, NULL);
708
709         return r;
710 }
711
712 #if 0 /// UNNEEDED by elogind
713 int strv_make_nulstr(char **l, char **p, size_t *q) {
714         size_t n_allocated = 0, n = 0;
715         _cleanup_free_ char *m = NULL;
716         char **i;
717
718         assert(p);
719         assert(q);
720
721         STRV_FOREACH(i, l) {
722                 size_t z;
723
724                 z = strlen(*i);
725
726                 if (!GREEDY_REALLOC(m, n_allocated, n + z + 1))
727                         return -ENOMEM;
728
729                 memcpy(m + n, *i, z + 1);
730                 n += z + 1;
731         }
732
733         if (!m) {
734                 m = new0(char, 1);
735                 if (!m)
736                         return -ENOMEM;
737                 n = 0;
738         }
739
740         *p = m;
741         *q = n;
742
743         m = NULL;
744
745         return 0;
746 }
747
748 bool strv_overlap(char **a, char **b) {
749         char **i;
750
751         STRV_FOREACH(i, a)
752                 if (strv_contains(b, *i))
753                         return true;
754
755         return false;
756 }
757 #endif // 0
758
759 static int str_compare(const void *_a, const void *_b) {
760         const char **a = (const char**) _a, **b = (const char**) _b;
761
762         return strcmp(*a, *b);
763 }
764
765 char **strv_sort(char **l) {
766
767         if (strv_isempty(l))
768                 return l;
769
770         qsort(l, strv_length(l), sizeof(char*), str_compare);
771         return l;
772 }
773
774 #if 0 /// UNNEEDED by elogind
775 bool strv_equal(char **a, char **b) {
776
777         if (strv_isempty(a))
778                 return strv_isempty(b);
779
780         if (strv_isempty(b))
781                 return false;
782
783         for ( ; *a || *b; ++a, ++b)
784                 if (!streq_ptr(*a, *b))
785                         return false;
786
787         return true;
788 }
789
790 void strv_print(char **l) {
791         char **s;
792
793         STRV_FOREACH(s, l)
794                 puts(*s);
795 }
796
797 int strv_extendf(char ***l, const char *format, ...) {
798         va_list ap;
799         char *x;
800         int r;
801
802         va_start(ap, format);
803         r = vasprintf(&x, format, ap);
804         va_end(ap);
805
806         if (r < 0)
807                 return -ENOMEM;
808
809         return strv_consume(l, x);
810 }
811
812 char **strv_reverse(char **l) {
813         unsigned n, i;
814
815         n = strv_length(l);
816         if (n <= 1)
817                 return l;
818
819         for (i = 0; i < n / 2; i++) {
820                 char *t;
821
822                 t = l[i];
823                 l[i] = l[n-1-i];
824                 l[n-1-i] = t;
825         }
826
827         return l;
828 }
829
830 char **strv_shell_escape(char **l, const char *bad) {
831         char **s;
832
833         /* Escapes every character in every string in l that is in bad,
834          * edits in-place, does not roll-back on error. */
835
836         STRV_FOREACH(s, l) {
837                 char *v;
838
839                 v = shell_escape(*s, bad);
840                 if (!v)
841                         return NULL;
842
843                 free(*s);
844                 *s = v;
845         }
846
847         return l;
848 }
849
850 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
851         char* const* p;
852
853         STRV_FOREACH(p, patterns)
854                 if (fnmatch(*p, s, 0) == 0)
855                         return true;
856
857         return false;
858 }
859
860 char ***strv_free_free(char ***l) {
861         char ***i;
862
863         if (!l)
864                 return NULL;
865
866         for (i = l; *i; i++)
867                 strv_free(*i);
868
869         free(l);
870         return NULL;
871 }
872
873 char **strv_skip(char **l, size_t n) {
874
875         while (n > 0) {
876                 if (strv_isempty(l))
877                         return l;
878
879                 l++, n--;
880         }
881
882         return l;
883 }
884
885 int strv_extend_n(char ***l, const char *value, size_t n) {
886         size_t i, j, k;
887         char **nl;
888
889         assert(l);
890
891         if (!value)
892                 return 0;
893         if (n == 0)
894                 return 0;
895
896         /* Adds the value value n times to l */
897
898         k = strv_length(*l);
899
900         nl = realloc(*l, sizeof(char*) * (k + n + 1));
901         if (!nl)
902                 return -ENOMEM;
903
904         *l = nl;
905
906         for (i = k; i < k + n; i++) {
907                 nl[i] = strdup(value);
908                 if (!nl[i])
909                         goto rollback;
910         }
911
912         nl[i] = NULL;
913         return 0;
914
915 rollback:
916         for (j = k; j < i; j++)
917                 free(nl[j]);
918
919         nl[k] = NULL;
920         return -ENOMEM;
921 }
922
923 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
924         bool b = false;
925         char **s;
926         int r;
927
928         /* Like fputs(), but for strv, and with a less stupid argument order */
929
930         if (!space)
931                 space = &b;
932
933         STRV_FOREACH(s, l) {
934                 r = fputs_with_space(f, *s, separator, space);
935                 if (r < 0)
936                         return r;
937         }
938
939         return 0;
940 }
941 #endif // 0