]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.c
NEWS: fix typos
[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"
2485b7e2 18#include "memstream-util.h"
f5c6b4f4 19#include "path-util.h"
b11d6a7b 20#include "string-util.h"
46bf625a 21#include "strv.h"
b4766d5f 22#include "terminal-util.h"
07630cea 23#include "utf8.h"
07630cea 24
07630cea
LP
25char* first_word(const char *s, const char *word) {
26 size_t sl, wl;
27 const char *p;
28
29 assert(s);
30 assert(word);
31
32 /* Checks if the string starts with the specified word, either
33 * followed by NUL or by whitespace. Returns a pointer to the
34 * NUL or the first character after the whitespace. */
35
36 sl = strlen(s);
37 wl = strlen(word);
38
39 if (sl < wl)
40 return NULL;
41
42 if (wl == 0)
43 return (char*) s;
44
45 if (memcmp(s, word, wl) != 0)
46 return NULL;
47
48 p = s + wl;
49 if (*p == 0)
50 return (char*) p;
51
52 if (!strchr(WHITESPACE, *p))
53 return NULL;
54
55 p += strspn(p, WHITESPACE);
56 return (char*) p;
57}
58
07630cea
LP
59char *strnappend(const char *s, const char *suffix, size_t b) {
60 size_t a;
61 char *r;
62
63 if (!s && !suffix)
64 return strdup("");
65
66 if (!s)
67 return strndup(suffix, b);
68
69 if (!suffix)
70 return strdup(s);
71
72 assert(s);
73 assert(suffix);
74
75 a = strlen(s);
fd4e991d 76 if (b > SIZE_MAX - a)
07630cea
LP
77 return NULL;
78
79 r = new(char, a+b+1);
80 if (!r)
81 return NULL;
82
83 memcpy(r, s, a);
84 memcpy(r+a, suffix, b);
85 r[a+b] = 0;
86
87 return r;
88}
89
605405c6 90char *strjoin_real(const char *x, ...) {
07630cea 91 va_list ap;
6ced0770 92 size_t l = 1;
07630cea
LP
93 char *r, *p;
94
95 va_start(ap, x);
020003f2
RV
96 for (const char *t = x; t; t = va_arg(ap, const char *)) {
97 size_t n;
07630cea 98
020003f2
RV
99 n = strlen(t);
100 if (n > SIZE_MAX - l) {
101 va_end(ap);
102 return NULL;
07630cea 103 }
020003f2
RV
104 l += n;
105 }
07630cea
LP
106 va_end(ap);
107
6ced0770 108 p = r = new(char, l);
07630cea
LP
109 if (!r)
110 return NULL;
111
020003f2
RV
112 va_start(ap, x);
113 for (const char *t = x; t; t = va_arg(ap, const char *))
114 p = stpcpy(p, t);
115 va_end(ap);
07630cea 116
020003f2 117 *p = 0;
07630cea
LP
118
119 return r;
120}
121
122char *strstrip(char *s) {
7546145e
LP
123 if (!s)
124 return NULL;
125
0a6ffc5c 126 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
07630cea 127
0a6ffc5c 128 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
07630cea
LP
129}
130
131char *delete_chars(char *s, const char *bad) {
132 char *f, *t;
133
7546145e
LP
134 /* Drops all specified bad characters, regardless where in the string */
135
136 if (!s)
137 return NULL;
138
139 if (!bad)
140 bad = WHITESPACE;
07630cea
LP
141
142 for (f = s, t = s; *f; f++) {
143 if (strchr(bad, *f))
144 continue;
145
146 *(t++) = *f;
147 }
148
149 *t = 0;
150
151 return s;
152}
153
7546145e 154char *delete_trailing_chars(char *s, const char *bad) {
a01080ce 155 char *c = s;
7546145e
LP
156
157 /* Drops all specified bad characters, at the end of the string */
158
159 if (!s)
160 return NULL;
161
162 if (!bad)
163 bad = WHITESPACE;
164
a01080ce 165 for (char *p = s; *p; p++)
7546145e
LP
166 if (!strchr(bad, *p))
167 c = p + 1;
168
169 *c = 0;
170
171 return s;
172}
173
61cecfa0 174char *truncate_nl_full(char *s, size_t *ret_len) {
175 size_t n;
176
07630cea
LP
177 assert(s);
178
61cecfa0 179 n = strcspn(s, NEWLINE);
180 s[n] = '\0';
181 if (ret_len)
182 *ret_len = n;
07630cea
LP
183 return s;
184}
185
b577e3d5
LP
186char ascii_tolower(char x) {
187
188 if (x >= 'A' && x <= 'Z')
189 return x - 'A' + 'a';
190
191 return x;
192}
193
846b8fc3
LP
194char ascii_toupper(char x) {
195
196 if (x >= 'a' && x <= 'z')
197 return x - 'a' + 'A';
198
199 return x;
200}
201
07630cea 202char *ascii_strlower(char *t) {
07630cea
LP
203 assert(t);
204
a01080ce 205 for (char *p = t; *p; p++)
b577e3d5
LP
206 *p = ascii_tolower(*p);
207
208 return t;
209}
210
846b8fc3 211char *ascii_strupper(char *t) {
846b8fc3
LP
212 assert(t);
213
a01080ce 214 for (char *p = t; *p; p++)
846b8fc3
LP
215 *p = ascii_toupper(*p);
216
217 return t;
218}
219
b577e3d5 220char *ascii_strlower_n(char *t, size_t n) {
b577e3d5
LP
221 if (n <= 0)
222 return t;
223
a01080ce 224 for (size_t i = 0; i < n; i++)
b577e3d5 225 t[i] = ascii_tolower(t[i]);
07630cea
LP
226
227 return t;
228}
522d85ae
LP
229
230int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
231
232 for (; n > 0; a++, b++, n--) {
233 int x, y;
234
235 x = (int) (uint8_t) ascii_tolower(*a);
236 y = (int) (uint8_t) ascii_tolower(*b);
237
238 if (x != y)
239 return x - y;
240 }
241
242 return 0;
243}
c1749834
LP
244
245int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
246 int r;
247
248 r = ascii_strcasecmp_n(a, b, MIN(n, m));
249 if (r != 0)
250 return r;
251
6dd91b36 252 return CMP(n, m);
c1749834 253}
07630cea
LP
254
255bool chars_intersect(const char *a, const char *b) {
07630cea 256 /* Returns true if any of the chars in a are in b. */
a01080ce 257 for (const char *p = a; *p; p++)
07630cea
LP
258 if (strchr(b, *p))
259 return true;
260
261 return false;
262}
263
264bool string_has_cc(const char *p, const char *ok) {
07630cea
LP
265 assert(p);
266
267 /*
268 * Check if a string contains control characters. If 'ok' is
269 * non-NULL it may be a string containing additional CCs to be
270 * considered OK.
271 */
272
a01080ce 273 for (const char *t = p; *t; t++) {
07630cea
LP
274 if (ok && strchr(ok, *t))
275 continue;
276
6302d386 277 if (char_is_cc(*t))
07630cea
LP
278 return true;
279 }
280
281 return false;
282}
283
8409f688
ZJS
284static int write_ellipsis(char *buf, bool unicode) {
285 if (unicode || is_locale_utf8()) {
286 buf[0] = 0xe2; /* tri-dot ellipsis: … */
287 buf[1] = 0x80;
288 buf[2] = 0xa6;
289 } else {
290 buf[0] = '.';
291 buf[1] = '.';
292 buf[2] = '.';
293 }
294
295 return 3;
296}
297
07630cea 298static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
9924aef6
ZJS
299 size_t x, need_space, suffix_len;
300 char *t;
07630cea
LP
301
302 assert(s);
303 assert(percent <= 100);
f5fbe71d 304 assert(new_length != SIZE_MAX);
07630cea 305
c30a49b2 306 if (old_length <= new_length)
07630cea
LP
307 return strndup(s, old_length);
308
c30a49b2
LP
309 /* Special case short ellipsations */
310 switch (new_length) {
311
312 case 0:
313 return strdup("");
314
315 case 1:
316 if (is_locale_utf8())
317 return strdup("…");
318 else
319 return strdup(".");
320
321 case 2:
322 if (!is_locale_utf8())
323 return strdup("..");
324
325 break;
326
327 default:
328 break;
329 }
330
331 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
332 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
333 * either for the UTF-8 encoded character or for three ASCII characters. */
334 need_space = is_locale_utf8() ? 1 : 3;
335
9924aef6
ZJS
336 t = new(char, new_length+3);
337 if (!t)
07630cea
LP
338 return NULL;
339
c30a49b2 340 assert(new_length >= need_space);
07630cea 341
c30a49b2
LP
342 x = ((new_length - need_space) * percent + 50) / 100;
343 assert(x <= new_length - need_space);
07630cea 344
9924aef6
ZJS
345 memcpy(t, s, x);
346 write_ellipsis(t + x, false);
347 suffix_len = new_length - x - need_space;
348 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
349 *(t + x + 3 + suffix_len) = '\0';
07630cea 350
9924aef6 351 return t;
07630cea
LP
352}
353
354char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 355 size_t x, k, len, len2;
07630cea 356 const char *i, *j;
c30a49b2 357 char *e;
c932fb71 358 int r;
07630cea 359
c30a49b2
LP
360 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
361 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
362 * strings.
363 *
364 * Ellipsation is done in a locale-dependent way:
365 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
366 * 2. Otherwise, a unicode ellipsis is used ("…")
367 *
368 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
369 * the current locale is UTF-8.
370 */
371
07630cea
LP
372 assert(s);
373 assert(percent <= 100);
ddbc9319 374
f5fbe71d 375 if (new_length == SIZE_MAX)
ddbc9319
LP
376 return strndup(s, old_length);
377
c30a49b2
LP
378 if (new_length == 0)
379 return strdup("");
07630cea 380
c30a49b2 381 /* If no multibyte characters use ascii_ellipsize_mem for speed */
21e4e3e0 382 if (ascii_is_valid_n(s, old_length))
07630cea
LP
383 return ascii_ellipsize_mem(s, old_length, new_length, percent);
384
c30a49b2
LP
385 x = ((new_length - 1) * percent) / 100;
386 assert(x <= new_length - 1);
07630cea
LP
387
388 k = 0;
9924aef6 389 for (i = s; i < s + old_length; i = utf8_next_char(i)) {
c932fb71 390 char32_t c;
9924aef6 391 int w;
07630cea 392
c932fb71
SL
393 r = utf8_encoded_to_unichar(i, &c);
394 if (r < 0)
07630cea 395 return NULL;
07630cea 396
9924aef6
ZJS
397 w = unichar_iswide(c) ? 2 : 1;
398 if (k + w <= x)
399 k += w;
400 else
401 break;
402 }
07630cea 403
9924aef6 404 for (j = s + old_length; j > i; ) {
c932fb71 405 char32_t c;
9924aef6
ZJS
406 int w;
407 const char *jj;
07630cea 408
9924aef6
ZJS
409 jj = utf8_prev_char(j);
410 r = utf8_encoded_to_unichar(jj, &c);
c932fb71 411 if (r < 0)
07630cea 412 return NULL;
9924aef6
ZJS
413
414 w = unichar_iswide(c) ? 2 : 1;
415 if (k + w <= new_length) {
416 k += w;
417 j = jj;
418 } else
419 break;
07630cea
LP
420 }
421 assert(i <= j);
422
423 /* we don't actually need to ellipsize */
424 if (i == j)
9924aef6 425 return memdup_suffix0(s, old_length);
07630cea 426
9924aef6
ZJS
427 /* make space for ellipsis, if possible */
428 if (j < s + old_length)
429 j = utf8_next_char(j);
430 else if (i > s)
431 i = utf8_prev_char(i);
07630cea
LP
432
433 len = i - s;
434 len2 = s + old_length - j;
435 e = new(char, len + 3 + len2 + 1);
436 if (!e)
437 return NULL;
438
439 /*
440 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
441 old_length, new_length, x, len, len2, k);
442 */
443
444 memcpy(e, s, len);
8409f688 445 write_ellipsis(e + len, true);
9924aef6
ZJS
446 memcpy(e + len + 3, j, len2);
447 *(e + len + 3 + len2) = '\0';
07630cea
LP
448
449 return e;
450}
451
8409f688
ZJS
452char *cellescape(char *buf, size_t len, const char *s) {
453 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
454 * characters are copied as they are, everything else is escaped. The result
455 * is different then if escaping and ellipsization was performed in two
456 * separate steps, because each sequence is either stored in full or skipped.
457 *
458 * This function should be used for logging about strings which expected to
459 * be plain ASCII in a safe way.
460 *
461 * An ellipsis will be used if s is too long. It was always placed at the
462 * very end.
463 */
464
a01080ce 465 size_t i = 0, last_char_width[4] = {}, k = 0;
61f6e276
LP
466
467 assert(len > 0); /* at least a terminating NUL */
8409f688 468
61f6e276
LP
469 for (;;) {
470 char four[4];
471 int w;
8409f688 472
61f6e276 473 if (*s == 0) /* terminating NUL detected? then we are done! */
8409f688 474 goto done;
61f6e276
LP
475
476 w = cescape_char(*s, four);
477 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
478 * ellipsize at the previous location */
479 break;
480
481 /* OK, there was space, let's add this escaped character to the buffer */
482 memcpy(buf + i, four, w);
483 i += w;
484
485 /* And remember its width in the ring buffer */
486 last_char_width[k] = w;
487 k = (k + 1) % 4;
488
489 s++;
8409f688
ZJS
490 }
491
61f6e276
LP
492 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
493 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
a01080ce 494 for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
61f6e276
LP
495
496 if (i + 4 <= len) /* nice, we reached our space goal */
497 break;
498
499 k = k == 0 ? 3 : k - 1;
500 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
501 break;
502
503 assert(i >= last_char_width[k]);
504 i -= last_char_width[k];
8409f688
ZJS
505 }
506
61f6e276
LP
507 if (i + 4 <= len) /* yay, enough space */
508 i += write_ellipsis(buf + i, false);
509 else if (i + 3 <= len) { /* only space for ".." */
510 buf[i++] = '.';
511 buf[i++] = '.';
512 } else if (i + 2 <= len) /* only space for a single "." */
513 buf[i++] = '.';
514 else
515 assert(i + 1 <= len);
516
8409f688
ZJS
517 done:
518 buf[i] = '\0';
519 return buf;
520}
521
07630cea
LP
522char* strshorten(char *s, size_t l) {
523 assert(s);
524
47b33c7d 525 if (strnlen(s, l+1) > l)
07630cea
LP
526 s[l] = 0;
527
528 return s;
529}
530
2812017c
DDM
531int strgrowpad0(char **s, size_t l) {
532 assert(s);
533
534 char *q = realloc(*s, l);
535 if (!q)
536 return -ENOMEM;
537 *s = q;
538
539 size_t sz = strlen(*s);
540 memzero(*s + sz, l - sz);
541 return 0;
542}
543
07630cea 544char *strreplace(const char *text, const char *old_string, const char *new_string) {
319a4f4b 545 size_t l, old_len, new_len;
9d73565a 546 char *t, *ret = NULL;
07630cea 547 const char *f;
07630cea 548
07630cea
LP
549 assert(old_string);
550 assert(new_string);
551
9d73565a
LP
552 if (!text)
553 return NULL;
554
07630cea
LP
555 old_len = strlen(old_string);
556 new_len = strlen(new_string);
557
558 l = strlen(text);
319a4f4b 559 if (!GREEDY_REALLOC(ret, l+1))
07630cea
LP
560 return NULL;
561
562 f = text;
9d73565a 563 t = ret;
07630cea 564 while (*f) {
07630cea
LP
565 size_t d, nl;
566
567 if (!startswith(f, old_string)) {
568 *(t++) = *(f++);
569 continue;
570 }
571
9d73565a 572 d = t - ret;
07630cea 573 nl = l - old_len + new_len;
9d73565a 574
319a4f4b 575 if (!GREEDY_REALLOC(ret, nl + 1))
9d73565a 576 return mfree(ret);
07630cea
LP
577
578 l = nl;
9d73565a 579 t = ret + d;
07630cea
LP
580
581 t = stpcpy(t, new_string);
582 f += old_len;
583 }
584
585 *t = 0;
9d73565a 586 return ret;
07630cea
LP
587}
588
6fb05690
LP
589static void advance_offsets(
590 ssize_t diff,
591 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
592 size_t shift[static 2],
593 size_t size) {
594
b4766d5f
ZJS
595 if (!offsets)
596 return;
597
6fb05690
LP
598 assert(shift);
599
b4766d5f
ZJS
600 if ((size_t) diff < offsets[0])
601 shift[0] += size;
602 if ((size_t) diff < offsets[1])
603 shift[1] += size;
604}
605
606char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
62a3fc6d 607 const char *begin = NULL;
07630cea
LP
608 enum {
609 STATE_OTHER,
610 STATE_ESCAPE,
695a944c
LP
611 STATE_CSI,
612 STATE_CSO,
07630cea 613 } state = STATE_OTHER;
2485b7e2
YW
614 _cleanup_(memstream_done) MemStream m = {};
615 size_t isz, shift[2] = {}, n_carriage_returns = 0;
616 FILE *f;
07630cea
LP
617
618 assert(ibuf);
619 assert(*ibuf);
620
695a944c
LP
621 /* This does three things:
622 *
623 * 1. Replaces TABs by 8 spaces
624 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
625 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
62a3fc6d
ZJS
626 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
627 * other effect).
695a944c 628 *
2fe21124
ZJS
629 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
630 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
631 * supposed to suppress the most basic formatting noise, but nothing else.
695a944c
LP
632 *
633 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
07630cea
LP
634
635 isz = _isz ? *_isz : strlen(*ibuf);
636
2fe21124
ZJS
637 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
638 * created f here and it doesn't leave our scope. */
2485b7e2 639 f = memstream_init(&m);
07630cea
LP
640 if (!f)
641 return NULL;
642
62a3fc6d 643 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
07630cea
LP
644
645 switch (state) {
646
647 case STATE_OTHER:
648 if (i >= *ibuf + isz) /* EOT */
649 break;
62a3fc6d
ZJS
650
651 if (*i == '\r') {
652 n_carriage_returns++;
653 break;
654 } else if (*i == '\n')
655 /* Ignore carriage returns before new line */
656 n_carriage_returns = 0;
657 for (; n_carriage_returns > 0; n_carriage_returns--)
658 fputc('\r', f);
659
660 if (*i == '\x1B')
07630cea 661 state = STATE_ESCAPE;
b4766d5f 662 else if (*i == '\t') {
0d536673 663 fputs(" ", f);
b4766d5f
ZJS
664 advance_offsets(i - *ibuf, highlight, shift, 7);
665 } else
0d536673 666 fputc(*i, f);
b4766d5f 667
07630cea
LP
668 break;
669
670 case STATE_ESCAPE:
62a3fc6d
ZJS
671 assert(n_carriage_returns == 0);
672
07630cea 673 if (i >= *ibuf + isz) { /* EOT */
0d536673 674 fputc('\x1B', f);
b4766d5f 675 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea 676 break;
695a944c
LP
677 } else if (*i == '[') { /* ANSI CSI */
678 state = STATE_CSI;
679 begin = i + 1;
680 } else if (*i == ']') { /* ANSI CSO */
681 state = STATE_CSO;
07630cea
LP
682 begin = i + 1;
683 } else {
0d536673
LP
684 fputc('\x1B', f);
685 fputc(*i, f);
b4766d5f 686 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea
LP
687 state = STATE_OTHER;
688 }
689
690 break;
691
695a944c 692 case STATE_CSI:
62a3fc6d 693 assert(n_carriage_returns == 0);
07630cea 694
695a944c
LP
695 if (i >= *ibuf + isz || /* EOT … */
696 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
0d536673
LP
697 fputc('\x1B', f);
698 fputc('[', f);
b4766d5f 699 advance_offsets(i - *ibuf, highlight, shift, 2);
07630cea
LP
700 state = STATE_OTHER;
701 i = begin-1;
702 } else if (*i == 'm')
703 state = STATE_OTHER;
695a944c
LP
704
705 break;
706
707 case STATE_CSO:
62a3fc6d 708 assert(n_carriage_returns == 0);
695a944c
LP
709
710 if (i >= *ibuf + isz || /* EOT … */
711 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
712 fputc('\x1B', f);
713 fputc(']', f);
714 advance_offsets(i - *ibuf, highlight, shift, 2);
715 state = STATE_OTHER;
716 i = begin-1;
717 } else if (*i == '\a')
718 state = STATE_OTHER;
719
07630cea
LP
720 break;
721 }
722 }
723
2485b7e2
YW
724 char *obuf;
725 if (memstream_finalize(&m, &obuf, _isz) < 0)
f392dfb5 726 return NULL;
07630cea 727
6fb05690 728 free_and_replace(*ibuf, obuf);
07630cea 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}
f5c6b4f4
LP
1300
1301bool version_is_valid(const char *s) {
1302 if (isempty(s))
1303 return false;
1304
1305 if (!filename_part_is_valid(s))
1306 return false;
1307
1308 /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1309 if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
1310 return false;
1311
1312 return true;
1313}