]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.c
util: introduce memstream-util
[thirdparty/systemd.git] / src / basic / string-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
07630cea 2
11c3a366
TA
3#include <errno.h>
4#include <stdarg.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8
b5efdb8a 9#include "alloc-util.h"
8409f688 10#include "escape.h"
53cd7f33 11#include "extract-word.h"
f392dfb5 12#include "fd-util.h"
090a9c1e 13#include "fileio.h"
07630cea 14#include "gunicode.h"
c30a49b2 15#include "locale-util.h"
11c3a366 16#include "macro.h"
090a9c1e 17#include "memory-util.h"
b11d6a7b 18#include "string-util.h"
46bf625a 19#include "strv.h"
b4766d5f 20#include "terminal-util.h"
07630cea 21#include "utf8.h"
07630cea 22
07630cea
LP
23char* 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
07630cea
LP
57char *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);
fd4e991d 74 if (b > SIZE_MAX - a)
07630cea
LP
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
605405c6 88char *strjoin_real(const char *x, ...) {
07630cea 89 va_list ap;
6ced0770 90 size_t l = 1;
07630cea
LP
91 char *r, *p;
92
93 va_start(ap, x);
020003f2
RV
94 for (const char *t = x; t; t = va_arg(ap, const char *)) {
95 size_t n;
07630cea 96
020003f2
RV
97 n = strlen(t);
98 if (n > SIZE_MAX - l) {
99 va_end(ap);
100 return NULL;
07630cea 101 }
020003f2
RV
102 l += n;
103 }
07630cea
LP
104 va_end(ap);
105
6ced0770 106 p = r = new(char, l);
07630cea
LP
107 if (!r)
108 return NULL;
109
020003f2
RV
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);
07630cea 114
020003f2 115 *p = 0;
07630cea
LP
116
117 return r;
118}
119
120char *strstrip(char *s) {
7546145e
LP
121 if (!s)
122 return NULL;
123
0a6ffc5c 124 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
07630cea 125
0a6ffc5c 126 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
07630cea
LP
127}
128
129char *delete_chars(char *s, const char *bad) {
130 char *f, *t;
131
7546145e
LP
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;
07630cea
LP
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
7546145e 152char *delete_trailing_chars(char *s, const char *bad) {
a01080ce 153 char *c = s;
7546145e
LP
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
a01080ce 163 for (char *p = s; *p; p++)
7546145e
LP
164 if (!strchr(bad, *p))
165 c = p + 1;
166
167 *c = 0;
168
169 return s;
170}
171
07630cea
LP
172char *truncate_nl(char *s) {
173 assert(s);
174
175 s[strcspn(s, NEWLINE)] = 0;
176 return s;
177}
178
b577e3d5
LP
179char ascii_tolower(char x) {
180
181 if (x >= 'A' && x <= 'Z')
182 return x - 'A' + 'a';
183
184 return x;
185}
186
846b8fc3
LP
187char ascii_toupper(char x) {
188
189 if (x >= 'a' && x <= 'z')
190 return x - 'a' + 'A';
191
192 return x;
193}
194
07630cea 195char *ascii_strlower(char *t) {
07630cea
LP
196 assert(t);
197
a01080ce 198 for (char *p = t; *p; p++)
b577e3d5
LP
199 *p = ascii_tolower(*p);
200
201 return t;
202}
203
846b8fc3 204char *ascii_strupper(char *t) {
846b8fc3
LP
205 assert(t);
206
a01080ce 207 for (char *p = t; *p; p++)
846b8fc3
LP
208 *p = ascii_toupper(*p);
209
210 return t;
211}
212
b577e3d5 213char *ascii_strlower_n(char *t, size_t n) {
b577e3d5
LP
214 if (n <= 0)
215 return t;
216
a01080ce 217 for (size_t i = 0; i < n; i++)
b577e3d5 218 t[i] = ascii_tolower(t[i]);
07630cea
LP
219
220 return t;
221}
522d85ae
LP
222
223int 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}
c1749834
LP
237
238int 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
6dd91b36 245 return CMP(n, m);
c1749834 246}
07630cea
LP
247
248bool chars_intersect(const char *a, const char *b) {
07630cea 249 /* Returns true if any of the chars in a are in b. */
a01080ce 250 for (const char *p = a; *p; p++)
07630cea
LP
251 if (strchr(b, *p))
252 return true;
253
254 return false;
255}
256
257bool string_has_cc(const char *p, const char *ok) {
07630cea
LP
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
a01080ce 266 for (const char *t = p; *t; t++) {
07630cea
LP
267 if (ok && strchr(ok, *t))
268 continue;
269
6302d386 270 if (char_is_cc(*t))
07630cea
LP
271 return true;
272 }
273
274 return false;
275}
276
8409f688
ZJS
277static 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
07630cea 291static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
9924aef6
ZJS
292 size_t x, need_space, suffix_len;
293 char *t;
07630cea
LP
294
295 assert(s);
296 assert(percent <= 100);
f5fbe71d 297 assert(new_length != SIZE_MAX);
07630cea 298
c30a49b2 299 if (old_length <= new_length)
07630cea
LP
300 return strndup(s, old_length);
301
c30a49b2
LP
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
9924aef6
ZJS
329 t = new(char, new_length+3);
330 if (!t)
07630cea
LP
331 return NULL;
332
c30a49b2 333 assert(new_length >= need_space);
07630cea 334
c30a49b2
LP
335 x = ((new_length - need_space) * percent + 50) / 100;
336 assert(x <= new_length - need_space);
07630cea 337
9924aef6
ZJS
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';
07630cea 343
9924aef6 344 return t;
07630cea
LP
345}
346
347char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 348 size_t x, k, len, len2;
07630cea 349 const char *i, *j;
c30a49b2 350 char *e;
c932fb71 351 int r;
07630cea 352
c30a49b2
LP
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
07630cea
LP
365 assert(s);
366 assert(percent <= 100);
ddbc9319 367
f5fbe71d 368 if (new_length == SIZE_MAX)
ddbc9319
LP
369 return strndup(s, old_length);
370
c30a49b2
LP
371 if (new_length == 0)
372 return strdup("");
07630cea 373
c30a49b2 374 /* If no multibyte characters use ascii_ellipsize_mem for speed */
21e4e3e0 375 if (ascii_is_valid_n(s, old_length))
07630cea
LP
376 return ascii_ellipsize_mem(s, old_length, new_length, percent);
377
c30a49b2
LP
378 x = ((new_length - 1) * percent) / 100;
379 assert(x <= new_length - 1);
07630cea
LP
380
381 k = 0;
9924aef6 382 for (i = s; i < s + old_length; i = utf8_next_char(i)) {
c932fb71 383 char32_t c;
9924aef6 384 int w;
07630cea 385
c932fb71
SL
386 r = utf8_encoded_to_unichar(i, &c);
387 if (r < 0)
07630cea 388 return NULL;
07630cea 389
9924aef6
ZJS
390 w = unichar_iswide(c) ? 2 : 1;
391 if (k + w <= x)
392 k += w;
393 else
394 break;
395 }
07630cea 396
9924aef6 397 for (j = s + old_length; j > i; ) {
c932fb71 398 char32_t c;
9924aef6
ZJS
399 int w;
400 const char *jj;
07630cea 401
9924aef6
ZJS
402 jj = utf8_prev_char(j);
403 r = utf8_encoded_to_unichar(jj, &c);
c932fb71 404 if (r < 0)
07630cea 405 return NULL;
9924aef6
ZJS
406
407 w = unichar_iswide(c) ? 2 : 1;
408 if (k + w <= new_length) {
409 k += w;
410 j = jj;
411 } else
412 break;
07630cea
LP
413 }
414 assert(i <= j);
415
416 /* we don't actually need to ellipsize */
417 if (i == j)
9924aef6 418 return memdup_suffix0(s, old_length);
07630cea 419
9924aef6
ZJS
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);
07630cea
LP
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);
8409f688 438 write_ellipsis(e + len, true);
9924aef6
ZJS
439 memcpy(e + len + 3, j, len2);
440 *(e + len + 3 + len2) = '\0';
07630cea
LP
441
442 return e;
443}
444
8409f688
ZJS
445char *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
a01080ce 458 size_t i = 0, last_char_width[4] = {}, k = 0;
61f6e276
LP
459
460 assert(len > 0); /* at least a terminating NUL */
8409f688 461
61f6e276
LP
462 for (;;) {
463 char four[4];
464 int w;
8409f688 465
61f6e276 466 if (*s == 0) /* terminating NUL detected? then we are done! */
8409f688 467 goto done;
61f6e276
LP
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++;
8409f688
ZJS
483 }
484
61f6e276
LP
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 */
a01080ce 487 for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
61f6e276
LP
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];
8409f688
ZJS
498 }
499
61f6e276
LP
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
8409f688
ZJS
510 done:
511 buf[i] = '\0';
512 return buf;
513}
514
07630cea
LP
515char* strshorten(char *s, size_t l) {
516 assert(s);
517
47b33c7d 518 if (strnlen(s, l+1) > l)
07630cea
LP
519 s[l] = 0;
520
521 return s;
522}
523
2812017c
DDM
524int strgrowpad0(char **s, size_t l) {
525 assert(s);
526
527 char *q = realloc(*s, l);
528 if (!q)
529 return -ENOMEM;
530 *s = q;
531
532 size_t sz = strlen(*s);
533 memzero(*s + sz, l - sz);
534 return 0;
535}
536
07630cea 537char *strreplace(const char *text, const char *old_string, const char *new_string) {
319a4f4b 538 size_t l, old_len, new_len;
9d73565a 539 char *t, *ret = NULL;
07630cea 540 const char *f;
07630cea 541
07630cea
LP
542 assert(old_string);
543 assert(new_string);
544
9d73565a
LP
545 if (!text)
546 return NULL;
547
07630cea
LP
548 old_len = strlen(old_string);
549 new_len = strlen(new_string);
550
551 l = strlen(text);
319a4f4b 552 if (!GREEDY_REALLOC(ret, l+1))
07630cea
LP
553 return NULL;
554
555 f = text;
9d73565a 556 t = ret;
07630cea 557 while (*f) {
07630cea
LP
558 size_t d, nl;
559
560 if (!startswith(f, old_string)) {
561 *(t++) = *(f++);
562 continue;
563 }
564
9d73565a 565 d = t - ret;
07630cea 566 nl = l - old_len + new_len;
9d73565a 567
319a4f4b 568 if (!GREEDY_REALLOC(ret, nl + 1))
9d73565a 569 return mfree(ret);
07630cea
LP
570
571 l = nl;
9d73565a 572 t = ret + d;
07630cea
LP
573
574 t = stpcpy(t, new_string);
575 f += old_len;
576 }
577
578 *t = 0;
9d73565a 579 return ret;
07630cea
LP
580}
581
6fb05690
LP
582static void advance_offsets(
583 ssize_t diff,
584 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
585 size_t shift[static 2],
586 size_t size) {
587
b4766d5f
ZJS
588 if (!offsets)
589 return;
590
6fb05690
LP
591 assert(shift);
592
b4766d5f
ZJS
593 if ((size_t) diff < offsets[0])
594 shift[0] += size;
595 if ((size_t) diff < offsets[1])
596 shift[1] += size;
597}
598
599char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
62a3fc6d 600 const char *begin = NULL;
07630cea
LP
601 enum {
602 STATE_OTHER,
603 STATE_ESCAPE,
695a944c
LP
604 STATE_CSI,
605 STATE_CSO,
07630cea 606 } state = STATE_OTHER;
f392dfb5
FS
607 _cleanup_free_ char *obuf = NULL;
608 _cleanup_fclose_ FILE *f = NULL;
62a3fc6d 609 size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0;
07630cea
LP
610
611 assert(ibuf);
612 assert(*ibuf);
613
695a944c
LP
614 /* This does three things:
615 *
616 * 1. Replaces TABs by 8 spaces
617 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
618 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
62a3fc6d
ZJS
619 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
620 * other effect).
695a944c 621 *
2fe21124
ZJS
622 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
623 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
624 * supposed to suppress the most basic formatting noise, but nothing else.
695a944c
LP
625 *
626 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
07630cea
LP
627
628 isz = _isz ? *_isz : strlen(*ibuf);
629
2fe21124
ZJS
630 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
631 * created f here and it doesn't leave our scope. */
632 f = open_memstream_unlocked(&obuf, &osz);
07630cea
LP
633 if (!f)
634 return NULL;
635
62a3fc6d 636 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
07630cea
LP
637
638 switch (state) {
639
640 case STATE_OTHER:
641 if (i >= *ibuf + isz) /* EOT */
642 break;
62a3fc6d
ZJS
643
644 if (*i == '\r') {
645 n_carriage_returns++;
646 break;
647 } else if (*i == '\n')
648 /* Ignore carriage returns before new line */
649 n_carriage_returns = 0;
650 for (; n_carriage_returns > 0; n_carriage_returns--)
651 fputc('\r', f);
652
653 if (*i == '\x1B')
07630cea 654 state = STATE_ESCAPE;
b4766d5f 655 else if (*i == '\t') {
0d536673 656 fputs(" ", f);
b4766d5f
ZJS
657 advance_offsets(i - *ibuf, highlight, shift, 7);
658 } else
0d536673 659 fputc(*i, f);
b4766d5f 660
07630cea
LP
661 break;
662
663 case STATE_ESCAPE:
62a3fc6d
ZJS
664 assert(n_carriage_returns == 0);
665
07630cea 666 if (i >= *ibuf + isz) { /* EOT */
0d536673 667 fputc('\x1B', f);
b4766d5f 668 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea 669 break;
695a944c
LP
670 } else if (*i == '[') { /* ANSI CSI */
671 state = STATE_CSI;
672 begin = i + 1;
673 } else if (*i == ']') { /* ANSI CSO */
674 state = STATE_CSO;
07630cea
LP
675 begin = i + 1;
676 } else {
0d536673
LP
677 fputc('\x1B', f);
678 fputc(*i, f);
b4766d5f 679 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea
LP
680 state = STATE_OTHER;
681 }
682
683 break;
684
695a944c 685 case STATE_CSI:
62a3fc6d 686 assert(n_carriage_returns == 0);
07630cea 687
695a944c
LP
688 if (i >= *ibuf + isz || /* EOT … */
689 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
0d536673
LP
690 fputc('\x1B', f);
691 fputc('[', f);
b4766d5f 692 advance_offsets(i - *ibuf, highlight, shift, 2);
07630cea
LP
693 state = STATE_OTHER;
694 i = begin-1;
695 } else if (*i == 'm')
696 state = STATE_OTHER;
695a944c
LP
697
698 break;
699
700 case STATE_CSO:
62a3fc6d 701 assert(n_carriage_returns == 0);
695a944c
LP
702
703 if (i >= *ibuf + isz || /* EOT … */
704 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
705 fputc('\x1B', f);
706 fputc(']', f);
707 advance_offsets(i - *ibuf, highlight, shift, 2);
708 state = STATE_OTHER;
709 i = begin-1;
710 } else if (*i == '\a')
711 state = STATE_OTHER;
712
07630cea
LP
713 break;
714 }
715 }
716
f392dfb5
FS
717 if (fflush_and_check(f) < 0)
718 return NULL;
719
720 f = safe_fclose(f);
721
722 if (!obuf)
723 return NULL;
07630cea 724
6fb05690 725 free_and_replace(*ibuf, obuf);
07630cea
LP
726
727 if (_isz)
728 *_isz = osz;
729
b4766d5f
ZJS
730 if (highlight) {
731 highlight[0] += shift[0];
732 highlight[1] += shift[1];
733 }
734
6fb05690 735 return *ibuf;
07630cea
LP
736}
737
c2bc710b 738char *strextend_with_separator_internal(char **x, const char *separator, ...) {
bb8ad9ea 739 size_t f, l, l_separator;
c2bc710b
LP
740 bool need_separator;
741 char *nr, *p;
bb8ad9ea 742 va_list ap;
07630cea
LP
743
744 assert(x);
745
7bf7ce28 746 l = f = strlen_ptr(*x);
07630cea 747
bb8ad9ea
LP
748 need_separator = !isempty(*x);
749 l_separator = strlen_ptr(separator);
750
751 va_start(ap, separator);
07630cea
LP
752 for (;;) {
753 const char *t;
754 size_t n;
755
756 t = va_arg(ap, const char *);
757 if (!t)
758 break;
759
760 n = strlen(t);
bb8ad9ea
LP
761
762 if (need_separator)
763 n += l_separator;
764
c2bc710b 765 if (n >= SIZE_MAX - l) {
07630cea
LP
766 va_end(ap);
767 return NULL;
768 }
769
770 l += n;
bb8ad9ea 771 need_separator = true;
07630cea
LP
772 }
773 va_end(ap);
774
bb8ad9ea
LP
775 need_separator = !isempty(*x);
776
2a4e1fd0 777 nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
c2bc710b 778 if (!nr)
07630cea
LP
779 return NULL;
780
c2bc710b
LP
781 *x = nr;
782 p = nr + f;
07630cea 783
bb8ad9ea 784 va_start(ap, separator);
07630cea
LP
785 for (;;) {
786 const char *t;
787
788 t = va_arg(ap, const char *);
789 if (!t)
790 break;
791
bb8ad9ea
LP
792 if (need_separator && separator)
793 p = stpcpy(p, separator);
794
07630cea 795 p = stpcpy(p, t);
bb8ad9ea
LP
796
797 need_separator = true;
07630cea
LP
798 }
799 va_end(ap);
800
c2bc710b 801 assert(p == nr + l);
bb8ad9ea 802
07630cea 803 *p = 0;
07630cea 804
c2bc710b 805 return p;
07630cea
LP
806}
807
6b13ca8a
YW
808int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
809 size_t m, a, l_separator;
e9b88a6d
LP
810 va_list ap;
811 int l;
812
813 /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
814 * we'll spend a tonload of time in determining the length of the string passed in, over and over
815 * again. */
816
817 assert(x);
818 assert(format);
819
6b13ca8a
YW
820 l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
821
e9b88a6d
LP
822 /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
823 if (*x) {
824 m = strlen(*x);
6df28e1f 825 a = MALLOC_SIZEOF_SAFE(*x);
e9b88a6d
LP
826 assert(a >= m + 1);
827 } else
828 m = a = 0;
829
6b13ca8a 830 if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
e9b88a6d
LP
831 char *n;
832
6b13ca8a
YW
833 if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
834 return -ENOMEM;
835 if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
e9b88a6d
LP
836 return -ENOMEM;
837
6b13ca8a 838 n = realloc(*x, m + 64 + l_separator);
e9b88a6d
LP
839 if (!n)
840 return -ENOMEM;
841
842 *x = n;
6df28e1f 843 a = MALLOC_SIZEOF_SAFE(*x);
e9b88a6d
LP
844 }
845
846 /* Now, let's try to format the string into it */
6b13ca8a 847 memcpy_safe(*x + m, separator, l_separator);
e9b88a6d 848 va_start(ap, format);
6b13ca8a 849 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
e9b88a6d
LP
850 va_end(ap);
851
852 assert(l >= 0);
853
6b13ca8a 854 if ((size_t) l < a - m - l_separator) {
e9b88a6d
LP
855 char *n;
856
857 /* Nice! This worked. We are done. But first, let's return the extra space we don't
858 * need. This should be a cheap operation, since we only lower the allocation size here,
859 * never increase. */
6b13ca8a 860 n = realloc(*x, m + (size_t) l + l_separator + 1);
e9b88a6d
LP
861 if (n)
862 *x = n;
863 } else {
864 char *n;
865
866 /* Wasn't enough. Then let's allocate exactly what we need. */
867
6b13ca8a 868 if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
e9b88a6d 869 goto oom;
6b13ca8a 870 if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
e9b88a6d
LP
871 goto oom;
872
6b13ca8a 873 a = m + (size_t) l + l_separator + 1;
e9b88a6d
LP
874 n = realloc(*x, a);
875 if (!n)
876 goto oom;
877 *x = n;
878
879 va_start(ap, format);
6b13ca8a 880 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
e9b88a6d
LP
881 va_end(ap);
882
6b13ca8a 883 assert((size_t) l < a - m - l_separator);
e9b88a6d
LP
884 }
885
886 return 0;
887
888oom:
889 /* truncate the bytes added after the first vsnprintf() attempt again */
890 (*x)[m] = 0;
891 return -ENOMEM;
892}
893
07630cea 894char *strrep(const char *s, unsigned n) {
07630cea 895 char *r, *p;
fe96c0f8 896 size_t l;
07630cea
LP
897
898 assert(s);
899
900 l = strlen(s);
901 p = r = malloc(l * n + 1);
902 if (!r)
903 return NULL;
904
fe96c0f8 905 for (unsigned i = 0; i < n; i++)
07630cea
LP
906 p = stpcpy(p, s);
907
908 *p = 0;
909 return r;
910}
911
912int split_pair(const char *s, const char *sep, char **l, char **r) {
913 char *x, *a, *b;
914
915 assert(s);
916 assert(sep);
917 assert(l);
918 assert(r);
919
920 if (isempty(sep))
921 return -EINVAL;
922
923 x = strstr(s, sep);
924 if (!x)
925 return -EINVAL;
926
927 a = strndup(s, x - s);
928 if (!a)
929 return -ENOMEM;
930
931 b = strdup(x + strlen(sep));
932 if (!b) {
933 free(a);
934 return -ENOMEM;
935 }
936
937 *l = a;
938 *r = b;
939
940 return 0;
941}
942
943int free_and_strdup(char **p, const char *s) {
944 char *t;
945
946 assert(p);
947
7f546026 948 /* Replaces a string pointer with a strdup()ed new string,
07630cea
LP
949 * possibly freeing the old one. */
950
951 if (streq_ptr(*p, s))
952 return 0;
953
954 if (s) {
955 t = strdup(s);
956 if (!t)
957 return -ENOMEM;
958 } else
959 t = NULL;
960
d6f2cd67 961 free_and_replace(*p, t);
07630cea
LP
962
963 return 1;
964}
965
7f546026
ZJS
966int free_and_strndup(char **p, const char *s, size_t l) {
967 char *t;
968
969 assert(p);
970 assert(s || l == 0);
971
972 /* Replaces a string pointer with a strndup()ed new string,
973 * freeing the old one. */
974
975 if (!*p && !s)
976 return 0;
977
978 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
979 return 0;
980
981 if (s) {
982 t = strndup(s, l);
983 if (!t)
984 return -ENOMEM;
985 } else
986 t = NULL;
987
988 free_and_replace(*p, t);
989 return 1;
990}
991
f3e2e81d 992bool string_is_safe(const char *p) {
f3e2e81d
LP
993 if (!p)
994 return false;
995
839d1b20
LP
996 /* Checks if the specified string contains no quotes or control characters */
997
a01080ce 998 for (const char *t = p; *t; t++) {
f3e2e81d
LP
999 if (*t > 0 && *t < ' ') /* no control characters */
1000 return false;
1001
1002 if (strchr(QUOTES "\\\x7f", *t))
1003 return false;
1004 }
1005
1006 return true;
1007}
53caaffd
LP
1008
1009char* string_erase(char *x) {
1010 if (!x)
1011 return NULL;
1012
1013 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1014 * used them. */
1015 explicit_bzero_safe(x, strlen(x));
1016 return x;
1017}
8dd6491e
LP
1018
1019int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1020 const char *p = s, *e = s;
1021 bool truncation_applied = false;
1022 char *copy;
1023 size_t n = 0;
1024
1025 assert(s);
1026
1027 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1028 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1029 * generated either. */
1030
1031 for (;;) {
1032 size_t k;
1033
1034 k = strcspn(p, "\n");
1035
1036 if (p[k] == 0) {
1037 if (k == 0) /* final empty line */
1038 break;
1039
1040 if (n >= n_lines) /* above threshold */
1041 break;
1042
1043 e = p + k; /* last line to include */
1044 break;
1045 }
1046
1047 assert(p[k] == '\n');
1048
1049 if (n >= n_lines)
1050 break;
1051
1052 if (k > 0)
1053 e = p + k;
1054
1055 p += k + 1;
1056 n++;
1057 }
1058
1059 /* e points after the last character we want to keep */
1060 if (isempty(e))
1061 copy = strdup(s);
1062 else {
1063 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1064 * isn't a new-line or a series of them */
1065 truncation_applied = true;
1066
1067 copy = strndup(s, e - s);
1068 }
1069 if (!copy)
1070 return -ENOMEM;
1071
1072 *ret = copy;
1073 return truncation_applied;
1074}
f6857fa6
LP
1075
1076int string_extract_line(const char *s, size_t i, char **ret) {
1077 const char *p = s;
1078 size_t c = 0;
1079
1080 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1081 * and == 0 if we are looking at the last line or already beyond the last line. As special
1082 * optimization, if the first line is requested and the string only consists of one line we return
1083 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1084 * common case. */
1085
1086 for (;;) {
1087 const char *q;
1088
1089 q = strchr(p, '\n');
1090 if (i == c) {
1091 /* The line we are looking for! */
1092
1093 if (q) {
1094 char *m;
1095
1096 m = strndup(p, q - p);
1097 if (!m)
1098 return -ENOMEM;
1099
1100 *ret = m;
1101 return !isempty(q + 1); /* more coming? */
1102 } else {
1103 if (p == s)
1104 *ret = NULL; /* Just use the input string */
1105 else {
1106 char *m;
1107
1108 m = strdup(p);
1109 if (!m)
1110 return -ENOMEM;
1111
1112 *ret = m;
1113 }
1114
1115 return 0; /* The end */
1116 }
1117 }
1118
1119 if (!q) {
1120 char *m;
1121
1122 /* No more lines, return empty line */
1123
1124 m = strdup("");
1125 if (!m)
1126 return -ENOMEM;
1127
1128 *ret = m;
1129 return 0; /* The end */
1130 }
1131
1132 p = q + 1;
1133 c++;
1134 }
1135}
53cd7f33 1136
46bf625a 1137int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word) {
53cd7f33
ZJS
1138 /* In the default mode with no separators specified, we split on whitespace and
1139 * don't coalesce separators. */
1140 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
1141
46bf625a
ZJS
1142 const char *found = NULL;
1143
53cd7f33
ZJS
1144 for (const char *p = string;;) {
1145 _cleanup_free_ char *w = NULL;
1146 int r;
1147
1148 r = extract_first_word(&p, &w, separators, flags);
1149 if (r < 0)
1150 return r;
1151 if (r == 0)
46bf625a
ZJS
1152 break;
1153
1154 found = strv_find(words, w);
1155 if (found)
1156 break;
53cd7f33 1157 }
46bf625a
ZJS
1158
1159 if (ret_word)
1160 *ret_word = found;
1161 return !!found;
53cd7f33 1162}
8034b42c
ADT
1163
1164bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
1165 if (!s1 && !s2)
1166 return true;
1167 if (!s1 || !s2)
1168 return false;
1169
1170 if (!ok)
1171 ok = WHITESPACE;
1172
1173 for (; *s1 && *s2; s1++, s2++)
1174 if (*s1 != *s2)
1175 break;
1176
1177 return in_charset(s1, ok) && in_charset(s2, ok);
1178}
072f5f9b
YW
1179
1180char *string_replace_char(char *str, char old_char, char new_char) {
1181 assert(str);
1182 assert(old_char != '\0');
1183 assert(new_char != '\0');
1184 assert(old_char != new_char);
1185
1186 for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
1187 *p = new_char;
1188
1189 return str;
1190}
146f4482 1191
7153213e
LP
1192int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret) {
1193 char *b;
1194
1195 assert(s || n == 0);
1196 assert(mode >= 0);
1197 assert(mode < _MAKE_CSTRING_MODE_MAX);
1198
1199 /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1200 * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1201
1202 if (n == 0) {
1203 if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1204 return -EINVAL;
1205
1206 if (!ret)
1207 return 0;
1208
1209 b = new0(char, 1);
1210 } else {
1211 const char *nul;
1212
1213 nul = memchr(s, 0, n);
1214 if (nul) {
1215 if (nul < s + n - 1 || /* embedded NUL? */
1216 mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1217 return -EINVAL;
1218
1219 n--;
1220 } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1221 return -EINVAL;
1222
1223 if (!ret)
1224 return 0;
1225
1226 b = memdup_suffix0(s, n);
1227 }
1228 if (!b)
1229 return -ENOMEM;
1230
1231 *ret = b;
1232 return 0;
1233}
1234
146f4482
YW
1235size_t strspn_from_end(const char *str, const char *accept) {
1236 size_t n = 0;
1237
1238 if (isempty(str))
1239 return 0;
1240
1241 if (isempty(accept))
1242 return 0;
1243
1244 for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1245 n++;
1246
1247 return n;
1248}
e8bec624
LP
1249
1250char *strdupspn(const char *a, const char *accept) {
1251 if (isempty(a) || isempty(accept))
1252 return strdup("");
1253
1254 return strndup(a, strspn(a, accept));
1255}
1256
1257char *strdupcspn(const char *a, const char *reject) {
1258 if (isempty(a))
1259 return strdup("");
1260 if (isempty(reject))
1261 return strdup(a);
1262
1263 return strndup(a, strcspn(a, reject));
1264}
7b82d95f
LP
1265
1266char *find_line_startswith(const char *haystack, const char *needle) {
1267 char *p;
1268
1269 assert(haystack);
1270 assert(needle);
1271
1272 /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1273 * first character after it */
1274
1275 p = strstr(haystack, needle);
1276 if (!p)
1277 return NULL;
1278
1279 if (p > haystack)
1280 while (p[-1] != '\n') {
1281 p = strstr(p + 1, needle);
1282 if (!p)
1283 return NULL;
1284 }
1285
1286 return p + strlen(needle);
1287}
70cc7ed9
DDM
1288
1289char *startswith_strv(const char *string, char **strv) {
1290 char *found = NULL;
1291
1292 STRV_FOREACH(i, strv) {
1293 found = startswith(string, *i);
1294 if (found)
1295 break;
1296 }
1297
1298 return found;
1299}