]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.c
425f9742375a9511b8fd2c45af64b5b2ba05d7fc
[thirdparty/systemd.git] / src / basic / string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "escape.h"
7 #include "extract-word.h"
8 #include "glyph-util.h"
9 #include "gunicode.h"
10 #include "locale-util.h"
11 #include "log.h"
12 #include "memory-util.h"
13 #include "memstream-util.h"
14 #include "path-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "terminal-util.h"
18 #include "utf8.h"
19
20 char* first_word(const char *s, const char *word) {
21 assert(s);
22 assert(word);
23
24 /* Checks if the string starts with the specified word, either followed by NUL or by whitespace.
25 * Returns a pointer to the NUL or the first character after the whitespace. */
26
27 if (isempty(word))
28 return (char*) s;
29
30 const char *p = startswith(s, word);
31 if (!p)
32 return NULL;
33 if (*p == '\0')
34 return (char*) p;
35
36 const char *nw = skip_leading_chars(p, WHITESPACE);
37 if (p == nw)
38 return NULL;
39
40 return (char*) nw;
41 }
42
43 char* strextendn(char **x, const char *s, size_t l) {
44 assert(x);
45 assert(s || l == 0);
46
47 if (l > 0)
48 l = strnlen(s, l); /* ignore trailing noise */
49
50 if (l > 0 || !*x) {
51 size_t q;
52 char *m;
53
54 q = strlen_ptr(*x);
55 m = realloc(*x, q + l + 1);
56 if (!m)
57 return NULL;
58
59 *mempcpy_typesafe(m + q, s, l) = 0;
60
61 *x = m;
62 }
63
64 return *x;
65 }
66
67 char* strstrip(char *s) {
68 if (!s)
69 return NULL;
70
71 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
72
73 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
74 }
75
76 char* delete_chars(char *s, const char *bad) {
77 char *f, *t;
78
79 /* Drops all specified bad characters, regardless where in the string */
80
81 if (!s)
82 return NULL;
83
84 if (!bad)
85 bad = WHITESPACE;
86
87 for (f = s, t = s; *f; f++) {
88 if (strchr(bad, *f))
89 continue;
90
91 *(t++) = *f;
92 }
93
94 *t = 0;
95
96 return s;
97 }
98
99 char* delete_trailing_chars(char *s, const char *bad) {
100 char *c = s;
101
102 /* Drops all specified bad characters, at the end of the string */
103
104 if (!s)
105 return NULL;
106
107 if (!bad)
108 bad = WHITESPACE;
109
110 for (char *p = s; *p; p++)
111 if (!strchr(bad, *p))
112 c = p + 1;
113
114 *c = 0;
115
116 return s;
117 }
118
119 char* truncate_nl_full(char *s, size_t *ret_len) {
120 size_t n;
121
122 assert(s);
123
124 n = strcspn(s, NEWLINE);
125 s[n] = '\0';
126 if (ret_len)
127 *ret_len = n;
128 return s;
129 }
130
131 char ascii_tolower(char x) {
132
133 if (x >= 'A' && x <= 'Z')
134 return x - 'A' + 'a';
135
136 return x;
137 }
138
139 char ascii_toupper(char x) {
140
141 if (x >= 'a' && x <= 'z')
142 return x - 'a' + 'A';
143
144 return x;
145 }
146
147 char* ascii_strlower(char *t) {
148 assert(t);
149
150 for (char *p = t; *p; p++)
151 *p = ascii_tolower(*p);
152
153 return t;
154 }
155
156 char* ascii_strupper(char *t) {
157 assert(t);
158
159 for (char *p = t; *p; p++)
160 *p = ascii_toupper(*p);
161
162 return t;
163 }
164
165 char* ascii_strlower_n(char *t, size_t n) {
166 if (n <= 0)
167 return t;
168
169 for (size_t i = 0; i < n; i++)
170 t[i] = ascii_tolower(t[i]);
171
172 return t;
173 }
174
175 int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
176
177 for (; n > 0; a++, b++, n--) {
178 int x, y;
179
180 x = (int) (uint8_t) ascii_tolower(*a);
181 y = (int) (uint8_t) ascii_tolower(*b);
182
183 if (x != y)
184 return x - y;
185 }
186
187 return 0;
188 }
189
190 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
191 int r;
192
193 r = ascii_strcasecmp_n(a, b, MIN(n, m));
194 if (r != 0)
195 return r;
196
197 return CMP(n, m);
198 }
199
200 bool chars_intersect(const char *a, const char *b) {
201 /* Returns true if any of the chars in a are in b. */
202 for (const char *p = a; *p; p++)
203 if (strchr(b, *p))
204 return true;
205
206 return false;
207 }
208
209 bool string_has_cc(const char *p, const char *ok) {
210 assert(p);
211
212 /*
213 * Check if a string contains control characters. If 'ok' is
214 * non-NULL it may be a string containing additional CCs to be
215 * considered OK.
216 */
217
218 for (const char *t = p; *t; t++) {
219 if (ok && strchr(ok, *t))
220 continue;
221
222 if (char_is_cc(*t))
223 return true;
224 }
225
226 return false;
227 }
228
229 static int write_ellipsis(char *buf, bool unicode) {
230 const char *s = glyph_full(GLYPH_ELLIPSIS, unicode);
231 assert(strlen(s) == 3);
232 memcpy(buf, s, 3);
233 return 3;
234 }
235
236 static size_t ansi_sequence_length(const char *s, size_t len) {
237 assert(s);
238
239 if (len < 2)
240 return 0;
241
242 if (s[0] != 0x1B) /* ASCII 27, aka ESC, aka Ctrl-[ */
243 return 0; /* Not the start of a sequence */
244
245 if (s[1] == 0x5B) { /* [, start of CSI sequence */
246 size_t i = 2;
247
248 if (i == len)
249 return 0;
250
251 while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */
252 if (++i == len)
253 return 0;
254 while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */
255 if (++i == len)
256 return 0;
257 if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */
258 return i + 1;
259 return 0; /* Bad sequence */
260
261 } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */
262 return 2;
263
264 return 0; /* Bad escape? */
265 }
266
267 static bool string_has_ansi_sequence(const char *s, size_t len) {
268 const char *t = s;
269
270 while ((t = memchr(s, 0x1B, len - (t - s))))
271 if (ansi_sequence_length(t, len - (t - s)) > 0)
272 return true;
273 return false;
274 }
275
276 static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) {
277 /* Locate the previous ANSI sequence and save its start in *ret_where and return length. */
278
279 for (size_t i = length - 2; i > 0; i--) { /* -2 because at least two bytes are needed */
280 size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1));
281 if (slen == 0)
282 continue;
283
284 *ret_where = s + (i - 1);
285 return slen;
286 }
287
288 *ret_where = NULL;
289 return 0;
290 }
291
292 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
293 size_t x, need_space, suffix_len;
294 char *t;
295
296 assert(s);
297 assert(percent <= 100);
298 assert(new_length != SIZE_MAX);
299
300 if (old_length <= new_length)
301 return strndup(s, old_length);
302
303 /* Special case short ellipsations */
304 switch (new_length) {
305
306 case 0:
307 return strdup("");
308
309 case 1:
310 if (is_locale_utf8())
311 return strdup("…");
312 else
313 return strdup(".");
314
315 case 2:
316 if (!is_locale_utf8())
317 return strdup("..");
318 break;
319 }
320
321 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
322 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
323 * either for the UTF-8 encoded character or for three ASCII characters. */
324 need_space = is_locale_utf8() ? 1 : 3;
325
326 t = new(char, new_length+3);
327 if (!t)
328 return NULL;
329
330 assert(new_length >= need_space);
331
332 x = ((new_length - need_space) * percent + 50) / 100;
333 assert(x <= new_length - need_space);
334
335 write_ellipsis(mempcpy(t, s, x), /* unicode = */ false);
336 suffix_len = new_length - x - need_space;
337 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
338 *(t + x + 3 + suffix_len) = '\0';
339
340 return t;
341 }
342
343 char* ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
344 size_t x, k, len, len2;
345 const char *i, *j;
346 int r;
347
348 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
349 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
350 * strings.
351 *
352 * Ellipsation is done in a locale-dependent way:
353 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
354 * 2. Otherwise, a unicode ellipsis is used ("…")
355 *
356 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
357 * the current locale is UTF-8.
358 */
359
360 assert(s);
361 assert(percent <= 100);
362
363 if (new_length == SIZE_MAX)
364 return strndup(s, old_length);
365
366 if (new_length == 0)
367 return strdup("");
368
369 bool has_ansi_seq = string_has_ansi_sequence(s, old_length);
370
371 /* If no multibyte characters or ANSI sequences, use ascii_ellipsize_mem for speed */
372 if (!has_ansi_seq && ascii_is_valid_n(s, old_length))
373 return ascii_ellipsize_mem(s, old_length, new_length, percent);
374
375 x = (new_length - 1) * percent / 100;
376 assert(x <= new_length - 1);
377
378 k = 0;
379 for (i = s; i < s + old_length; ) {
380 size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0;
381 if (slen > 0) {
382 i += slen;
383 continue; /* ANSI sequences don't take up any space in output */
384 }
385
386 char32_t c;
387 r = utf8_encoded_to_unichar(i, &c);
388 if (r < 0)
389 return NULL;
390
391 int w = unichar_iswide(c) ? 2 : 1;
392 if (k + w > x)
393 break;
394
395 k += w;
396 i += r;
397 }
398
399 const char *ansi_start = s + old_length;
400 size_t ansi_len = 0;
401
402 for (const char *t = j = s + old_length; t > i && k < new_length; ) {
403 char32_t c;
404 int w;
405 const char *tt;
406
407 if (has_ansi_seq && ansi_start >= t)
408 /* Figure out the previous ANSI sequence, if any */
409 ansi_len = previous_ansi_sequence(s, t - s, &ansi_start);
410
411 /* If the sequence extends all the way to the current position, skip it. */
412 if (has_ansi_seq && ansi_len > 0 && ansi_start + ansi_len == t) {
413 t = ansi_start;
414 continue;
415 }
416
417 tt = utf8_prev_char(t);
418 r = utf8_encoded_to_unichar(tt, &c);
419 if (r < 0)
420 return NULL;
421
422 w = unichar_iswide(c) ? 2 : 1;
423 if (k + w > new_length)
424 break;
425
426 k += w;
427 j = t = tt; /* j should always point to the first "real" character */
428 }
429
430 /* We don't actually need to ellipsize */
431 if (i >= j)
432 return memdup_suffix0(s, old_length);
433
434 if (k >= new_length) {
435 /* Make space for ellipsis, if required and possible. We know that the edge character is not
436 * part of an ANSI sequence (because then we'd skip it). If the last character we looked at
437 * was wide, we don't need to make space. */
438 if (j < s + old_length)
439 j = utf8_next_char(j);
440 else if (i > s)
441 i = utf8_prev_char(i);
442 }
443
444 len = i - s;
445 len2 = s + old_length - j;
446
447 /* If we have ANSI, allow the same length as the source string + ellipsis. It'd be too involved to
448 * figure out what exact space is needed. Strings with ANSI sequences are most likely to be fairly
449 * short anyway. */
450 size_t alloc_len = has_ansi_seq ? old_length + 3 + 1 : len + 3 + len2 + 1;
451
452 char *e = new(char, alloc_len);
453 if (!e)
454 return NULL;
455
456 memcpy_safe(e, s, len);
457 write_ellipsis(e + len, /* unicode = */ true);
458
459 char *dst = e + len + 3;
460
461 if (has_ansi_seq)
462 /* Copy over any ANSI sequences in full */
463 for (const char *p = s + len; p < j; ) {
464 size_t slen = ansi_sequence_length(p, j - p);
465 if (slen > 0) {
466 dst = mempcpy(dst, p, slen);
467 p += slen;
468 } else
469 p = utf8_next_char(p);
470 }
471
472 memcpy_safe(dst, j, len2);
473 dst[len2] = '\0';
474
475 return e;
476 }
477
478 char* cellescape(char *buf, size_t len, const char *s) {
479 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
480 * characters are copied as they are, everything else is escaped. The result
481 * is different then if escaping and ellipsization was performed in two
482 * separate steps, because each sequence is either stored in full or skipped.
483 *
484 * This function should be used for logging about strings which expected to
485 * be plain ASCII in a safe way.
486 *
487 * An ellipsis will be used if s is too long. It was always placed at the
488 * very end.
489 */
490
491 size_t i = 0, last_char_width[4] = {}, k = 0;
492
493 assert(buf);
494 assert(len > 0); /* at least a terminating NUL */
495 assert(s);
496
497 for (;;) {
498 char four[4];
499 int w;
500
501 if (*s == 0) /* terminating NUL detected? then we are done! */
502 goto done;
503
504 w = cescape_char(*s, four);
505 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
506 * ellipsize at the previous location */
507 break;
508
509 /* OK, there was space, let's add this escaped character to the buffer */
510 memcpy(buf + i, four, w);
511 i += w;
512
513 /* And remember its width in the ring buffer */
514 last_char_width[k] = w;
515 k = (k + 1) % 4;
516
517 s++;
518 }
519
520 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
521 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
522 for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
523
524 if (i + 4 <= len) /* nice, we reached our space goal */
525 break;
526
527 k = k == 0 ? 3 : k - 1;
528 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
529 break;
530
531 assert(i >= last_char_width[k]);
532 i -= last_char_width[k];
533 }
534
535 if (i + 4 <= len) /* yay, enough space */
536 i += write_ellipsis(buf + i, /* unicode = */ false);
537 else if (i + 3 <= len) { /* only space for ".." */
538 buf[i++] = '.';
539 buf[i++] = '.';
540 } else if (i + 2 <= len) /* only space for a single "." */
541 buf[i++] = '.';
542 else
543 assert(i + 1 <= len);
544
545 done:
546 buf[i] = '\0';
547 return buf;
548 }
549
550 char* strshorten(char *s, size_t l) {
551 assert(s);
552
553 if (l >= SIZE_MAX-1) /* Would not change anything */
554 return s;
555
556 if (strnlen(s, l+1) > l)
557 s[l] = 0;
558
559 return s;
560 }
561
562 int strgrowpad0(char **s, size_t l) {
563 size_t sz;
564
565 assert(s);
566
567 if (*s) {
568 sz = strlen(*s) + 1;
569 if (sz >= l) /* never shrink */
570 return 0;
571 } else
572 sz = 0;
573
574 char *q = realloc(*s, l);
575 if (!q)
576 return -ENOMEM;
577
578 *s = q;
579
580 memzero(*s + sz, l - sz);
581 return 0;
582 }
583
584 char* strreplace(const char *text, const char *old_string, const char *new_string) {
585 size_t l, old_len, new_len;
586 char *t, *ret = NULL;
587 const char *f;
588
589 assert(old_string);
590 assert(new_string);
591
592 if (!text)
593 return NULL;
594
595 old_len = strlen(old_string);
596 new_len = strlen(new_string);
597
598 l = strlen(text);
599 if (!GREEDY_REALLOC(ret, l+1))
600 return NULL;
601
602 f = text;
603 t = ret;
604 while (*f) {
605 size_t d, nl;
606
607 if (!startswith(f, old_string)) {
608 *(t++) = *(f++);
609 continue;
610 }
611
612 d = t - ret;
613 nl = l - old_len + new_len;
614
615 if (!GREEDY_REALLOC(ret, nl + 1))
616 return mfree(ret);
617
618 l = nl;
619 t = ret + d;
620
621 t = stpcpy(t, new_string);
622 f += old_len;
623 }
624
625 *t = 0;
626 return ret;
627 }
628
629 static void advance_offsets(
630 ssize_t diff,
631 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
632 size_t shift[static 2],
633 size_t size) {
634
635 if (!offsets)
636 return;
637
638 assert(shift);
639
640 if ((size_t) diff < offsets[0])
641 shift[0] += size;
642 if ((size_t) diff < offsets[1])
643 shift[1] += size;
644 }
645
646 char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
647 const char *begin = NULL;
648 enum {
649 STATE_OTHER,
650 STATE_ESCAPE,
651 STATE_CSI,
652 STATE_OSC,
653 STATE_OSC_CLOSING,
654 } state = STATE_OTHER;
655 _cleanup_(memstream_done) MemStream m = {};
656 size_t isz, shift[2] = {}, n_carriage_returns = 0;
657 FILE *f;
658
659 assert(ibuf);
660 assert(*ibuf);
661
662 /* This does three things:
663 *
664 * 1. Replaces TABs by 8 spaces
665 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
666 * 3. Strips ANSI operating system sequences (OSC), i.e. ESC ']' … ST sequences
667 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
668 * other effect).
669 *
670 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
671 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
672 * supposed to suppress the most basic formatting noise, but nothing else.
673 *
674 * Why care for OSC sequences? Well, to undo what terminal_urlify() and friends generate. */
675
676 isz = _isz ? *_isz : strlen(*ibuf);
677
678 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
679 * created f here and it doesn't leave our scope. */
680 f = memstream_init(&m);
681 if (!f)
682 return NULL;
683
684 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
685
686 bool eot = i >= *ibuf + isz;
687
688 switch (state) {
689
690 case STATE_OTHER:
691 if (eot)
692 break;
693
694 if (*i == '\r') {
695 n_carriage_returns++;
696 break;
697 } else if (*i == '\n')
698 /* Ignore carriage returns before new line */
699 n_carriage_returns = 0;
700 for (; n_carriage_returns > 0; n_carriage_returns--)
701 fputc('\r', f);
702
703 if (*i == '\x1B')
704 state = STATE_ESCAPE;
705 else if (*i == '\t') {
706 fputs(" ", f);
707 advance_offsets(i - *ibuf, highlight, shift, 7);
708 } else
709 fputc(*i, f);
710
711 break;
712
713 case STATE_ESCAPE:
714 assert(n_carriage_returns == 0);
715
716 if (eot) {
717 fputc('\x1B', f);
718 advance_offsets(i - *ibuf, highlight, shift, 1);
719 break;
720 } else if (*i == '[') { /* ANSI CSI */
721 state = STATE_CSI;
722 begin = i + 1;
723 } else if (*i == ']') { /* ANSI OSC */
724 state = STATE_OSC;
725 begin = i + 1;
726 } else {
727 fputc('\x1B', f);
728 fputc(*i, f);
729 advance_offsets(i - *ibuf, highlight, shift, 1);
730 state = STATE_OTHER;
731 }
732
733 break;
734
735 case STATE_CSI:
736 assert(n_carriage_returns == 0);
737
738 if (eot || !strchr("01234567890;m", *i)) { /* EOT or invalid chars in sequence */
739 fputc('\x1B', f);
740 fputc('[', f);
741 advance_offsets(i - *ibuf, highlight, shift, 2);
742 state = STATE_OTHER;
743 i = begin-1;
744 } else if (*i == 'm')
745 state = STATE_OTHER;
746
747 break;
748
749 case STATE_OSC:
750 assert(n_carriage_returns == 0);
751
752 /* There are three kinds of OSC terminators: \x07, \x1b\x5c or \x9c. We only support
753 * the first two, because the last one is a valid UTF-8 codepoint and hence creates
754 * an ambiguity (many Terminal emulators refuse to support it as well). */
755 if (eot || (!IN_SET(*i, '\x07', '\x1b') && !osc_char_is_valid(*i))) { /* EOT or invalid chars in sequence */
756 fputc('\x1B', f);
757 fputc(']', f);
758 advance_offsets(i - *ibuf, highlight, shift, 2);
759 state = STATE_OTHER;
760 i = begin-1;
761 } else if (*i == '\x07') /* Single character ST */
762 state = STATE_OTHER;
763 else if (*i == '\x1B')
764 state = STATE_OSC_CLOSING;
765
766 break;
767
768 case STATE_OSC_CLOSING:
769 if (eot || *i != '\x5c') { /* EOT or incomplete two-byte ST in sequence */
770 fputc('\x1B', f);
771 fputc(']', f);
772 advance_offsets(i - *ibuf, highlight, shift, 2);
773 state = STATE_OTHER;
774 i = begin-1;
775 } else if (*i == '\x5c')
776 state = STATE_OTHER;
777
778 break;
779 }
780 }
781
782 char *obuf;
783 if (memstream_finalize(&m, &obuf, _isz) < 0)
784 return NULL;
785
786 free_and_replace(*ibuf, obuf);
787
788 if (highlight) {
789 highlight[0] += shift[0];
790 highlight[1] += shift[1];
791 }
792
793 return *ibuf;
794 }
795
796 char* strextend_with_separator_internal(char **x, const char *separator, ...) {
797 _cleanup_free_ char *buffer = NULL;
798 size_t f, l, l_separator;
799 bool need_separator;
800 char *nr, *p;
801 va_list ap;
802
803 if (!x)
804 x = &buffer;
805
806 l = f = strlen_ptr(*x);
807
808 need_separator = !isempty(*x);
809 l_separator = strlen_ptr(separator);
810
811 va_start(ap, separator);
812 for (const char *t;;) {
813 size_t n;
814
815 t = va_arg(ap, const char *);
816 if (!t)
817 break;
818 if (t == POINTER_MAX)
819 continue;
820
821 n = strlen(t);
822
823 if (need_separator)
824 n += l_separator;
825
826 if (n >= SIZE_MAX - l) {
827 va_end(ap);
828 return NULL;
829 }
830
831 l += n;
832 need_separator = true;
833 }
834 va_end(ap);
835
836 need_separator = !isempty(*x);
837
838 nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
839 if (!nr)
840 return NULL;
841
842 *x = nr;
843 p = nr + f;
844
845 va_start(ap, separator);
846 for (;;) {
847 const char *t;
848
849 t = va_arg(ap, const char *);
850 if (!t)
851 break;
852 if (t == POINTER_MAX)
853 continue;
854
855 if (need_separator && separator)
856 p = stpcpy(p, separator);
857
858 p = stpcpy(p, t);
859
860 need_separator = true;
861 }
862 va_end(ap);
863
864 assert(p == nr + l);
865 *p = 0;
866
867 /* If no buffer to extend was passed in return the start of the buffer */
868 if (buffer)
869 return TAKE_PTR(buffer);
870
871 /* Otherwise we extended the buffer: return the end */
872 return p;
873 }
874
875 int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
876 size_t m, a, l_separator;
877 va_list ap;
878 int l;
879
880 /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
881 * we'll spend a tonload of time in determining the length of the string passed in, over and over
882 * again. */
883
884 assert(x);
885 assert(format);
886
887 l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
888
889 /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
890 if (*x) {
891 m = strlen(*x);
892 a = MALLOC_SIZEOF_SAFE(*x);
893 assert(a >= m + 1);
894 } else
895 m = a = 0;
896
897 if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
898 char *n;
899
900 if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
901 return -ENOMEM;
902 if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
903 return -ENOMEM;
904
905 n = realloc(*x, m + 64 + l_separator);
906 if (!n)
907 return -ENOMEM;
908
909 *x = n;
910 a = MALLOC_SIZEOF_SAFE(*x);
911 }
912
913 /* Now, let's try to format the string into it */
914 memcpy_safe(*x + m, separator, l_separator);
915 va_start(ap, format);
916 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
917 va_end(ap);
918
919 assert(l >= 0);
920
921 if ((size_t) l < a - m - l_separator) {
922 char *n;
923
924 /* Nice! This worked. We are done. But first, let's return the extra space we don't
925 * need. This should be a cheap operation, since we only lower the allocation size here,
926 * never increase. */
927 n = realloc(*x, m + (size_t) l + l_separator + 1);
928 if (n)
929 *x = n;
930 } else {
931 char *n;
932
933 /* Wasn't enough. Then let's allocate exactly what we need. */
934
935 if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
936 goto oom;
937 if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
938 goto oom;
939
940 a = m + (size_t) l + l_separator + 1;
941 n = realloc(*x, a);
942 if (!n)
943 goto oom;
944 *x = n;
945
946 va_start(ap, format);
947 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
948 va_end(ap);
949
950 assert((size_t) l < a - m - l_separator);
951 }
952
953 return 0;
954
955 oom:
956 /* truncate the bytes added after memcpy_safe() again */
957 (*x)[m] = 0;
958 return -ENOMEM;
959 }
960
961 char* strrep(const char *s, unsigned n) {
962 char *r, *p;
963 size_t l;
964
965 assert(s);
966
967 l = strlen(s);
968 p = r = malloc(l * n + 1);
969 if (!r)
970 return NULL;
971
972 for (unsigned i = 0; i < n; i++)
973 p = stpcpy(p, s);
974
975 *p = 0;
976 return r;
977 }
978
979 int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
980 assert(s);
981 assert(!isempty(sep));
982 assert(ret_first);
983 assert(ret_second);
984
985 const char *x = strstr(s, sep);
986 if (!x)
987 return -EINVAL;
988
989 _cleanup_free_ char *a = strndup(s, x - s);
990 if (!a)
991 return -ENOMEM;
992
993 _cleanup_free_ char *b = strdup(x + strlen(sep));
994 if (!b)
995 return -ENOMEM;
996
997 *ret_first = TAKE_PTR(a);
998 *ret_second = TAKE_PTR(b);
999 return 0;
1000 }
1001
1002 int free_and_strdup(char **p, const char *s) {
1003 char *t;
1004
1005 assert(p);
1006
1007 /* Replaces a string pointer with a strdup()ed new string,
1008 * possibly freeing the old one. */
1009
1010 if (streq_ptr(*p, s))
1011 return 0;
1012
1013 if (s) {
1014 t = strdup(s);
1015 if (!t)
1016 return -ENOMEM;
1017 } else
1018 t = NULL;
1019
1020 free_and_replace(*p, t);
1021
1022 return 1;
1023 }
1024
1025 int free_and_strdup_warn(char **p, const char *s) {
1026 int r;
1027
1028 r = free_and_strdup(p, s);
1029 if (r < 0)
1030 return log_oom();
1031 return r;
1032 }
1033
1034 int free_and_strndup(char **p, const char *s, size_t l) {
1035 char *t;
1036
1037 assert(p);
1038 assert(s || l == 0);
1039
1040 /* Replaces a string pointer with a strndup()ed new string,
1041 * freeing the old one. */
1042
1043 if (!*p && !s)
1044 return 0;
1045
1046 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
1047 return 0;
1048
1049 if (s) {
1050 t = strndup(s, l);
1051 if (!t)
1052 return -ENOMEM;
1053 } else
1054 t = NULL;
1055
1056 free_and_replace(*p, t);
1057 return 1;
1058 }
1059
1060 int strdup_to_full(char **ret, const char *src) {
1061 if (!src) {
1062 if (ret)
1063 *ret = NULL;
1064
1065 return 0;
1066 } else {
1067 if (ret) {
1068 char *t = strdup(src);
1069 if (!t)
1070 return -ENOMEM;
1071 *ret = t;
1072 }
1073
1074 return 1;
1075 }
1076 };
1077
1078 bool string_is_safe(const char *p) {
1079 if (!p)
1080 return false;
1081
1082 /* Checks if the specified string contains no quotes or control characters */
1083
1084 for (const char *t = p; *t; t++) {
1085 if (*t > 0 && *t < ' ') /* no control characters */
1086 return false;
1087
1088 if (strchr(QUOTES "\\\x7f", *t))
1089 return false;
1090 }
1091
1092 return true;
1093 }
1094
1095 bool string_is_safe_ascii(const char *p) {
1096 return ascii_is_valid(p) && string_is_safe(p);
1097 }
1098
1099 char* str_realloc(char *p) {
1100 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
1101
1102 if (!p)
1103 return NULL;
1104
1105 return realloc(p, strlen(p) + 1) ?: p;
1106 }
1107
1108 char* string_erase(char *x) {
1109 if (!x)
1110 return NULL;
1111
1112 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1113 * used them. */
1114 explicit_bzero_safe(x, strlen(x));
1115 return x;
1116 }
1117
1118 int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1119 const char *p = s, *e = s;
1120 bool truncation_applied = false;
1121 char *copy;
1122 size_t n = 0;
1123
1124 assert(s);
1125
1126 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1127 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1128 * generated either. */
1129
1130 for (;;) {
1131 size_t k;
1132
1133 k = strcspn(p, "\n");
1134
1135 if (p[k] == 0) {
1136 if (k == 0) /* final empty line */
1137 break;
1138
1139 if (n >= n_lines) /* above threshold */
1140 break;
1141
1142 e = p + k; /* last line to include */
1143 break;
1144 }
1145
1146 assert(p[k] == '\n');
1147
1148 if (n >= n_lines)
1149 break;
1150
1151 if (k > 0)
1152 e = p + k;
1153
1154 p += k + 1;
1155 n++;
1156 }
1157
1158 /* e points after the last character we want to keep */
1159 if (isempty(e))
1160 copy = strdup(s);
1161 else {
1162 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1163 * isn't a new-line or a series of them */
1164 truncation_applied = true;
1165
1166 copy = strndup(s, e - s);
1167 }
1168 if (!copy)
1169 return -ENOMEM;
1170
1171 *ret = copy;
1172 return truncation_applied;
1173 }
1174
1175 int string_extract_line(const char *s, size_t i, char **ret) {
1176 const char *p = s;
1177 size_t c = 0;
1178
1179 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1180 * and == 0 if we are looking at the last line or already beyond the last line. As special
1181 * optimization, if the first line is requested and the string only consists of one line we return
1182 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1183 * common case. */
1184
1185 for (;;) {
1186 const char *q;
1187
1188 q = strchr(p, '\n');
1189 if (i == c) {
1190 /* The line we are looking for! */
1191
1192 if (q) {
1193 char *m;
1194
1195 m = strndup(p, q - p);
1196 if (!m)
1197 return -ENOMEM;
1198
1199 *ret = m;
1200 return !isempty(q + 1); /* More coming? */
1201 } else
1202 /* Tell the caller to use the input string if equal */
1203 return strdup_to(ret, p != s ? p : NULL);
1204 }
1205
1206 if (!q)
1207 /* No more lines, return empty line */
1208 return strdup_to(ret, "");
1209
1210 p = q + 1;
1211 c++;
1212 }
1213 }
1214
1215 int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
1216 /* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
1217 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
1218 const char *found = NULL;
1219 int r;
1220
1221 for (;;) {
1222 _cleanup_free_ char *w = NULL;
1223
1224 r = extract_first_word(&string, &w, separators, flags);
1225 if (r < 0)
1226 return r;
1227 if (r == 0)
1228 break;
1229
1230 found = strv_find(words, w);
1231 if (found)
1232 break;
1233 }
1234
1235 if (ret_word)
1236 *ret_word = found;
1237 return !!found;
1238 }
1239
1240 bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
1241 if (!s1 && !s2)
1242 return true;
1243 if (!s1 || !s2)
1244 return false;
1245
1246 if (!ok)
1247 ok = WHITESPACE;
1248
1249 for (; *s1 && *s2; s1++, s2++)
1250 if (*s1 != *s2)
1251 break;
1252
1253 return in_charset(s1, ok) && in_charset(s2, ok);
1254 }
1255
1256 char* string_replace_char(char *str, char old_char, char new_char) {
1257 assert(str);
1258 assert(old_char != '\0');
1259 assert(new_char != '\0');
1260 assert(old_char != new_char);
1261
1262 for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
1263 *p = new_char;
1264
1265 return str;
1266 }
1267
1268 int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret) {
1269 char *b;
1270
1271 assert(s || n == 0);
1272 assert(mode >= 0);
1273 assert(mode < _MAKE_CSTRING_MODE_MAX);
1274
1275 /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1276 * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1277
1278 if (n == 0) {
1279 if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1280 return -EINVAL;
1281
1282 if (!ret)
1283 return 0;
1284
1285 b = new0(char, 1);
1286 } else {
1287 const char *nul;
1288
1289 nul = memchr(s, 0, n);
1290 if (nul) {
1291 if (nul < s + n - 1 || /* embedded NUL? */
1292 mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1293 return -EINVAL;
1294
1295 n--;
1296 } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1297 return -EINVAL;
1298
1299 if (!ret)
1300 return 0;
1301
1302 b = memdup_suffix0(s, n);
1303 }
1304 if (!b)
1305 return -ENOMEM;
1306
1307 *ret = b;
1308 return 0;
1309 }
1310
1311 size_t strspn_from_end(const char *str, const char *accept) {
1312 size_t n = 0;
1313
1314 if (isempty(str))
1315 return 0;
1316
1317 if (isempty(accept))
1318 return 0;
1319
1320 for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1321 n++;
1322
1323 return n;
1324 }
1325
1326 char* strdupspn(const char *a, const char *accept) {
1327 if (isempty(a) || isempty(accept))
1328 return strdup("");
1329
1330 return strndup(a, strspn(a, accept));
1331 }
1332
1333 char* strdupcspn(const char *a, const char *reject) {
1334 if (isempty(a))
1335 return strdup("");
1336 if (isempty(reject))
1337 return strdup(a);
1338
1339 return strndup(a, strcspn(a, reject));
1340 }
1341
1342 char* find_line_startswith(const char *haystack, const char *needle) {
1343 char *p;
1344
1345 assert(haystack);
1346 assert(needle);
1347
1348 /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1349 * first character after it */
1350
1351 p = strstr(haystack, needle);
1352 if (!p)
1353 return NULL;
1354
1355 if (p > haystack)
1356 while (p[-1] != '\n') {
1357 p = strstr(p + 1, needle);
1358 if (!p)
1359 return NULL;
1360 }
1361
1362 return p + strlen(needle);
1363 }
1364
1365 char* find_line(const char *haystack, const char *needle) {
1366 char *p;
1367
1368 assert(haystack);
1369 assert(needle);
1370
1371 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1372 * beginning of the line */
1373
1374 p = find_line_startswith(haystack, needle);
1375 if (!p)
1376 return NULL;
1377
1378 if (*p == 0 || strchr(NEWLINE, *p))
1379 return p - strlen(needle);
1380
1381 return NULL;
1382 }
1383
1384 char* find_line_after(const char *haystack, const char *needle) {
1385 char *p;
1386
1387 assert(haystack);
1388 assert(needle);
1389
1390 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1391 * next line after it */
1392
1393 p = find_line_startswith(haystack, needle);
1394 if (!p)
1395 return NULL;
1396
1397 if (*p == 0)
1398 return p;
1399 if (strchr(NEWLINE, *p))
1400 return p + 1;
1401
1402 return NULL;
1403 }
1404
1405 bool version_is_valid(const char *s) {
1406 if (isempty(s))
1407 return false;
1408
1409 if (!filename_part_is_valid(s))
1410 return false;
1411
1412 /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1413 if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
1414 return false;
1415
1416 return true;
1417 }
1418
1419 bool version_is_valid_versionspec(const char *s) {
1420 if (!filename_part_is_valid(s))
1421 return false;
1422
1423 if (!in_charset(s, ALPHANUMERICAL "-.~^"))
1424 return false;
1425
1426 return true;
1427 }
1428
1429 ssize_t strlevenshtein(const char *x, const char *y) {
1430 _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL;
1431 size_t xl, yl;
1432
1433 /* This is inspired from the Linux kernel's Levenshtein implementation */
1434
1435 if (streq_ptr(x, y))
1436 return 0;
1437
1438 xl = strlen_ptr(x);
1439 if (xl > SSIZE_MAX)
1440 return -E2BIG;
1441
1442 yl = strlen_ptr(y);
1443 if (yl > SSIZE_MAX)
1444 return -E2BIG;
1445
1446 if (isempty(x))
1447 return yl;
1448 if (isempty(y))
1449 return xl;
1450
1451 t0 = new0(size_t, yl + 1);
1452 if (!t0)
1453 return -ENOMEM;
1454 t1 = new0(size_t, yl + 1);
1455 if (!t1)
1456 return -ENOMEM;
1457 t2 = new0(size_t, yl + 1);
1458 if (!t2)
1459 return -ENOMEM;
1460
1461 for (size_t i = 0; i <= yl; i++)
1462 t1[i] = i;
1463
1464 for (size_t i = 0; i < xl; i++) {
1465 t2[0] = i + 1;
1466
1467 for (size_t j = 0; j < yl; j++) {
1468 /* Substitution */
1469 t2[j+1] = t1[j] + (x[i] != y[j]);
1470
1471 /* Swap */
1472 if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1)
1473 t2[j+1] = t0[j-1] + 1;
1474
1475 /* Deletion */
1476 if (t2[j+1] > t1[j+1] + 1)
1477 t2[j+1] = t1[j+1] + 1;
1478
1479 /* Insertion */
1480 if (t2[j+1] > t2[j] + 1)
1481 t2[j+1] = t2[j] + 1;
1482 }
1483
1484 size_t *dummy = t0;
1485 t0 = t1;
1486 t1 = t2;
1487 t2 = dummy;
1488 }
1489
1490 return t1[yl];
1491 }
1492
1493 char* strrstr(const char *haystack, const char *needle) {
1494 /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
1495
1496 if (!haystack || !needle)
1497 return NULL;
1498
1499 /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
1500 * last char, not before. */
1501 if (*needle == 0)
1502 return strchr(haystack, 0);
1503
1504 for (const char *p = strstr(haystack, needle), *q; p; p = q) {
1505 q = strstr(p + 1, needle);
1506 if (!q)
1507 return (char *) p;
1508 }
1509 return NULL;
1510 }
1511
1512 size_t str_common_prefix(const char *a, const char *b) {
1513 assert(a);
1514 assert(b);
1515
1516 /* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
1517 * strings are fully identical. */
1518
1519 for (size_t n = 0;; n++) {
1520 char c = a[n];
1521 if (c != b[n])
1522 return n;
1523 if (c == 0)
1524 return SIZE_MAX;
1525 }
1526 }