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