1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "alloc-util.h"
10 #include "escape.h"
11 #include "extract-word.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 "strv.h"
19 #include "terminal-util.h"
20 #include "utf8.h"
21 #include "util.h"
22
first_word(const char * s,const char * word)23 char* first_word(const char *s, const char *word) {
24 size_t sl, wl;
25 const char *p;
26
27 assert(s);
28 assert(word);
29
30 /* Checks if the string starts with the specified word, either
31 * followed by NUL or by whitespace. Returns a pointer to the
32 * NUL or the first character after the whitespace. */
33
34 sl = strlen(s);
35 wl = strlen(word);
36
37 if (sl < wl)
38 return NULL;
39
40 if (wl == 0)
41 return (char*) s;
42
43 if (memcmp(s, word, wl) != 0)
44 return NULL;
45
46 p = s + wl;
47 if (*p == 0)
48 return (char*) p;
49
50 if (!strchr(WHITESPACE, *p))
51 return NULL;
52
53 p += strspn(p, WHITESPACE);
54 return (char*) p;
55 }
56
strnappend(const char * s,const char * suffix,size_t b)57 char *strnappend(const char *s, const char *suffix, size_t b) {
58 size_t a;
59 char *r;
60
61 if (!s && !suffix)
62 return strdup("");
63
64 if (!s)
65 return strndup(suffix, b);
66
67 if (!suffix)
68 return strdup(s);
69
70 assert(s);
71 assert(suffix);
72
73 a = strlen(s);
74 if (b > SIZE_MAX - a)
75 return NULL;
76
77 r = new(char, a+b+1);
78 if (!r)
79 return NULL;
80
81 memcpy(r, s, a);
82 memcpy(r+a, suffix, b);
83 r[a+b] = 0;
84
85 return r;
86 }
87
strjoin_real(const char * x,...)88 char *strjoin_real(const char *x, ...) {
89 va_list ap;
90 size_t l = 1;
91 char *r, *p;
92
93 va_start(ap, x);
94 for (const char *t = x; t; t = va_arg(ap, const char *)) {
95 size_t n;
96
97 n = strlen(t);
98 if (n > SIZE_MAX - l) {
99 va_end(ap);
100 return NULL;
101 }
102 l += n;
103 }
104 va_end(ap);
105
106 p = r = new(char, l);
107 if (!r)
108 return NULL;
109
110 va_start(ap, x);
111 for (const char *t = x; t; t = va_arg(ap, const char *))
112 p = stpcpy(p, t);
113 va_end(ap);
114
115 *p = 0;
116
117 return r;
118 }
119
strstrip(char * s)120 char *strstrip(char *s) {
121 if (!s)
122 return NULL;
123
124 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
125
126 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
127 }
128
delete_chars(char * s,const char * bad)129 char *delete_chars(char *s, const char *bad) {
130 char *f, *t;
131
132 /* Drops all specified bad characters, regardless where in the string */
133
134 if (!s)
135 return NULL;
136
137 if (!bad)
138 bad = WHITESPACE;
139
140 for (f = s, t = s; *f; f++) {
141 if (strchr(bad, *f))
142 continue;
143
144 *(t++) = *f;
145 }
146
147 *t = 0;
148
149 return s;
150 }
151
delete_trailing_chars(char * s,const char * bad)152 char *delete_trailing_chars(char *s, const char *bad) {
153 char *c = s;
154
155 /* Drops all specified bad characters, at the end of the string */
156
157 if (!s)
158 return NULL;
159
160 if (!bad)
161 bad = WHITESPACE;
162
163 for (char *p = s; *p; p++)
164 if (!strchr(bad, *p))
165 c = p + 1;
166
167 *c = 0;
168
169 return s;
170 }
171
truncate_nl(char * s)172 char *truncate_nl(char *s) {
173 assert(s);
174
175 s[strcspn(s, NEWLINE)] = 0;
176 return s;
177 }
178
ascii_tolower(char x)179 char ascii_tolower(char x) {
180
181 if (x >= 'A' && x <= 'Z')
182 return x - 'A' + 'a';
183
184 return x;
185 }
186
ascii_toupper(char x)187 char ascii_toupper(char x) {
188
189 if (x >= 'a' && x <= 'z')
190 return x - 'a' + 'A';
191
192 return x;
193 }
194
ascii_strlower(char * t)195 char *ascii_strlower(char *t) {
196 assert(t);
197
198 for (char *p = t; *p; p++)
199 *p = ascii_tolower(*p);
200
201 return t;
202 }
203
ascii_strupper(char * t)204 char *ascii_strupper(char *t) {
205 assert(t);
206
207 for (char *p = t; *p; p++)
208 *p = ascii_toupper(*p);
209
210 return t;
211 }
212
ascii_strlower_n(char * t,size_t n)213 char *ascii_strlower_n(char *t, size_t n) {
214 if (n <= 0)
215 return t;
216
217 for (size_t i = 0; i < n; i++)
218 t[i] = ascii_tolower(t[i]);
219
220 return t;
221 }
222
ascii_strcasecmp_n(const char * a,const char * b,size_t n)223 int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
224
225 for (; n > 0; a++, b++, n--) {
226 int x, y;
227
228 x = (int) (uint8_t) ascii_tolower(*a);
229 y = (int) (uint8_t) ascii_tolower(*b);
230
231 if (x != y)
232 return x - y;
233 }
234
235 return 0;
236 }
237
ascii_strcasecmp_nn(const char * a,size_t n,const char * b,size_t m)238 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
239 int r;
240
241 r = ascii_strcasecmp_n(a, b, MIN(n, m));
242 if (r != 0)
243 return r;
244
245 return CMP(n, m);
246 }
247
chars_intersect(const char * a,const char * b)248 bool chars_intersect(const char *a, const char *b) {
249 /* Returns true if any of the chars in a are in b. */
250 for (const char *p = a; *p; p++)
251 if (strchr(b, *p))
252 return true;
253
254 return false;
255 }
256
string_has_cc(const char * p,const char * ok)257 bool string_has_cc(const char *p, const char *ok) {
258 assert(p);
259
260 /*
261 * Check if a string contains control characters. If 'ok' is
262 * non-NULL it may be a string containing additional CCs to be
263 * considered OK.
264 */
265
266 for (const char *t = p; *t; t++) {
267 if (ok && strchr(ok, *t))
268 continue;
269
270 if (char_is_cc(*t))
271 return true;
272 }
273
274 return false;
275 }
276
write_ellipsis(char * buf,bool unicode)277 static int write_ellipsis(char *buf, bool unicode) {
278 if (unicode || is_locale_utf8()) {
279 buf[0] = 0xe2; /* tri-dot ellipsis: … */
280 buf[1] = 0x80;
281 buf[2] = 0xa6;
282 } else {
283 buf[0] = '.';
284 buf[1] = '.';
285 buf[2] = '.';
286 }
287
288 return 3;
289 }
290
ascii_ellipsize_mem(const char * s,size_t old_length,size_t new_length,unsigned percent)291 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
292 size_t x, need_space, suffix_len;
293 char *t;
294
295 assert(s);
296 assert(percent <= 100);
297 assert(new_length != SIZE_MAX);
298
299 if (old_length <= new_length)
300 return strndup(s, old_length);
301
302 /* Special case short ellipsations */
303 switch (new_length) {
304
305 case 0:
306 return strdup("");
307
308 case 1:
309 if (is_locale_utf8())
310 return strdup("…");
311 else
312 return strdup(".");
313
314 case 2:
315 if (!is_locale_utf8())
316 return strdup("..");
317
318 break;
319
320 default:
321 break;
322 }
323
324 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
325 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
326 * either for the UTF-8 encoded character or for three ASCII characters. */
327 need_space = is_locale_utf8() ? 1 : 3;
328
329 t = new(char, new_length+3);
330 if (!t)
331 return NULL;
332
333 assert(new_length >= need_space);
334
335 x = ((new_length - need_space) * percent + 50) / 100;
336 assert(x <= new_length - need_space);
337
338 memcpy(t, s, x);
339 write_ellipsis(t + x, false);
340 suffix_len = new_length - x - need_space;
341 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
342 *(t + x + 3 + suffix_len) = '\0';
343
344 return t;
345 }
346
ellipsize_mem(const char * s,size_t old_length,size_t new_length,unsigned percent)347 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
348 size_t x, k, len, len2;
349 const char *i, *j;
350 char *e;
351 int r;
352
353 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
354 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
355 * strings.
356 *
357 * Ellipsation is done in a locale-dependent way:
358 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
359 * 2. Otherwise, a unicode ellipsis is used ("…")
360 *
361 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
362 * the current locale is UTF-8.
363 */
364
365 assert(s);
366 assert(percent <= 100);
367
368 if (new_length == SIZE_MAX)
369 return strndup(s, old_length);
370
371 if (new_length == 0)
372 return strdup("");
373
374 /* If no multibyte characters use ascii_ellipsize_mem for speed */
375 if (ascii_is_valid_n(s, old_length))
376 return ascii_ellipsize_mem(s, old_length, new_length, percent);
377
378 x = ((new_length - 1) * percent) / 100;
379 assert(x <= new_length - 1);
380
381 k = 0;
382 for (i = s; i < s + old_length; i = utf8_next_char(i)) {
383 char32_t c;
384 int w;
385
386 r = utf8_encoded_to_unichar(i, &c);
387 if (r < 0)
388 return NULL;
389
390 w = unichar_iswide(c) ? 2 : 1;
391 if (k + w <= x)
392 k += w;
393 else
394 break;
395 }
396
397 for (j = s + old_length; j > i; ) {
398 char32_t c;
399 int w;
400 const char *jj;
401
402 jj = utf8_prev_char(j);
403 r = utf8_encoded_to_unichar(jj, &c);
404 if (r < 0)
405 return NULL;
406
407 w = unichar_iswide(c) ? 2 : 1;
408 if (k + w <= new_length) {
409 k += w;
410 j = jj;
411 } else
412 break;
413 }
414 assert(i <= j);
415
416 /* we don't actually need to ellipsize */
417 if (i == j)
418 return memdup_suffix0(s, old_length);
419
420 /* make space for ellipsis, if possible */
421 if (j < s + old_length)
422 j = utf8_next_char(j);
423 else if (i > s)
424 i = utf8_prev_char(i);
425
426 len = i - s;
427 len2 = s + old_length - j;
428 e = new(char, len + 3 + len2 + 1);
429 if (!e)
430 return NULL;
431
432 /*
433 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
434 old_length, new_length, x, len, len2, k);
435 */
436
437 memcpy(e, s, len);
438 write_ellipsis(e + len, true);
439 memcpy(e + len + 3, j, len2);
440 *(e + len + 3 + len2) = '\0';
441
442 return e;
443 }
444
cellescape(char * buf,size_t len,const char * s)445 char *cellescape(char *buf, size_t len, const char *s) {
446 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
447 * characters are copied as they are, everything else is escaped. The result
448 * is different then if escaping and ellipsization was performed in two
449 * separate steps, because each sequence is either stored in full or skipped.
450 *
451 * This function should be used for logging about strings which expected to
452 * be plain ASCII in a safe way.
453 *
454 * An ellipsis will be used if s is too long. It was always placed at the
455 * very end.
456 */
457
458 size_t i = 0, last_char_width[4] = {}, k = 0;
459
460 assert(len > 0); /* at least a terminating NUL */
461
462 for (;;) {
463 char four[4];
464 int w;
465
466 if (*s == 0) /* terminating NUL detected? then we are done! */
467 goto done;
468
469 w = cescape_char(*s, four);
470 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
471 * ellipsize at the previous location */
472 break;
473
474 /* OK, there was space, let's add this escaped character to the buffer */
475 memcpy(buf + i, four, w);
476 i += w;
477
478 /* And remember its width in the ring buffer */
479 last_char_width[k] = w;
480 k = (k + 1) % 4;
481
482 s++;
483 }
484
485 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
486 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
487 for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
488
489 if (i + 4 <= len) /* nice, we reached our space goal */
490 break;
491
492 k = k == 0 ? 3 : k - 1;
493 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
494 break;
495
496 assert(i >= last_char_width[k]);
497 i -= last_char_width[k];
498 }
499
500 if (i + 4 <= len) /* yay, enough space */
501 i += write_ellipsis(buf + i, false);
502 else if (i + 3 <= len) { /* only space for ".." */
503 buf[i++] = '.';
504 buf[i++] = '.';
505 } else if (i + 2 <= len) /* only space for a single "." */
506 buf[i++] = '.';
507 else
508 assert(i + 1 <= len);
509
510 done:
511 buf[i] = '\0';
512 return buf;
513 }
514
strshorten(char * s,size_t l)515 char* strshorten(char *s, size_t l) {
516 assert(s);
517
518 if (strnlen(s, l+1) > l)
519 s[l] = 0;
520
521 return s;
522 }
523
strreplace(const char * text,const char * old_string,const char * new_string)524 char *strreplace(const char *text, const char *old_string, const char *new_string) {
525 size_t l, old_len, new_len;
526 char *t, *ret = NULL;
527 const char *f;
528
529 assert(old_string);
530 assert(new_string);
531
532 if (!text)
533 return NULL;
534
535 old_len = strlen(old_string);
536 new_len = strlen(new_string);
537
538 l = strlen(text);
539 if (!GREEDY_REALLOC(ret, l+1))
540 return NULL;
541
542 f = text;
543 t = ret;
544 while (*f) {
545 size_t d, nl;
546
547 if (!startswith(f, old_string)) {
548 *(t++) = *(f++);
549 continue;
550 }
551
552 d = t - ret;
553 nl = l - old_len + new_len;
554
555 if (!GREEDY_REALLOC(ret, nl + 1))
556 return mfree(ret);
557
558 l = nl;
559 t = ret + d;
560
561 t = stpcpy(t, new_string);
562 f += old_len;
563 }
564
565 *t = 0;
566 return ret;
567 }
568
advance_offsets(ssize_t diff,size_t offsets[2],size_t shift[static2],size_t size)569 static void advance_offsets(
570 ssize_t diff,
571 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
572 size_t shift[static 2],
573 size_t size) {
574
575 if (!offsets)
576 return;
577
578 assert(shift);
579
580 if ((size_t) diff < offsets[0])
581 shift[0] += size;
582 if ((size_t) diff < offsets[1])
583 shift[1] += size;
584 }
585
strip_tab_ansi(char ** ibuf,size_t * _isz,size_t highlight[2])586 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
587 const char *begin = NULL;
588 enum {
589 STATE_OTHER,
590 STATE_ESCAPE,
591 STATE_CSI,
592 STATE_CSO,
593 } state = STATE_OTHER;
594 char *obuf = NULL;
595 size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0;
596 FILE *f;
597
598 assert(ibuf);
599 assert(*ibuf);
600
601 /* This does three things:
602 *
603 * 1. Replaces TABs by 8 spaces
604 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
605 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
606 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
607 * other effect).
608 *
609 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
610 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
611 * supposed to suppress the most basic formatting noise, but nothing else.
612 *
613 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
614
615 isz = _isz ? *_isz : strlen(*ibuf);
616
617 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
618 * created f here and it doesn't leave our scope. */
619 f = open_memstream_unlocked(&obuf, &osz);
620 if (!f)
621 return NULL;
622
623 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
624
625 switch (state) {
626
627 case STATE_OTHER:
628 if (i >= *ibuf + isz) /* EOT */
629 break;
630
631 if (*i == '\r') {
632 n_carriage_returns++;
633 break;
634 } else if (*i == '\n')
635 /* Ignore carriage returns before new line */
636 n_carriage_returns = 0;
637 for (; n_carriage_returns > 0; n_carriage_returns--)
638 fputc('\r', f);
639
640 if (*i == '\x1B')
641 state = STATE_ESCAPE;
642 else if (*i == '\t') {
643 fputs(" ", f);
644 advance_offsets(i - *ibuf, highlight, shift, 7);
645 } else
646 fputc(*i, f);
647
648 break;
649
650 case STATE_ESCAPE:
651 assert(n_carriage_returns == 0);
652
653 if (i >= *ibuf + isz) { /* EOT */
654 fputc('\x1B', f);
655 advance_offsets(i - *ibuf, highlight, shift, 1);
656 break;
657 } else if (*i == '[') { /* ANSI CSI */
658 state = STATE_CSI;
659 begin = i + 1;
660 } else if (*i == ']') { /* ANSI CSO */
661 state = STATE_CSO;
662 begin = i + 1;
663 } else {
664 fputc('\x1B', f);
665 fputc(*i, f);
666 advance_offsets(i - *ibuf, highlight, shift, 1);
667 state = STATE_OTHER;
668 }
669
670 break;
671
672 case STATE_CSI:
673 assert(n_carriage_returns == 0);
674
675 if (i >= *ibuf + isz || /* EOT … */
676 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
677 fputc('\x1B', f);
678 fputc('[', f);
679 advance_offsets(i - *ibuf, highlight, shift, 2);
680 state = STATE_OTHER;
681 i = begin-1;
682 } else if (*i == 'm')
683 state = STATE_OTHER;
684
685 break;
686
687 case STATE_CSO:
688 assert(n_carriage_returns == 0);
689
690 if (i >= *ibuf + isz || /* EOT … */
691 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
692 fputc('\x1B', f);
693 fputc(']', f);
694 advance_offsets(i - *ibuf, highlight, shift, 2);
695 state = STATE_OTHER;
696 i = begin-1;
697 } else if (*i == '\a')
698 state = STATE_OTHER;
699
700 break;
701 }
702 }
703
704 if (fflush_and_check(f) < 0) {
705 fclose(f);
706 return mfree(obuf);
707 }
708 fclose(f);
709
710 free_and_replace(*ibuf, obuf);
711
712 if (_isz)
713 *_isz = osz;
714
715 if (highlight) {
716 highlight[0] += shift[0];
717 highlight[1] += shift[1];
718 }
719
720 return *ibuf;
721 }
722
strextend_with_separator_internal(char ** x,const char * separator,...)723 char *strextend_with_separator_internal(char **x, const char *separator, ...) {
724 size_t f, l, l_separator;
725 bool need_separator;
726 char *nr, *p;
727 va_list ap;
728
729 assert(x);
730
731 l = f = strlen_ptr(*x);
732
733 need_separator = !isempty(*x);
734 l_separator = strlen_ptr(separator);
735
736 va_start(ap, separator);
737 for (;;) {
738 const char *t;
739 size_t n;
740
741 t = va_arg(ap, const char *);
742 if (!t)
743 break;
744
745 n = strlen(t);
746
747 if (need_separator)
748 n += l_separator;
749
750 if (n >= SIZE_MAX - l) {
751 va_end(ap);
752 return NULL;
753 }
754
755 l += n;
756 need_separator = true;
757 }
758 va_end(ap);
759
760 need_separator = !isempty(*x);
761
762 nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
763 if (!nr)
764 return NULL;
765
766 *x = nr;
767 p = nr + f;
768
769 va_start(ap, separator);
770 for (;;) {
771 const char *t;
772
773 t = va_arg(ap, const char *);
774 if (!t)
775 break;
776
777 if (need_separator && separator)
778 p = stpcpy(p, separator);
779
780 p = stpcpy(p, t);
781
782 need_separator = true;
783 }
784 va_end(ap);
785
786 assert(p == nr + l);
787
788 *p = 0;
789
790 return p;
791 }
792
strextendf_with_separator(char ** x,const char * separator,const char * format,...)793 int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
794 size_t m, a, l_separator;
795 va_list ap;
796 int l;
797
798 /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
799 * we'll spend a tonload of time in determining the length of the string passed in, over and over
800 * again. */
801
802 assert(x);
803 assert(format);
804
805 l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
806
807 /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
808 if (*x) {
809 m = strlen(*x);
810 a = MALLOC_SIZEOF_SAFE(*x);
811 assert(a >= m + 1);
812 } else
813 m = a = 0;
814
815 if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
816 char *n;
817
818 if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
819 return -ENOMEM;
820 if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
821 return -ENOMEM;
822
823 n = realloc(*x, m + 64 + l_separator);
824 if (!n)
825 return -ENOMEM;
826
827 *x = n;
828 a = MALLOC_SIZEOF_SAFE(*x);
829 }
830
831 /* Now, let's try to format the string into it */
832 memcpy_safe(*x + m, separator, l_separator);
833 va_start(ap, format);
834 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
835 va_end(ap);
836
837 assert(l >= 0);
838
839 if ((size_t) l < a - m - l_separator) {
840 char *n;
841
842 /* Nice! This worked. We are done. But first, let's return the extra space we don't
843 * need. This should be a cheap operation, since we only lower the allocation size here,
844 * never increase. */
845 n = realloc(*x, m + (size_t) l + l_separator + 1);
846 if (n)
847 *x = n;
848 } else {
849 char *n;
850
851 /* Wasn't enough. Then let's allocate exactly what we need. */
852
853 if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
854 goto oom;
855 if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
856 goto oom;
857
858 a = m + (size_t) l + l_separator + 1;
859 n = realloc(*x, a);
860 if (!n)
861 goto oom;
862 *x = n;
863
864 va_start(ap, format);
865 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
866 va_end(ap);
867
868 assert((size_t) l < a - m - l_separator);
869 }
870
871 return 0;
872
873 oom:
874 /* truncate the bytes added after the first vsnprintf() attempt again */
875 (*x)[m] = 0;
876 return -ENOMEM;
877 }
878
strrep(const char * s,unsigned n)879 char *strrep(const char *s, unsigned n) {
880 char *r, *p;
881 size_t l;
882
883 assert(s);
884
885 l = strlen(s);
886 p = r = malloc(l * n + 1);
887 if (!r)
888 return NULL;
889
890 for (unsigned i = 0; i < n; i++)
891 p = stpcpy(p, s);
892
893 *p = 0;
894 return r;
895 }
896
split_pair(const char * s,const char * sep,char ** l,char ** r)897 int split_pair(const char *s, const char *sep, char **l, char **r) {
898 char *x, *a, *b;
899
900 assert(s);
901 assert(sep);
902 assert(l);
903 assert(r);
904
905 if (isempty(sep))
906 return -EINVAL;
907
908 x = strstr(s, sep);
909 if (!x)
910 return -EINVAL;
911
912 a = strndup(s, x - s);
913 if (!a)
914 return -ENOMEM;
915
916 b = strdup(x + strlen(sep));
917 if (!b) {
918 free(a);
919 return -ENOMEM;
920 }
921
922 *l = a;
923 *r = b;
924
925 return 0;
926 }
927
free_and_strdup(char ** p,const char * s)928 int free_and_strdup(char **p, const char *s) {
929 char *t;
930
931 assert(p);
932
933 /* Replaces a string pointer with a strdup()ed new string,
934 * possibly freeing the old one. */
935
936 if (streq_ptr(*p, s))
937 return 0;
938
939 if (s) {
940 t = strdup(s);
941 if (!t)
942 return -ENOMEM;
943 } else
944 t = NULL;
945
946 free(*p);
947 *p = t;
948
949 return 1;
950 }
951
free_and_strndup(char ** p,const char * s,size_t l)952 int free_and_strndup(char **p, const char *s, size_t l) {
953 char *t;
954
955 assert(p);
956 assert(s || l == 0);
957
958 /* Replaces a string pointer with a strndup()ed new string,
959 * freeing the old one. */
960
961 if (!*p && !s)
962 return 0;
963
964 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
965 return 0;
966
967 if (s) {
968 t = strndup(s, l);
969 if (!t)
970 return -ENOMEM;
971 } else
972 t = NULL;
973
974 free_and_replace(*p, t);
975 return 1;
976 }
977
string_is_safe(const char * p)978 bool string_is_safe(const char *p) {
979 if (!p)
980 return false;
981
982 /* Checks if the specified string contains no quotes or control characters */
983
984 for (const char *t = p; *t; t++) {
985 if (*t > 0 && *t < ' ') /* no control characters */
986 return false;
987
988 if (strchr(QUOTES "\\\x7f", *t))
989 return false;
990 }
991
992 return true;
993 }
994
string_erase(char * x)995 char* string_erase(char *x) {
996 if (!x)
997 return NULL;
998
999 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1000 * used them. */
1001 explicit_bzero_safe(x, strlen(x));
1002 return x;
1003 }
1004
string_truncate_lines(const char * s,size_t n_lines,char ** ret)1005 int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1006 const char *p = s, *e = s;
1007 bool truncation_applied = false;
1008 char *copy;
1009 size_t n = 0;
1010
1011 assert(s);
1012
1013 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1014 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1015 * generated either. */
1016
1017 for (;;) {
1018 size_t k;
1019
1020 k = strcspn(p, "\n");
1021
1022 if (p[k] == 0) {
1023 if (k == 0) /* final empty line */
1024 break;
1025
1026 if (n >= n_lines) /* above threshold */
1027 break;
1028
1029 e = p + k; /* last line to include */
1030 break;
1031 }
1032
1033 assert(p[k] == '\n');
1034
1035 if (n >= n_lines)
1036 break;
1037
1038 if (k > 0)
1039 e = p + k;
1040
1041 p += k + 1;
1042 n++;
1043 }
1044
1045 /* e points after the last character we want to keep */
1046 if (isempty(e))
1047 copy = strdup(s);
1048 else {
1049 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1050 * isn't a new-line or a series of them */
1051 truncation_applied = true;
1052
1053 copy = strndup(s, e - s);
1054 }
1055 if (!copy)
1056 return -ENOMEM;
1057
1058 *ret = copy;
1059 return truncation_applied;
1060 }
1061
string_extract_line(const char * s,size_t i,char ** ret)1062 int string_extract_line(const char *s, size_t i, char **ret) {
1063 const char *p = s;
1064 size_t c = 0;
1065
1066 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1067 * and == 0 if we are looking at the last line or already beyond the last line. As special
1068 * optimization, if the first line is requested and the string only consists of one line we return
1069 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1070 * common case. */
1071
1072 for (;;) {
1073 const char *q;
1074
1075 q = strchr(p, '\n');
1076 if (i == c) {
1077 /* The line we are looking for! */
1078
1079 if (q) {
1080 char *m;
1081
1082 m = strndup(p, q - p);
1083 if (!m)
1084 return -ENOMEM;
1085
1086 *ret = m;
1087 return !isempty(q + 1); /* more coming? */
1088 } else {
1089 if (p == s)
1090 *ret = NULL; /* Just use the input string */
1091 else {
1092 char *m;
1093
1094 m = strdup(p);
1095 if (!m)
1096 return -ENOMEM;
1097
1098 *ret = m;
1099 }
1100
1101 return 0; /* The end */
1102 }
1103 }
1104
1105 if (!q) {
1106 char *m;
1107
1108 /* No more lines, return empty line */
1109
1110 m = strdup("");
1111 if (!m)
1112 return -ENOMEM;
1113
1114 *ret = m;
1115 return 0; /* The end */
1116 }
1117
1118 p = q + 1;
1119 c++;
1120 }
1121 }
1122
string_contains_word_strv(const char * string,const char * separators,char ** words,const char ** ret_word)1123 int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word) {
1124 /* In the default mode with no separators specified, we split on whitespace and
1125 * don't coalesce separators. */
1126 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
1127
1128 const char *found = NULL;
1129
1130 for (const char *p = string;;) {
1131 _cleanup_free_ char *w = NULL;
1132 int r;
1133
1134 r = extract_first_word(&p, &w, separators, flags);
1135 if (r < 0)
1136 return r;
1137 if (r == 0)
1138 break;
1139
1140 found = strv_find(words, w);
1141 if (found)
1142 break;
1143 }
1144
1145 if (ret_word)
1146 *ret_word = found;
1147 return !!found;
1148 }
1149
streq_skip_trailing_chars(const char * s1,const char * s2,const char * ok)1150 bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
1151 if (!s1 && !s2)
1152 return true;
1153 if (!s1 || !s2)
1154 return false;
1155
1156 if (!ok)
1157 ok = WHITESPACE;
1158
1159 for (; *s1 && *s2; s1++, s2++)
1160 if (*s1 != *s2)
1161 break;
1162
1163 return in_charset(s1, ok) && in_charset(s2, ok);
1164 }
1165
string_replace_char(char * str,char old_char,char new_char)1166 char *string_replace_char(char *str, char old_char, char new_char) {
1167 assert(str);
1168 assert(old_char != '\0');
1169 assert(new_char != '\0');
1170 assert(old_char != new_char);
1171
1172 for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
1173 *p = new_char;
1174
1175 return str;
1176 }
1177
strspn_from_end(const char * str,const char * accept)1178 size_t strspn_from_end(const char *str, const char *accept) {
1179 size_t n = 0;
1180
1181 if (isempty(str))
1182 return 0;
1183
1184 if (isempty(accept))
1185 return 0;
1186
1187 for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1188 n++;
1189
1190 return n;
1191 }
1192