]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.c
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / string-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07630cea
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
07630cea
LP
6***/
7
11c3a366
TA
8#include <errno.h>
9#include <stdarg.h>
10#include <stdint.h>
11#include <stdio.h>
0d536673 12#include <stdio_ext.h>
11c3a366 13#include <stdlib.h>
b6b609db 14#include <string.h>
11c3a366 15
b5efdb8a 16#include "alloc-util.h"
07630cea 17#include "gunicode.h"
c30a49b2 18#include "locale-util.h"
11c3a366 19#include "macro.h"
b11d6a7b 20#include "string-util.h"
b4766d5f 21#include "terminal-util.h"
07630cea
LP
22#include "utf8.h"
23#include "util.h"
c7e03d2e 24#include "fileio.h"
07630cea
LP
25
26int strcmp_ptr(const char *a, const char *b) {
27
28 /* Like strcmp(), but tries to make sense of NULL pointers */
29 if (a && b)
30 return strcmp(a, b);
31
32 if (!a && b)
33 return -1;
34
35 if (a && !b)
36 return 1;
37
38 return 0;
39}
40
41char* endswith(const char *s, const char *postfix) {
42 size_t sl, pl;
43
44 assert(s);
45 assert(postfix);
46
47 sl = strlen(s);
48 pl = strlen(postfix);
49
50 if (pl == 0)
51 return (char*) s + sl;
52
53 if (sl < pl)
54 return NULL;
55
56 if (memcmp(s + sl - pl, postfix, pl) != 0)
57 return NULL;
58
59 return (char*) s + sl - pl;
60}
61
62char* endswith_no_case(const char *s, const char *postfix) {
63 size_t sl, pl;
64
65 assert(s);
66 assert(postfix);
67
68 sl = strlen(s);
69 pl = strlen(postfix);
70
71 if (pl == 0)
72 return (char*) s + sl;
73
74 if (sl < pl)
75 return NULL;
76
77 if (strcasecmp(s + sl - pl, postfix) != 0)
78 return NULL;
79
80 return (char*) s + sl - pl;
81}
82
83char* first_word(const char *s, const char *word) {
84 size_t sl, wl;
85 const char *p;
86
87 assert(s);
88 assert(word);
89
90 /* Checks if the string starts with the specified word, either
91 * followed by NUL or by whitespace. Returns a pointer to the
92 * NUL or the first character after the whitespace. */
93
94 sl = strlen(s);
95 wl = strlen(word);
96
97 if (sl < wl)
98 return NULL;
99
100 if (wl == 0)
101 return (char*) s;
102
103 if (memcmp(s, word, wl) != 0)
104 return NULL;
105
106 p = s + wl;
107 if (*p == 0)
108 return (char*) p;
109
110 if (!strchr(WHITESPACE, *p))
111 return NULL;
112
113 p += strspn(p, WHITESPACE);
114 return (char*) p;
115}
116
117static size_t strcspn_escaped(const char *s, const char *reject) {
118 bool escaped = false;
119 int n;
120
121 for (n=0; s[n]; n++) {
122 if (escaped)
123 escaped = false;
124 else if (s[n] == '\\')
125 escaped = true;
126 else if (strchr(reject, s[n]))
127 break;
128 }
129
130 /* if s ends in \, return index of previous char */
131 return n - escaped;
132}
133
134/* Split a string into words. */
135const char* split(const char **state, size_t *l, const char *separator, bool quoted) {
136 const char *current;
137
138 current = *state;
139
140 if (!*current) {
141 assert(**state == '\0');
142 return NULL;
143 }
144
145 current += strspn(current, separator);
146 if (!*current) {
147 *state = current;
148 return NULL;
149 }
150
151 if (quoted && strchr("\'\"", *current)) {
152 char quotechars[2] = {*current, '\0'};
153
154 *l = strcspn_escaped(current + 1, quotechars);
155 if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] ||
156 (current[*l + 2] && !strchr(separator, current[*l + 2]))) {
157 /* right quote missing or garbage at the end */
158 *state = current;
159 return NULL;
160 }
161 *state = current++ + *l + 2;
162 } else if (quoted) {
163 *l = strcspn_escaped(current, separator);
164 if (current[*l] && !strchr(separator, current[*l])) {
165 /* unfinished escape */
166 *state = current;
167 return NULL;
168 }
169 *state = current + *l;
170 } else {
171 *l = strcspn(current, separator);
172 *state = current + *l;
173 }
174
175 return current;
176}
177
178char *strnappend(const char *s, const char *suffix, size_t b) {
179 size_t a;
180 char *r;
181
182 if (!s && !suffix)
183 return strdup("");
184
185 if (!s)
186 return strndup(suffix, b);
187
188 if (!suffix)
189 return strdup(s);
190
191 assert(s);
192 assert(suffix);
193
194 a = strlen(s);
195 if (b > ((size_t) -1) - a)
196 return NULL;
197
198 r = new(char, a+b+1);
199 if (!r)
200 return NULL;
201
202 memcpy(r, s, a);
203 memcpy(r+a, suffix, b);
204 r[a+b] = 0;
205
206 return r;
207}
208
209char *strappend(const char *s, const char *suffix) {
7bf7ce28 210 return strnappend(s, suffix, strlen_ptr(suffix));
07630cea
LP
211}
212
605405c6 213char *strjoin_real(const char *x, ...) {
07630cea
LP
214 va_list ap;
215 size_t l;
216 char *r, *p;
217
218 va_start(ap, x);
219
220 if (x) {
221 l = strlen(x);
222
223 for (;;) {
224 const char *t;
225 size_t n;
226
227 t = va_arg(ap, const char *);
228 if (!t)
229 break;
230
231 n = strlen(t);
232 if (n > ((size_t) -1) - l) {
233 va_end(ap);
234 return NULL;
235 }
236
237 l += n;
238 }
239 } else
240 l = 0;
241
242 va_end(ap);
243
244 r = new(char, l+1);
245 if (!r)
246 return NULL;
247
248 if (x) {
249 p = stpcpy(r, x);
250
251 va_start(ap, x);
252
253 for (;;) {
254 const char *t;
255
256 t = va_arg(ap, const char *);
257 if (!t)
258 break;
259
260 p = stpcpy(p, t);
261 }
262
263 va_end(ap);
264 } else
265 r[0] = 0;
266
267 return r;
268}
269
270char *strstrip(char *s) {
271 char *e;
272
7546145e
LP
273 if (!s)
274 return NULL;
275
07630cea
LP
276 /* Drops trailing whitespace. Modifies the string in
277 * place. Returns pointer to first non-space character */
278
279 s += strspn(s, WHITESPACE);
280
281 for (e = strchr(s, 0); e > s; e --)
282 if (!strchr(WHITESPACE, e[-1]))
283 break;
284
285 *e = 0;
286
287 return s;
288}
289
290char *delete_chars(char *s, const char *bad) {
291 char *f, *t;
292
7546145e
LP
293 /* Drops all specified bad characters, regardless where in the string */
294
295 if (!s)
296 return NULL;
297
298 if (!bad)
299 bad = WHITESPACE;
07630cea
LP
300
301 for (f = s, t = s; *f; f++) {
302 if (strchr(bad, *f))
303 continue;
304
305 *(t++) = *f;
306 }
307
308 *t = 0;
309
310 return s;
311}
312
7546145e
LP
313char *delete_trailing_chars(char *s, const char *bad) {
314 char *p, *c = s;
315
316 /* Drops all specified bad characters, at the end of the string */
317
318 if (!s)
319 return NULL;
320
321 if (!bad)
322 bad = WHITESPACE;
323
324 for (p = s; *p; p++)
325 if (!strchr(bad, *p))
326 c = p + 1;
327
328 *c = 0;
329
330 return s;
331}
332
07630cea
LP
333char *truncate_nl(char *s) {
334 assert(s);
335
336 s[strcspn(s, NEWLINE)] = 0;
337 return s;
338}
339
b577e3d5
LP
340char ascii_tolower(char x) {
341
342 if (x >= 'A' && x <= 'Z')
343 return x - 'A' + 'a';
344
345 return x;
346}
347
846b8fc3
LP
348char ascii_toupper(char x) {
349
350 if (x >= 'a' && x <= 'z')
351 return x - 'a' + 'A';
352
353 return x;
354}
355
07630cea
LP
356char *ascii_strlower(char *t) {
357 char *p;
358
359 assert(t);
360
361 for (p = t; *p; p++)
b577e3d5
LP
362 *p = ascii_tolower(*p);
363
364 return t;
365}
366
846b8fc3
LP
367char *ascii_strupper(char *t) {
368 char *p;
369
370 assert(t);
371
372 for (p = t; *p; p++)
373 *p = ascii_toupper(*p);
374
375 return t;
376}
377
b577e3d5
LP
378char *ascii_strlower_n(char *t, size_t n) {
379 size_t i;
380
381 if (n <= 0)
382 return t;
383
384 for (i = 0; i < n; i++)
385 t[i] = ascii_tolower(t[i]);
07630cea
LP
386
387 return t;
388}
522d85ae
LP
389
390int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
391
392 for (; n > 0; a++, b++, n--) {
393 int x, y;
394
395 x = (int) (uint8_t) ascii_tolower(*a);
396 y = (int) (uint8_t) ascii_tolower(*b);
397
398 if (x != y)
399 return x - y;
400 }
401
402 return 0;
403}
c1749834
LP
404
405int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
406 int r;
407
408 r = ascii_strcasecmp_n(a, b, MIN(n, m));
409 if (r != 0)
410 return r;
411
412 if (n < m)
413 return -1;
414 else if (n > m)
415 return 1;
416 else
417 return 0;
418}
07630cea
LP
419
420bool chars_intersect(const char *a, const char *b) {
421 const char *p;
422
423 /* Returns true if any of the chars in a are in b. */
424 for (p = a; *p; p++)
425 if (strchr(b, *p))
426 return true;
427
428 return false;
429}
430
431bool string_has_cc(const char *p, const char *ok) {
432 const char *t;
433
434 assert(p);
435
436 /*
437 * Check if a string contains control characters. If 'ok' is
438 * non-NULL it may be a string containing additional CCs to be
439 * considered OK.
440 */
441
442 for (t = p; *t; t++) {
443 if (ok && strchr(ok, *t))
444 continue;
445
446 if (*t > 0 && *t < ' ')
447 return true;
448
449 if (*t == 127)
450 return true;
451 }
452
453 return false;
454}
455
456static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 457 size_t x, need_space;
07630cea
LP
458 char *r;
459
460 assert(s);
461 assert(percent <= 100);
c30a49b2 462 assert(new_length != (size_t) -1);
07630cea 463
c30a49b2 464 if (old_length <= new_length)
07630cea
LP
465 return strndup(s, old_length);
466
c30a49b2
LP
467 /* Special case short ellipsations */
468 switch (new_length) {
469
470 case 0:
471 return strdup("");
472
473 case 1:
474 if (is_locale_utf8())
475 return strdup("…");
476 else
477 return strdup(".");
478
479 case 2:
480 if (!is_locale_utf8())
481 return strdup("..");
482
483 break;
484
485 default:
486 break;
487 }
488
489 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
490 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
491 * either for the UTF-8 encoded character or for three ASCII characters. */
492 need_space = is_locale_utf8() ? 1 : 3;
493
494 r = new(char, new_length+3);
07630cea
LP
495 if (!r)
496 return NULL;
497
c30a49b2 498 assert(new_length >= need_space);
07630cea 499
c30a49b2
LP
500 x = ((new_length - need_space) * percent + 50) / 100;
501 assert(x <= new_length - need_space);
07630cea
LP
502
503 memcpy(r, s, x);
c30a49b2
LP
504
505 if (is_locale_utf8()) {
506 r[x+0] = 0xe2; /* tri-dot ellipsis: … */
507 r[x+1] = 0x80;
508 r[x+2] = 0xa6;
509 } else {
510 r[x+0] = '.';
511 r[x+1] = '.';
512 r[x+2] = '.';
513 }
514
07630cea 515 memcpy(r + x + 3,
c30a49b2
LP
516 s + old_length - (new_length - x - need_space),
517 new_length - x - need_space + 1);
07630cea
LP
518
519 return r;
520}
521
522char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 523 size_t x, k, len, len2;
07630cea 524 const char *i, *j;
c30a49b2 525 char *e;
c932fb71 526 int r;
07630cea 527
c30a49b2
LP
528 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
529 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
530 * strings.
531 *
532 * Ellipsation is done in a locale-dependent way:
533 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
534 * 2. Otherwise, a unicode ellipsis is used ("…")
535 *
536 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
537 * the current locale is UTF-8.
538 */
539
07630cea
LP
540 assert(s);
541 assert(percent <= 100);
ddbc9319
LP
542
543 if (new_length == (size_t) -1)
544 return strndup(s, old_length);
545
c30a49b2
LP
546 if (new_length == 0)
547 return strdup("");
07630cea 548
c30a49b2 549 /* If no multibyte characters use ascii_ellipsize_mem for speed */
07630cea
LP
550 if (ascii_is_valid(s))
551 return ascii_ellipsize_mem(s, old_length, new_length, percent);
552
c30a49b2
LP
553 x = ((new_length - 1) * percent) / 100;
554 assert(x <= new_length - 1);
07630cea
LP
555
556 k = 0;
557 for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
c932fb71 558 char32_t c;
07630cea 559
c932fb71
SL
560 r = utf8_encoded_to_unichar(i, &c);
561 if (r < 0)
07630cea
LP
562 return NULL;
563 k += unichar_iswide(c) ? 2 : 1;
564 }
565
566 if (k > x) /* last character was wide and went over quota */
313cefa1 567 x++;
07630cea
LP
568
569 for (j = s + old_length; k < new_length && j > i; ) {
c932fb71 570 char32_t c;
07630cea
LP
571
572 j = utf8_prev_char(j);
c932fb71
SL
573 r = utf8_encoded_to_unichar(j, &c);
574 if (r < 0)
07630cea
LP
575 return NULL;
576 k += unichar_iswide(c) ? 2 : 1;
577 }
578 assert(i <= j);
579
580 /* we don't actually need to ellipsize */
581 if (i == j)
582 return memdup(s, old_length + 1);
583
584 /* make space for ellipsis */
585 j = utf8_next_char(j);
586
587 len = i - s;
588 len2 = s + old_length - j;
589 e = new(char, len + 3 + len2 + 1);
590 if (!e)
591 return NULL;
592
593 /*
594 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
595 old_length, new_length, x, len, len2, k);
596 */
597
598 memcpy(e, s, len);
c30a49b2 599 e[len + 0] = 0xe2; /* tri-dot ellipsis: … */
07630cea
LP
600 e[len + 1] = 0x80;
601 e[len + 2] = 0xa6;
602
603 memcpy(e + len + 3, j, len2 + 1);
604
605 return e;
606}
607
608char *ellipsize(const char *s, size_t length, unsigned percent) {
ddbc9319
LP
609
610 if (length == (size_t) -1)
611 return strdup(s);
612
07630cea
LP
613 return ellipsize_mem(s, strlen(s), length, percent);
614}
615
2d5dece8 616bool nulstr_contains(const char *nulstr, const char *needle) {
07630cea
LP
617 const char *i;
618
619 if (!nulstr)
620 return false;
621
622 NULSTR_FOREACH(i, nulstr)
623 if (streq(i, needle))
624 return true;
625
626 return false;
627}
628
629char* strshorten(char *s, size_t l) {
630 assert(s);
631
47b33c7d 632 if (strnlen(s, l+1) > l)
07630cea
LP
633 s[l] = 0;
634
635 return s;
636}
637
638char *strreplace(const char *text, const char *old_string, const char *new_string) {
9d73565a
LP
639 size_t l, old_len, new_len, allocated = 0;
640 char *t, *ret = NULL;
07630cea 641 const char *f;
07630cea 642
07630cea
LP
643 assert(old_string);
644 assert(new_string);
645
9d73565a
LP
646 if (!text)
647 return NULL;
648
07630cea
LP
649 old_len = strlen(old_string);
650 new_len = strlen(new_string);
651
652 l = strlen(text);
9d73565a 653 if (!GREEDY_REALLOC(ret, allocated, l+1))
07630cea
LP
654 return NULL;
655
656 f = text;
9d73565a 657 t = ret;
07630cea 658 while (*f) {
07630cea
LP
659 size_t d, nl;
660
661 if (!startswith(f, old_string)) {
662 *(t++) = *(f++);
663 continue;
664 }
665
9d73565a 666 d = t - ret;
07630cea 667 nl = l - old_len + new_len;
9d73565a
LP
668
669 if (!GREEDY_REALLOC(ret, allocated, nl + 1))
670 return mfree(ret);
07630cea
LP
671
672 l = nl;
9d73565a 673 t = ret + d;
07630cea
LP
674
675 t = stpcpy(t, new_string);
676 f += old_len;
677 }
678
679 *t = 0;
9d73565a 680 return ret;
07630cea
LP
681}
682
b4766d5f
ZJS
683static void advance_offsets(ssize_t diff, size_t offsets[2], size_t shift[2], size_t size) {
684 if (!offsets)
685 return;
686
687 if ((size_t) diff < offsets[0])
688 shift[0] += size;
689 if ((size_t) diff < offsets[1])
690 shift[1] += size;
691}
692
693char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
07630cea
LP
694 const char *i, *begin = NULL;
695 enum {
696 STATE_OTHER,
697 STATE_ESCAPE,
695a944c
LP
698 STATE_CSI,
699 STATE_CSO,
07630cea
LP
700 } state = STATE_OTHER;
701 char *obuf = NULL;
b4766d5f 702 size_t osz = 0, isz, shift[2] = {};
07630cea
LP
703 FILE *f;
704
705 assert(ibuf);
706 assert(*ibuf);
707
695a944c
LP
708 /* This does three things:
709 *
710 * 1. Replaces TABs by 8 spaces
711 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
712 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
713 *
714 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as are any
715 * other special characters. Truncated ANSI sequences are left-as is too. This call is supposed to suppress the
716 * most basic formatting noise, but nothing else.
717 *
718 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
07630cea
LP
719
720 isz = _isz ? *_isz : strlen(*ibuf);
721
722 f = open_memstream(&obuf, &osz);
723 if (!f)
724 return NULL;
725
0d536673
LP
726 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we created f here
727 * and it doesn't leave our scope. */
728
729 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
db3f45e2 730
07630cea
LP
731 for (i = *ibuf; i < *ibuf + isz + 1; i++) {
732
733 switch (state) {
734
735 case STATE_OTHER:
736 if (i >= *ibuf + isz) /* EOT */
737 break;
738 else if (*i == '\x1B')
739 state = STATE_ESCAPE;
b4766d5f 740 else if (*i == '\t') {
0d536673 741 fputs(" ", f);
b4766d5f
ZJS
742 advance_offsets(i - *ibuf, highlight, shift, 7);
743 } else
0d536673 744 fputc(*i, f);
b4766d5f 745
07630cea
LP
746 break;
747
748 case STATE_ESCAPE:
749 if (i >= *ibuf + isz) { /* EOT */
0d536673 750 fputc('\x1B', f);
b4766d5f 751 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea 752 break;
695a944c
LP
753 } else if (*i == '[') { /* ANSI CSI */
754 state = STATE_CSI;
755 begin = i + 1;
756 } else if (*i == ']') { /* ANSI CSO */
757 state = STATE_CSO;
07630cea
LP
758 begin = i + 1;
759 } else {
0d536673
LP
760 fputc('\x1B', f);
761 fputc(*i, f);
b4766d5f 762 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea
LP
763 state = STATE_OTHER;
764 }
765
766 break;
767
695a944c 768 case STATE_CSI:
07630cea 769
695a944c
LP
770 if (i >= *ibuf + isz || /* EOT … */
771 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
0d536673
LP
772 fputc('\x1B', f);
773 fputc('[', f);
b4766d5f 774 advance_offsets(i - *ibuf, highlight, shift, 2);
07630cea
LP
775 state = STATE_OTHER;
776 i = begin-1;
777 } else if (*i == 'm')
778 state = STATE_OTHER;
695a944c
LP
779
780 break;
781
782 case STATE_CSO:
783
784 if (i >= *ibuf + isz || /* EOT … */
785 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
786 fputc('\x1B', f);
787 fputc(']', f);
788 advance_offsets(i - *ibuf, highlight, shift, 2);
789 state = STATE_OTHER;
790 i = begin-1;
791 } else if (*i == '\a')
792 state = STATE_OTHER;
793
07630cea
LP
794 break;
795 }
796 }
797
c7e03d2e 798 if (fflush_and_check(f) < 0) {
07630cea 799 fclose(f);
6b430fdb 800 return mfree(obuf);
07630cea
LP
801 }
802
803 fclose(f);
804
805 free(*ibuf);
806 *ibuf = obuf;
807
808 if (_isz)
809 *_isz = osz;
810
b4766d5f
ZJS
811 if (highlight) {
812 highlight[0] += shift[0];
813 highlight[1] += shift[1];
814 }
815
07630cea
LP
816 return obuf;
817}
818
bb8ad9ea
LP
819char *strextend_with_separator(char **x, const char *separator, ...) {
820 bool need_separator;
821 size_t f, l, l_separator;
07630cea 822 char *r, *p;
bb8ad9ea 823 va_list ap;
07630cea
LP
824
825 assert(x);
826
7bf7ce28 827 l = f = strlen_ptr(*x);
07630cea 828
bb8ad9ea
LP
829 need_separator = !isempty(*x);
830 l_separator = strlen_ptr(separator);
831
832 va_start(ap, separator);
07630cea
LP
833 for (;;) {
834 const char *t;
835 size_t n;
836
837 t = va_arg(ap, const char *);
838 if (!t)
839 break;
840
841 n = strlen(t);
bb8ad9ea
LP
842
843 if (need_separator)
844 n += l_separator;
845
07630cea
LP
846 if (n > ((size_t) -1) - l) {
847 va_end(ap);
848 return NULL;
849 }
850
851 l += n;
bb8ad9ea 852 need_separator = true;
07630cea
LP
853 }
854 va_end(ap);
855
bb8ad9ea
LP
856 need_separator = !isempty(*x);
857
07630cea
LP
858 r = realloc(*x, l+1);
859 if (!r)
860 return NULL;
861
862 p = r + f;
863
bb8ad9ea 864 va_start(ap, separator);
07630cea
LP
865 for (;;) {
866 const char *t;
867
868 t = va_arg(ap, const char *);
869 if (!t)
870 break;
871
bb8ad9ea
LP
872 if (need_separator && separator)
873 p = stpcpy(p, separator);
874
07630cea 875 p = stpcpy(p, t);
bb8ad9ea
LP
876
877 need_separator = true;
07630cea
LP
878 }
879 va_end(ap);
880
bb8ad9ea
LP
881 assert(p == r + l);
882
07630cea
LP
883 *p = 0;
884 *x = r;
885
886 return r + l;
887}
888
889char *strrep(const char *s, unsigned n) {
890 size_t l;
891 char *r, *p;
892 unsigned i;
893
894 assert(s);
895
896 l = strlen(s);
897 p = r = malloc(l * n + 1);
898 if (!r)
899 return NULL;
900
901 for (i = 0; i < n; i++)
902 p = stpcpy(p, s);
903
904 *p = 0;
905 return r;
906}
907
908int split_pair(const char *s, const char *sep, char **l, char **r) {
909 char *x, *a, *b;
910
911 assert(s);
912 assert(sep);
913 assert(l);
914 assert(r);
915
916 if (isempty(sep))
917 return -EINVAL;
918
919 x = strstr(s, sep);
920 if (!x)
921 return -EINVAL;
922
923 a = strndup(s, x - s);
924 if (!a)
925 return -ENOMEM;
926
927 b = strdup(x + strlen(sep));
928 if (!b) {
929 free(a);
930 return -ENOMEM;
931 }
932
933 *l = a;
934 *r = b;
935
936 return 0;
937}
938
939int free_and_strdup(char **p, const char *s) {
940 char *t;
941
942 assert(p);
943
944 /* Replaces a string pointer with an strdup()ed new string,
945 * possibly freeing the old one. */
946
947 if (streq_ptr(*p, s))
948 return 0;
949
950 if (s) {
951 t = strdup(s);
952 if (!t)
953 return -ENOMEM;
954 } else
955 t = NULL;
956
957 free(*p);
958 *p = t;
959
960 return 1;
961}
962
4b9545f1 963#if !HAVE_EXPLICIT_BZERO
b6b609db
MB
964/*
965 * Pointer to memset is volatile so that compiler must de-reference
966 * the pointer and can't assume that it points to any function in
967 * particular (such as memset, which it then might further "optimize")
968 * This approach is inspired by openssl's crypto/mem_clr.c.
969 */
970typedef void *(*memset_t)(void *,int,size_t);
9fe4ea21 971
b6b609db 972static volatile memset_t memset_func = memset;
9fe4ea21 973
2d26d8e0
ZJS
974void explicit_bzero(void *p, size_t l) {
975 memset_func(p, '\0', l);
9fe4ea21 976}
2d26d8e0 977#endif
9fe4ea21 978
9fe4ea21 979char* string_erase(char *x) {
07630cea 980 if (!x)
9fe4ea21 981 return NULL;
07630cea
LP
982
983 /* A delicious drop of snake-oil! To be called on memory where
984 * we stored passphrases or so, after we used them. */
2d26d8e0
ZJS
985 explicit_bzero(x, strlen(x));
986 return x;
07630cea
LP
987}
988
989char *string_free_erase(char *s) {
9fe4ea21 990 return mfree(string_erase(s));
07630cea 991}
f3e2e81d
LP
992
993bool string_is_safe(const char *p) {
994 const char *t;
995
996 if (!p)
997 return false;
998
999 for (t = p; *t; t++) {
1000 if (*t > 0 && *t < ' ') /* no control characters */
1001 return false;
1002
1003 if (strchr(QUOTES "\\\x7f", *t))
1004 return false;
1005 }
1006
1007 return true;
1008}