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