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