• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 24809677062

22 Apr 2026 10:05PM UTC coverage: 71.989% (-0.2%) from 72.23%
24809677062

push

github

bluca
ukify: fix default path for hwids

The documentation and commit that added this seem to suggest this should
be under /usr/lib/systemd

fixes 117ec9db7

321862 of 447097 relevant lines covered (71.99%)

1201427.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.85
/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) {
3,636,480✔
21
        assert(s);
3,636,480✔
22
        assert(word);
3,636,480✔
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))
3,636,480✔
28
                return (char*) s;
29

30
        const char *p = startswith(s, word);
3,636,479✔
31
        if (!p)
3,636,479✔
32
                return NULL;
33
        if (*p == '\0')
122,419✔
34
                return (char*) p;
35

36
        const char *nw = skip_leading_chars(p, WHITESPACE);
122,418✔
37
        if (p == nw)
122,418✔
38
                return NULL;
1✔
39

40
        return (char*) nw;
41
}
42

43
char* strextendn(char **x, const char *s, size_t l) {
29,199✔
44
        assert(x);
29,199✔
45
        assert(s || l == 0);
29,199✔
46

47
        if (l > 0)
29,199✔
48
                l = strnlen(s, l); /* ignore trailing noise */
28,873✔
49

50
        if (l > 0 || !*x) {
29,199✔
51
                size_t q;
29,043✔
52
                char *m;
29,043✔
53

54
                q = strlen_ptr(*x);
29,043✔
55
                m = realloc(*x, q + l + 1);
29,043✔
56
                if (!m)
29,043✔
57
                        return NULL;
58

59
                *mempcpy_typesafe(m + q, s, l) = 0;
29,043✔
60

61
                *x = m;
29,043✔
62
        }
63

64
        return *x;
29,199✔
65
}
66

67
char* strstrip(char *s) {
6,514,973✔
68
        if (!s)
6,514,973✔
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);
6,511,616✔
74
}
75

76
char* delete_chars(char *s, const char *bad) {
9✔
77
        char *f, *t;
9✔
78

79
        /* Drops all specified bad characters, regardless where in the string */
80

81
        if (!s)
9✔
82
                return NULL;
83

84
        if (!bad)
9✔
85
                bad = WHITESPACE;
×
86

87
        for (f = s, t = s; *f; f++) {
131✔
88
                if (strchr(bad, *f))
122✔
89
                        continue;
67✔
90

91
                *(t++) = *f;
55✔
92
        }
93

94
        *t = 0;
9✔
95

96
        return s;
9✔
97
}
98

99
char* delete_trailing_chars(char *s, const char *bad) {
8,114,794✔
100
        char *c = s;
8,114,794✔
101

102
        /* Drops all specified bad characters, at the end of the string */
103

104
        if (!s)
8,114,794✔
105
                return NULL;
106

107
        if (!bad)
8,114,794✔
108
                bad = WHITESPACE;
86,117✔
109

110
        for (char *p = s; *p; p++)
290,572,140✔
111
                if (!strchr(bad, *p))
282,457,346✔
112
                        c = p + 1;
270,717,528✔
113

114
        *c = 0;
8,114,794✔
115

116
        return s;
8,114,794✔
117
}
118

119
char* truncate_nl_full(char *s, size_t *ret_len) {
2,903,785✔
120
        size_t n;
2,903,785✔
121

122
        assert(s);
2,903,785✔
123

124
        n = strcspn(s, NEWLINE);
2,903,785✔
125
        s[n] = '\0';
2,903,785✔
126
        if (ret_len)
2,903,785✔
127
                *ret_len = n;
6✔
128
        return s;
2,903,785✔
129
}
130

131
char ascii_tolower(char x) {
51,581,229✔
132

133
        if (x >= 'A' && x <= 'Z')
51,581,229✔
134
                return x - 'A' + 'a';
3,547,641✔
135

136
        return x;
137
}
138

139
char ascii_toupper(char x) {
330,912✔
140

141
        if (x >= 'a' && x <= 'z')
330,912✔
142
                return x - 'a' + 'A';
201,998✔
143

144
        return x;
145
}
146

147
char* ascii_strlower(char *s) {
28,943✔
148
        assert(s);
28,943✔
149

150
        for (char *p = s; *p; p++)
185,438✔
151
                *p = ascii_tolower(*p);
156,495✔
152

153
        return s;
28,943✔
154
}
155

156
char* ascii_strupper(char *s) {
23,710✔
157
        assert(s);
23,710✔
158

159
        for (char *p = s; *p; p++)
354,492✔
160
                *p = ascii_toupper(*p);
330,782✔
161

162
        return s;
23,710✔
163
}
164

165
char* ascii_strlower_n(char *s, size_t n) {
1,820,154✔
166
        if (n <= 0)
1,820,154✔
167
                return s;
168

169
        for (size_t i = 0; i < n; i++)
15,406,430✔
170
                s[i] = ascii_tolower(s[i]);
13,586,619✔
171

172
        return s;
173
}
174

175
int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
2,515,612✔
176

177
        assert(a);
2,515,612✔
178
        assert(b);
2,515,612✔
179

180
        for (; n > 0; a++, b++, n--) {
20,832,386✔
181
                int x, y;
18,914,520✔
182

183
                x = (int) (uint8_t) ascii_tolower(*a);
18,914,520✔
184
                y = (int) (uint8_t) ascii_tolower(*b);
18,914,520✔
185

186
                if (x != y)
18,914,520✔
187
                        return x - y;
597,746✔
188
        }
189

190
        return 0;
191
}
192

193
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
1,080,055✔
194
        int r;
1,080,055✔
195

196
        r = ascii_strcasecmp_n(a, b, MIN(n, m));
1,080,055✔
197
        if (r != 0)
1,080,055✔
198
                return r;
199

200
        return CMP(n, m);
949,532✔
201
}
202

203
bool chars_intersect(const char *a, const char *b) {
11,720✔
204
        /* Returns true if any of the chars in a are in b. */
205
        for (const char *p = a; *p; p++)
168,896✔
206
                if (strchr(b, *p))
157,189✔
207
                        return true;
208

209
        return false;
210
}
211

212
bool string_has_cc(const char *p, const char *ok) {
13,910,662✔
213
        assert(p);
13,910,662✔
214

215
        /*
216
         * Check if a string contains control characters. If 'ok' is
217
         * non-NULL it may be a string containing additional CCs to be
218
         * considered OK.
219
         */
220

221
        for (const char *t = p; *t; t++) {
251,603,217✔
222
                if (ok && strchr(ok, *t))
237,692,577✔
223
                        continue;
8✔
224

225
                if (char_is_cc(*t))
237,692,569✔
226
                        return true;
227
        }
228

229
        return false;
230
}
231

232
static int write_ellipsis(char *buf, bool unicode) {
4,381✔
233
        const char *s = glyph_full(GLYPH_ELLIPSIS, unicode);
4,381✔
234
        assert(strlen(s) == 3);
4,381✔
235
        memcpy(buf, s, 3);
4,381✔
236
        return 3;
4,381✔
237
}
238

239
static size_t ansi_sequence_length(const char *s, size_t len) {
6,334✔
240
        assert(s);
6,334✔
241

242
        if (len < 2)
6,334✔
243
                return 0;
244

245
        if (s[0] != 0x1B)  /* ASCII 27, aka ESC, aka Ctrl-[ */
6,242✔
246
                return 0;  /* Not the start of a sequence */
247

248
        if (s[1] == 0x5B) { /* [, start of CSI sequence */
903✔
249
                size_t i = 2;
902✔
250

251
                if (i == len)
902✔
252
                        return 0;
253

254
                while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */
9,245✔
255
                        if (++i == len)
8,343✔
256
                                return 0;
257
                while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */
902✔
258
                        if (++i == len)
×
259
                                return 0;
260
                if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */
902✔
261
                        return i + 1;
902✔
262
                return 0;  /* Bad sequence */
263

264
        } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */
1✔
265
                return 2;
×
266

267
        return 0;  /* Bad escape? */
268
}
269

270
static bool string_has_ansi_sequence(const char *s, size_t len) {
7,909✔
271
        const char *t = s;
7,909✔
272

273
        while ((t = memchr(t, 0x1B, len - (t - s)))) {
7,910✔
274
                if (ansi_sequence_length(t, len - (t - s)) > 0)
154✔
275
                        return true;
276
                t++;
1✔
277
        }
278
        return false;
279
}
280

281
static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) {
265✔
282

283
        assert(s);
265✔
284
        assert(ret_where);
265✔
285

286
        /* Locate the previous ANSI sequence and save its start in *ret_where and return length. */
287

288
        for (size_t i = length - 2; i > 0; i--) {  /* -2 because at least two bytes are needed */
3,442✔
289
                size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1));
3,440✔
290
                if (slen == 0)
3,440✔
291
                        continue;
3,177✔
292

293
                *ret_where = s + (i - 1);
263✔
294
                return slen;
263✔
295
        }
296

297
        *ret_where = NULL;
2✔
298
        return 0;
2✔
299
}
300

301
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
4,356✔
302
        size_t x, need_space, suffix_len;
4,356✔
303
        char *t;
4,356✔
304

305
        assert(s);
4,356✔
306
        assert(percent <= 100);
4,356✔
307
        assert(new_length != SIZE_MAX);
4,356✔
308

309
        if (old_length <= new_length)
4,356✔
310
                return strndup(s, old_length);
1,767✔
311

312
        /* Special case short ellipsations */
313
        switch (new_length) {
2,589✔
314

315
        case 0:
×
316
                return strdup("");
×
317

318
        case 1:
65✔
319
                if (is_locale_utf8())
65✔
320
                        return strdup("…");
65✔
321
                else
322
                        return strdup(".");
×
323

324
        case 2:
35✔
325
                if (!is_locale_utf8())
35✔
326
                        return strdup("..");
×
327
                break;
328
        }
329

330
        /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
331
         * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
332
         * either for the UTF-8 encoded character or for three ASCII characters. */
333
        need_space = is_locale_utf8() ? 1 : 3;
2,524✔
334

335
        t = new(char, new_length+3);
2,524✔
336
        if (!t)
2,524✔
337
                return NULL;
338

339
        assert(new_length >= need_space);
2,524✔
340

341
        x = ((new_length - need_space) * percent + 50) / 100;
2,524✔
342
        assert(x <= new_length - need_space);
2,524✔
343

344
        write_ellipsis(mempcpy(t, s, x), /* unicode= */ false);
2,524✔
345
        suffix_len = new_length - x - need_space;
2,524✔
346
        memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
2,524✔
347
        *(t + x + 3 + suffix_len) = '\0';
2,524✔
348

349
        return t;
2,524✔
350
}
351

352
char* ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
8,080✔
353
        size_t x, k, len, len2;
8,080✔
354
        const char *i, *j;
8,080✔
355
        int r;
8,080✔
356

357
        /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
358
         * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
359
         * strings.
360
         *
361
         * Ellipsation is done in a locale-dependent way:
362
         * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
363
         * 2. Otherwise, a unicode ellipsis is used ("…")
364
         *
365
         * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
366
         * the current locale is UTF-8.
367
         */
368

369
        assert(s);
8,080✔
370
        assert(percent <= 100);
8,080✔
371

372
        if (new_length == SIZE_MAX)
8,080✔
373
                return strndup(s, old_length);
×
374

375
        if (new_length == 0)
8,080✔
376
                return strdup("");
171✔
377

378
        bool has_ansi_seq = string_has_ansi_sequence(s, old_length);
7,909✔
379

380
        /* If no multibyte characters or ANSI sequences, use ascii_ellipsize_mem for speed */
381
        if (!has_ansi_seq && ascii_is_valid_n(s, old_length))
7,909✔
382
                return ascii_ellipsize_mem(s, old_length, new_length, percent);
4,356✔
383

384
        x = (new_length - 1) * percent / 100;
3,553✔
385
        assert(x <= new_length - 1);
3,553✔
386

387
        k = 0;
388
        for (i = s; i < s + old_length; ) {
49,915✔
389
                size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0;
48,723✔
390
                if (slen > 0) {
1,011✔
391
                        i += slen;
224✔
392
                        continue;  /* ANSI sequences don't take up any space in output */
224✔
393
                }
394

395
                char32_t c;
48,499✔
396
                r = utf8_encoded_to_unichar(i, &c);
48,499✔
397
                if (r < 0)
48,499✔
398
                        return NULL;
×
399

400
                int w = unichar_iswide(c) ? 2 : 1;
48,499✔
401
                if (k + w > x)
48,499✔
402
                        break;
403

404
                k += w;
46,138✔
405
                i += r;
46,138✔
406
        }
407

408
        const char *ansi_start = s + old_length;
3,553✔
409
        size_t ansi_len = 0;
3,553✔
410

411
        for (const char *t = j = s + old_length; t > i && k < new_length; ) {
13,240✔
412
                char32_t c;
9,773✔
413
                int w;
9,773✔
414
                const char *tt;
9,773✔
415

416
                if (has_ansi_seq && ansi_start >= t)
9,773✔
417
                        /* Figure out the previous ANSI sequence, if any */
418
                        ansi_len = previous_ansi_sequence(s, t - s, &ansi_start);
265✔
419

420
                /* If the sequence extends all the way to the current position, skip it. */
421
                if (has_ansi_seq && ansi_len > 0 && ansi_start + ansi_len == t) {
9,773✔
422
                        t = ansi_start;
112✔
423
                        continue;
112✔
424
                }
425

426
                tt = utf8_prev_char(t);
9,661✔
427
                r = utf8_encoded_to_unichar(tt, &c);
9,661✔
428
                if (r < 0)
9,661✔
429
                        return NULL;
×
430

431
                w = unichar_iswide(c) ? 2 : 1;
9,661✔
432
                if (k + w > new_length)
9,661✔
433
                        break;
434

435
                k += w;
9,575✔
436
                j = t = tt;  /* j should always point to the first "real" character */
9,575✔
437
        }
438

439
        /* We don't actually need to ellipsize */
440
        if (i >= j)
3,553✔
441
                return memdup_suffix0(s, old_length);
1,720✔
442

443
        if (k >= new_length) {
1,833✔
444
                /* Make space for ellipsis, if required and possible. We know that the edge character is not
445
                 * part of an ANSI sequence (because then we'd skip it). If the last character we looked at
446
                 * was wide, we don't need to make space. */
447
                if (j < s + old_length)
1,747✔
448
                        j = utf8_next_char(j);
1,747✔
449
                else if (i > s)
×
450
                        i = utf8_prev_char(i);
×
451
        }
452

453
        len = i - s;
1,833✔
454
        len2 = s + old_length - j;
1,833✔
455

456
        /* If we have ANSI, allow the same length as the source string + ellipsis. It'd be too involved to
457
         * figure out what exact space is needed. Strings with ANSI sequences are most likely to be fairly
458
         * short anyway. */
459
        size_t alloc_len = has_ansi_seq ? old_length + 3 + 1 : len + 3 + len2 + 1;
1,833✔
460

461
        char *e = new(char, alloc_len);
1,833✔
462
        if (!e)
1,833✔
463
                return NULL;
464

465
        memcpy_safe(e, s, len);
1,833✔
466
        write_ellipsis(e + len, /* unicode= */ true);
1,833✔
467

468
        char *dst = e + len + 3;
1,833✔
469

470
        if (has_ansi_seq)
1,833✔
471
                /* Copy over any ANSI sequences in full */
472
                for (const char *p = s + len; p < j; ) {
1,864✔
473
                        size_t slen = ansi_sequence_length(p, j - p);
1,729✔
474
                        if (slen > 0) {
1,729✔
475
                                dst = mempcpy(dst, p, slen);
262✔
476
                                p += slen;
262✔
477
                        } else
478
                                p = utf8_next_char(p);
1,467✔
479
                }
480

481
        memcpy_safe(dst, j, len2);
1,833✔
482
        dst[len2] = '\0';
1,833✔
483

484
        return e;
1,833✔
485
}
486

487
char* cellescape(char *buf, size_t len, const char *s) {
43,333✔
488
        /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
489
         * characters are copied as they are, everything else is escaped. The result
490
         * is different then if escaping and ellipsization was performed in two
491
         * separate steps, because each sequence is either stored in full or skipped.
492
         *
493
         * This function should be used for logging about strings which expected to
494
         * be plain ASCII in a safe way.
495
         *
496
         * An ellipsis will be used if s is too long. It was always placed at the
497
         * very end.
498
         */
499

500
        size_t i = 0, last_char_width[4] = {}, k = 0;
43,333✔
501

502
        assert(buf);
43,333✔
503
        assert(len > 0); /* at least a terminating NUL */
43,333✔
504
        assert(s);
43,333✔
505

506
        for (;;) {
949,875✔
507
                char four[4];
496,604✔
508
                int w;
496,604✔
509

510
                if (*s == 0) /* terminating NUL detected? then we are done! */
496,604✔
511
                        goto done;
43,297✔
512

513
                w = cescape_char(*s, four);
453,307✔
514
                if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
453,307✔
515
                                      * ellipsize at the previous location */
516
                        break;
517

518
                /* OK, there was space, let's add this escaped character to the buffer */
519
                memcpy(buf + i, four, w);
453,271✔
520
                i += w;
453,271✔
521

522
                /* And remember its width in the ring buffer */
523
                last_char_width[k] = w;
453,271✔
524
                k = (k + 1) % 4;
453,271✔
525

526
                s++;
453,271✔
527
        }
528

529
        /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
530
         * characters ideally, but the buffer is shorter than that in the first place take what we can get */
531
        for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
81✔
532

533
                if (i + 4 <= len) /* nice, we reached our space goal */
81✔
534
                        break;
535

536
                k = k == 0 ? 3 : k - 1;
57✔
537
                if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
57✔
538
                        break;
539

540
                assert(i >= last_char_width[k]);
45✔
541
                i -= last_char_width[k];
45✔
542
        }
543

544
        if (i + 4 <= len) /* yay, enough space */
36✔
545
                i += write_ellipsis(buf + i, /* unicode= */ false);
24✔
546
        else if (i + 3 <= len) { /* only space for ".." */
12✔
547
                buf[i++] = '.';
4✔
548
                buf[i++] = '.';
4✔
549
        } else if (i + 2 <= len) /* only space for a single "." */
8✔
550
                buf[i++] = '.';
4✔
551
        else
552
                assert(i + 1 <= len);
4✔
553

554
done:
4✔
555
        buf[i] = '\0';
43,333✔
556
        return buf;
43,333✔
557
}
558

559
char* strshorten(char *s, size_t l) {
357,090✔
560
        assert(s);
357,090✔
561

562
        if (l >= SIZE_MAX-1) /* Would not change anything */
357,090✔
563
                return s;
564

565
        if (strnlen(s, l+1) > l)
357,088✔
566
                s[l] = 0;
110✔
567

568
        return s;
569
}
570

571
int strgrowpad0(char **s, size_t l) {
26✔
572
        size_t sz;
26✔
573

574
        assert(s);
26✔
575

576
        if (*s) {
26✔
577
                sz = strlen(*s) + 1;
26✔
578
                if (sz >= l) /* never shrink */
26✔
579
                        return 0;
580
        } else
581
                sz = 0;
582

583
        char *q = realloc(*s, l);
26✔
584
        if (!q)
26✔
585
                return -ENOMEM;
586

587
        *s = q;
26✔
588

589
        memzero(*s + sz, l - sz);
26✔
590
        return 0;
591
}
592

593
char* strreplace(const char *text, const char *old_string, const char *new_string) {
4,622✔
594
        size_t l, old_len, new_len;
4,622✔
595
        char *t, *ret = NULL;
4,622✔
596
        const char *f;
4,622✔
597

598
        assert(old_string);
4,622✔
599
        assert(new_string);
4,622✔
600

601
        if (!text)
4,622✔
602
                return NULL;
4,622✔
603

604
        old_len = strlen(old_string);
4,621✔
605
        new_len = strlen(new_string);
4,621✔
606

607
        l = strlen(text);
4,621✔
608
        if (!GREEDY_REALLOC(ret, l+1))
4,621✔
609
                return NULL;
610

611
        f = text;
4,621✔
612
        t = ret;
4,621✔
613
        while (*f) {
162,309✔
614
                size_t d, nl;
157,688✔
615

616
                if (!startswith(f, old_string)) {
157,688✔
617
                        *(t++) = *(f++);
155,484✔
618
                        continue;
155,484✔
619
                }
620

621
                d = t - ret;
2,204✔
622
                nl = l - old_len + new_len;
2,204✔
623

624
                if (!GREEDY_REALLOC(ret, nl + 1))
2,204✔
625
                        return mfree(ret);
×
626

627
                l = nl;
2,204✔
628
                t = ret + d;
2,204✔
629

630
                t = stpcpy(t, new_string);
2,204✔
631
                f += old_len;
2,204✔
632
        }
633

634
        *t = 0;
4,621✔
635
        return ret;
4,621✔
636
}
637

638
static void advance_offsets(
406✔
639
                ssize_t diff,
640
                size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
641
                size_t shift[static 2],
642
                size_t size) {
643

644
        if (!offsets)
406✔
645
                return;
646

647
        assert(shift);
396✔
648

649
        if ((size_t) diff < offsets[0])
396✔
650
                shift[0] += size;
×
651
        if ((size_t) diff < offsets[1])
396✔
652
                shift[1] += size;
×
653
}
654

655
char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
179,213✔
656
        const char *begin = NULL;
179,213✔
657
        enum {
179,213✔
658
                STATE_OTHER,
659
                STATE_ESCAPE,
660
                STATE_CSI,
661
                STATE_OSC,
662
                STATE_OSC_CLOSING,
663
        } state = STATE_OTHER;
179,213✔
664
        _cleanup_(memstream_done) MemStream m = {};
179,213✔
665
        size_t isz, shift[2] = {}, n_carriage_returns = 0;
179,213✔
666
        FILE *f;
179,213✔
667

668
        assert(ibuf);
179,213✔
669
        assert(*ibuf);
179,213✔
670
        POINTER_MAY_BE_NULL(_isz);
179,213✔
671

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 (OSC), i.e. ESC ']' … ST sequences
677
         * 4. Strip trailing \r characters (since they would "move the cursor", but have no
678
         *    other effect).
679
         *
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.
683
         *
684
         * Why care for OSC sequences? Well, to undo what terminal_urlify() and friends generate. */
685

686
        isz = _isz ? *_isz : strlen(*ibuf);
179,213✔
687

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 = memstream_init(&m);
179,213✔
691
        if (!f)
179,213✔
692
                return NULL;
693

694
        for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
21,278,792✔
695

696
                bool eot = i >= *ibuf + isz;
21,099,579✔
697

698
                switch (state) {
21,099,579✔
699

700
                case STATE_OTHER:
21,099,328✔
701
                        if (eot)
21,099,328✔
702
                                break;
703

704
                        if (*i == '\r') {
20,920,115✔
705
                                n_carriage_returns++;
7✔
706
                                break;
7✔
707
                        } else if (*i == '\n')
20,920,108✔
708
                                /* Ignore carriage returns before new line */
709
                                n_carriage_returns = 0;
34✔
710
                        for (; n_carriage_returns > 0; n_carriage_returns--)
20,920,110✔
711
                                fputc('\r', f);
2✔
712

713
                        if (*i == '\x1B')
20,920,108✔
714
                                state = STATE_ESCAPE;
715
                        else if (*i == '\t') {
20,920,074✔
716
                                fputs("        ", f);
401✔
717
                                advance_offsets(i - *ibuf, highlight, shift, 7);
401✔
718
                        } else
719
                                fputc(*i, f);
20,919,673✔
720

721
                        break;
722

723
                case STATE_ESCAPE:
34✔
724
                        assert(n_carriage_returns == 0);
34✔
725

726
                        if (eot) {
34✔
727
                                fputc('\x1B', f);
×
728
                                advance_offsets(i - *ibuf, highlight, shift, 1);
×
729
                                break;
730
                        } else if (*i == '[') { /* ANSI CSI */
34✔
731
                                state = STATE_CSI;
31✔
732
                                begin = i + 1;
31✔
733
                        } else if (*i == ']') { /* ANSI OSC */
3✔
734
                                state = STATE_OSC;
3✔
735
                                begin = i + 1;
3✔
736
                        } else {
737
                                fputc('\x1B', f);
×
738
                                fputc(*i, f);
×
739
                                advance_offsets(i - *ibuf, highlight, shift, 1);
×
740
                                state = STATE_OTHER;
×
741
                        }
742

743
                        break;
744

745
                case STATE_CSI:
191✔
746
                        assert(n_carriage_returns == 0);
191✔
747

748
                        if (eot || !strchr(DIGITS ";:m", *i)) { /* EOT or invalid chars in sequence */
191✔
749
                                fputc('\x1B', f);
5✔
750
                                fputc('[', f);
5✔
751
                                advance_offsets(i - *ibuf, highlight, shift, 2);
5✔
752
                                state = STATE_OTHER;
5✔
753
                                i = begin-1;
5✔
754
                        } else if (*i == 'm')
186✔
755
                                state = STATE_OTHER;
26✔
756

757
                        break;
758

759
                case STATE_OSC:
24✔
760
                        assert(n_carriage_returns == 0);
24✔
761

762
                        /* There are three kinds of OSC terminators: \x07, \x1b\x5c or \x9c. We only support
763
                         * the first two, because the last one is a valid UTF-8 codepoint and hence creates
764
                         * an ambiguity (many Terminal emulators refuse to support it as well). */
765
                        if (eot || (!IN_SET(*i, '\x07', '\x1b') && !osc_char_is_valid(*i))) { /* EOT or invalid chars in sequence */
24✔
766
                                fputc('\x1B', f);
×
767
                                fputc(']', f);
×
768
                                advance_offsets(i - *ibuf, highlight, shift, 2);
×
769
                                state = STATE_OTHER;
×
770
                                i = begin-1;
×
771
                        } else if (*i == '\x07') /* Single character ST */
24✔
772
                                state = STATE_OTHER;
773
                        else if (*i == '\x1B')
23✔
774
                                state = STATE_OSC_CLOSING;
2✔
775

776
                        break;
777

778
                case STATE_OSC_CLOSING:
2✔
779
                        if (eot || *i != '\x5c') { /* EOT or incomplete two-byte ST in sequence */
2✔
780
                                fputc('\x1B', f);
×
781
                                fputc(']', f);
×
782
                                advance_offsets(i - *ibuf, highlight, shift, 2);
×
783
                                state = STATE_OTHER;
×
784
                                i = begin-1;
×
785
                        } else if (*i == '\x5c')
786
                                state = STATE_OTHER;
787

788
                        break;
789
                }
790
        }
791

792
        char *obuf;
179,213✔
793
        if (memstream_finalize(&m, &obuf, _isz) < 0)
179,213✔
794
                return NULL;
795

796
        free_and_replace(*ibuf, obuf);
179,213✔
797

798
        if (highlight) {
179,213✔
799
                highlight[0] += shift[0];
179,186✔
800
                highlight[1] += shift[1];
179,186✔
801
        }
802

803
        return *ibuf;
179,213✔
804
}
805

806
char* strextendv_with_separator(char **x, const char *separator, va_list ap) {
23,271,613✔
807
        _cleanup_free_ char *buffer = NULL;
23,271,613✔
808
        size_t f, l, l_separator;
23,271,613✔
809
        bool need_separator;
23,271,613✔
810
        char *nr, *p;
23,271,613✔
811

812
        if (!x)
23,271,613✔
813
                x = &buffer;
21,949,333✔
814

815
        l = f = strlen_ptr(*x);
23,271,613✔
816

817
        need_separator = !isempty(*x);
23,271,613✔
818
        l_separator = strlen_ptr(separator);
23,271,613✔
819

820
        va_list aq;
23,271,613✔
821
        va_copy(aq, ap);
23,271,613✔
822
        for (const char *t;;) {
88,377,458✔
823
                size_t n;
88,377,458✔
824

825
                t = va_arg(aq, const char *);
88,377,458✔
826
                if (!t)
88,377,458✔
827
                        break;
828
                if (t == POINTER_MAX)
65,105,845✔
829
                        continue;
3✔
830

831
                n = strlen(t);
65,105,842✔
832

833
                if (need_separator)
65,105,842✔
834
                        n += l_separator;
42,585,451✔
835

836
                if (n >= SIZE_MAX - l) {
65,105,842✔
837
                        va_end(aq);
×
838
                        return NULL;
×
839
                }
840

841
                l += n;
65,105,842✔
842
                need_separator = true;
65,105,842✔
843
        }
844
        va_end(aq);
23,271,613✔
845

846
        need_separator = !isempty(*x);
23,271,613✔
847

848
        nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
23,271,613✔
849
        if (!nr)
23,271,613✔
850
                return NULL;
851

852
        *x = nr;
23,271,613✔
853
        p = nr + f;
23,271,613✔
854

855
        for (;;) {
88,377,458✔
856
                const char *t;
88,377,458✔
857

858
                t = va_arg(ap, const char *);
88,377,458✔
859
                if (!t)
88,377,458✔
860
                        break;
861
                if (t == POINTER_MAX)
65,105,845✔
862
                        continue;
3✔
863

864
                if (need_separator && separator)
65,105,842✔
865
                        p = stpcpy(p, separator);
162,494✔
866

867
                p = stpcpy(p, t);
65,105,842✔
868

869
                need_separator = true;
65,105,842✔
870
        }
871

872
        assert(p == nr + l);
23,271,613✔
873
        *p = 0;
23,271,613✔
874

875
        /* If no buffer to extend was passed in return the start of the buffer */
876
        if (buffer)
23,271,613✔
877
                return TAKE_PTR(buffer);
21,949,333✔
878

879
        /* Otherwise we extended the buffer: return the end */
880
        return p;
881
}
882

883
char* strextend_with_separator_internal(char **x, const char *separator, ...) {
23,219,976✔
884
        va_list ap;
23,219,976✔
885
        char *ret;
23,219,976✔
886

887
        va_start(ap, separator);
23,219,976✔
888
        ret = strextendv_with_separator(x, separator, ap);
23,219,976✔
889
        va_end(ap);
23,219,976✔
890

891
        return ret;
23,219,976✔
892
}
893

894
int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
17,926✔
895
        size_t m, a, l_separator;
17,926✔
896
        va_list ap;
17,926✔
897
        int l;
17,926✔
898

899
        /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
900
         * we'll spend a tonload of time in determining the length of the string passed in, over and over
901
         * again. */
902

903
        assert(x);
17,926✔
904
        assert(format);
17,926✔
905

906
        l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
31,180✔
907

908
        /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
909
        if (*x) {
17,926✔
910
                m = strlen(*x);
13,254✔
911
                a = MALLOC_SIZEOF_SAFE(*x);
13,254✔
912
                assert(a >= m + 1);
13,254✔
913
        } else
914
                m = a = 0;
915

916
        if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
17,926✔
917
                char *n;
11,027✔
918

919
                if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
11,027✔
920
                        return -ENOMEM;
17,926✔
921
                if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
11,027✔
922
                        return -ENOMEM;
923

924
                n = realloc(*x, m + 64 + l_separator);
11,027✔
925
                if (!n)
11,027✔
926
                        return -ENOMEM;
927

928
                *x = n;
11,027✔
929
                a = MALLOC_SIZEOF_SAFE(*x);
11,027✔
930
        }
931

932
        /* Now, let's try to format the string into it */
933
        memcpy_safe(*x + m, separator, l_separator);
17,926✔
934
        va_start(ap, format);
17,926✔
935
        l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
17,926✔
936
        va_end(ap);
17,926✔
937

938
        assert(l >= 0);
17,926✔
939

940
        if ((size_t) l < a - m - l_separator) {
17,926✔
941
                char *n;
17,427✔
942

943
                /* Nice! This worked. We are done. But first, let's return the extra space we don't
944
                 * need. This should be a cheap operation, since we only lower the allocation size here,
945
                 * never increase. */
946
                n = realloc(*x, m + (size_t) l + l_separator + 1);
17,427✔
947
                if (n)
17,427✔
948
                        *x = n;
17,427✔
949
        } else {
950
                char *n;
499✔
951

952
                /* Wasn't enough. Then let's allocate exactly what we need. */
953

954
                if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
499✔
955
                        goto oom;
×
956
                if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
499✔
957
                        goto oom;
×
958

959
                a = m + (size_t) l + l_separator + 1;
499✔
960
                n = realloc(*x, a);
499✔
961
                if (!n)
499✔
962
                        goto oom;
×
963
                *x = n;
499✔
964

965
                va_start(ap, format);
499✔
966
                l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
499✔
967
                va_end(ap);
499✔
968

969
                assert((size_t) l < a - m - l_separator);
499✔
970
        }
971

972
        return 0;
973

974
oom:
×
975
        /* truncate the bytes added after memcpy_safe() again */
976
        (*x)[m] = 0;
×
977
        return -ENOMEM;
×
978
}
979

980
char* strrep(const char *s, size_t n) {
12✔
981
        char *ret, *p;
12✔
982
        size_t l;
12✔
983

984
        assert(s);
12✔
985

986
        l = strlen(s);
12✔
987
        if (!MUL_ASSIGN_SAFE(&l, n))
12✔
988
                return NULL;
12✔
989
        if (!INC_SAFE(&l, 1))
11✔
990
                return NULL;
991

992
        p = ret = malloc(l);
11✔
993
        if (!ret)
11✔
994
                return NULL;
995

996
        for (size_t i = 0; i < n; i++)
40,337✔
997
                p = stpcpy(p, s);
40,326✔
998

999
        *p = 0;
11✔
1000
        return ret;
11✔
1001
}
1002

1003
int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
1,564✔
1004
        assert(s);
1,564✔
1005
        assert(!isempty(sep));
1,564✔
1006
        assert(ret_first);
1,564✔
1007
        assert(ret_second);
1,564✔
1008

1009
        const char *x = strstr(s, sep);
1,564✔
1010
        if (!x)
1,564✔
1011
                return -EINVAL;
1,564✔
1012

1013
        _cleanup_free_ char *a = strndup(s, x - s);
262✔
1014
        if (!a)
262✔
1015
                return -ENOMEM;
1016

1017
        _cleanup_free_ char *b = strdup(x + strlen(sep));
262✔
1018
        if (!b)
262✔
1019
                return -ENOMEM;
1020

1021
        *ret_first = TAKE_PTR(a);
262✔
1022
        *ret_second = TAKE_PTR(b);
262✔
1023
        return 0;
262✔
1024
}
1025

1026
int free_and_strdup(char **p, const char *s) {
2,167,593✔
1027
        char *t;
2,167,593✔
1028

1029
        assert(p);
2,167,593✔
1030

1031
        /* Replaces a string pointer with a strdup()ed new string,
1032
         * possibly freeing the old one. */
1033

1034
        if (streq_ptr(*p, s))
2,167,593✔
1035
                return 0;
2,167,593✔
1036

1037
        if (s) {
1,722,390✔
1038
                t = strdup(s);
1,714,295✔
1039
                if (!t)
1,714,295✔
1040
                        return -ENOMEM;
1041
        } else
1042
                t = NULL;
1043

1044
        free_and_replace(*p, t);
1,722,390✔
1045

1046
        return 1;
1,722,390✔
1047
}
1048

1049
int free_and_strdup_warn(char **p, const char *s) {
63,505✔
1050
        int r;
63,505✔
1051

1052
        r = free_and_strdup(p, s);
63,505✔
1053
        if (r < 0)
63,505✔
1054
                return log_oom();
×
1055
        return r;
1056
}
1057

1058
int free_and_strndup(char **p, const char *s, size_t l) {
69,969✔
1059
        char *t;
69,969✔
1060

1061
        assert(p);
69,969✔
1062
        assert(s || l == 0);
69,969✔
1063

1064
        /* Replaces a string pointer with a strndup()ed new string,
1065
         * freeing the old one. */
1066

1067
        if (!*p && !s)
69,969✔
1068
                return 0;
69,969✔
1069

1070
        if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
69,969✔
1071
                return 0;
1072

1073
        if (s) {
32,637✔
1074
                t = strndup(s, l);
32,696✔
1075
                if (!t)
32,696✔
1076
                        return -ENOMEM;
1077
        } else
1078
                t = NULL;
1079

1080
        free_and_replace(*p, t);
32,697✔
1081
        return 1;
32,697✔
1082
}
1083

1084
int strdup_to_full(char **ret, const char *src) {
805,668✔
1085
        if (!src) {
805,668✔
1086
                if (ret)
276,124✔
1087
                        *ret = NULL;
276,123✔
1088

1089
                return 0;
276,124✔
1090
        } else {
1091
                if (ret) {
529,544✔
1092
                        char *t = strdup(src);
529,542✔
1093
                        if (!t)
529,542✔
1094
                                return -ENOMEM;
1095
                        *ret = t;
529,542✔
1096
                }
1097

1098
                return 1;
529,544✔
1099
        }
1100
};
1101

1102
bool string_is_safe(const char *p, StringSafeFlags flags) {
197,860✔
1103

1104
        /* Baseline checks are:
1105
         *   • No control characters (i.e. 0…31 + 127)
1106
         *   • UTF-8 valid (well, technically we skip this test if STRING_ASCII is set, since that is a tighter test)
1107
         */
1108

1109
        if (FLAGS_SET(flags, STRING_ALLOW_EMPTY) ? !p : isempty(p))
197,860✔
1110
                return false;
1111

1112
        if (!FLAGS_SET(flags, STRING_ASCII) && !utf8_is_valid(p))
395,665✔
1113
                return false;
1114

1115
        for (const char *t = p; *t; t++) {
2,319,514✔
1116
                if ((*t > 0 && *t < ' ') || *t == 0x7f) /* never allow control characters */
2,121,714✔
1117
                        return false;
1118

1119
                if (!FLAGS_SET(flags, STRING_ALLOW_BACKSLASHES) && *t == '\\')
2,121,698✔
1120
                        return false;
1121

1122
                if (!FLAGS_SET(flags, STRING_ALLOW_QUOTES) && strchr(QUOTES, *t))
2,121,690✔
1123
                        return false;
1124

1125
                if (!FLAGS_SET(flags, STRING_ALLOW_GLOBS) && strchr(GLOB_CHARS, *t))
2,121,681✔
1126
                        return false;
1127

1128
                if (FLAGS_SET(flags, STRING_ASCII) && (uint8_t) *t >= 0x80)
2,121,674✔
1129
                        return false;
1130
        }
1131

1132
        if (FLAGS_SET(flags, STRING_FILENAME) && !filename_is_valid(p))
197,800✔
1133
                return false;
10✔
1134

1135
        return true;
1136
}
1137

1138
char* str_realloc(char *p) {
43,316✔
1139
        /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
1140

1141
        if (!p)
43,316✔
1142
                return NULL;
1143

1144
        return realloc(p, strlen(p) + 1) ?: p;
43,316✔
1145
}
1146

1147
char* string_erase(char *x) {
119✔
1148
        if (!x)
119✔
1149
                return NULL;
1150

1151
        /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1152
         * used them. */
1153
        explicit_bzero_safe(x, strlen(x));
119✔
1154
        return x;
119✔
1155
}
1156

1157
int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
300✔
1158
        const char *p = s, *e = s;
300✔
1159
        bool truncation_applied = false;
300✔
1160
        char *copy;
300✔
1161
        size_t n = 0;
300✔
1162

1163
        assert(s);
300✔
1164
        assert(ret);
300✔
1165

1166
        /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1167
         * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1168
         * generated either. */
1169

1170
        for (;;) {
530✔
1171
                size_t k;
415✔
1172

1173
                k = strcspn(p, "\n");
415✔
1174

1175
                if (p[k] == 0) {
415✔
1176
                        if (k == 0) /* final empty line */
268✔
1177
                                break;
1178

1179
                        if (n >= n_lines) /* above threshold */
243✔
1180
                                break;
1181

1182
                        e = p + k; /* last line to include */
226✔
1183
                        break;
226✔
1184
                }
1185

1186
                assert(p[k] == '\n');
147✔
1187

1188
                if (n >= n_lines)
147✔
1189
                        break;
1190

1191
                if (k > 0)
115✔
1192
                        e = p + k;
92✔
1193

1194
                p += k + 1;
115✔
1195
                n++;
115✔
1196
        }
1197

1198
        /* e points after the last character we want to keep */
1199
        if (isempty(e))
300✔
1200
                copy = strdup(s);
230✔
1201
        else {
1202
                if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
70✔
1203
                                           * isn't a new-line or a series of them */
1204
                        truncation_applied = true;
42✔
1205

1206
                copy = strndup(s, e - s);
70✔
1207
        }
1208
        if (!copy)
300✔
1209
                return -ENOMEM;
1210

1211
        *ret = copy;
300✔
1212
        return truncation_applied;
300✔
1213
}
1214

1215
int string_extract_line(const char *s, size_t i, char **ret) {
207,926✔
1216
        const char *p = s;
207,926✔
1217
        size_t c = 0;
207,926✔
1218

1219
        assert(ret);
207,926✔
1220

1221
        /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1222
         * and == 0 if we are looking at the last line or already beyond the last line. As special
1223
         * optimization, if the first line is requested and the string only consists of one line we return
1224
         * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1225
         * common case. */
1226

1227
        for (;;) {
395,066✔
1228
                const char *q;
301,496✔
1229

1230
                q = strchr(p, '\n');
301,496✔
1231
                if (i == c) {
301,496✔
1232
                        /* The line we are looking for! */
1233

1234
                        if (q) {
204,348✔
1235
                                char *m;
3,712✔
1236

1237
                                m = strndup(p, q - p);
3,712✔
1238
                                if (!m)
3,712✔
1239
                                        return -ENOMEM;
1240

1241
                                *ret = m;
3,712✔
1242
                                return !isempty(q + 1); /* More coming? */
7,424✔
1243
                        } else
1244
                                /* Tell the caller to use the input string if equal */
1245
                                return strdup_to(ret, p != s ? p : NULL);
399,586✔
1246
                }
1247

1248
                if (!q)
97,148✔
1249
                        /* No more lines, return empty line */
1250
                        return strdup_to(ret, "");
3,578✔
1251

1252
                p = q + 1;
93,570✔
1253
                c++;
93,570✔
1254
        }
1255
}
1256

1257
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
71✔
1258
        /* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
1259
        const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
71✔
1260
        const char *found = NULL;
71✔
1261
        int r;
196✔
1262

1263
        for (;;) {
321✔
1264
                _cleanup_free_ char *w = NULL;
125✔
1265

1266
                r = extract_first_word(&string, &w, separators, flags);
196✔
1267
                if (r < 0)
196✔
1268
                        return r;
×
1269
                if (r == 0)
196✔
1270
                        break;
1271

1272
                found = strv_find(words, w);
146✔
1273
                if (found)
146✔
1274
                        break;
1275
        }
1276

1277
        if (ret_word)
71✔
1278
                *ret_word = found;
7✔
1279
        return !!found;
71✔
1280
}
1281

1282
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
8,819✔
1283
        if (!s1 && !s2)
8,819✔
1284
                return true;
1285
        if (!s1 || !s2)
8,818✔
1286
                return false;
1287

1288
        if (!ok)
8,816✔
1289
                ok = WHITESPACE;
13✔
1290

1291
        for (; *s1 && *s2; s1++, s2++)
16,675✔
1292
                if (*s1 != *s2)
10,052✔
1293
                        break;
1294

1295
        return in_charset(s1, ok) && in_charset(s2, ok);
11,015✔
1296
}
1297

1298
char* string_replace_char(char *str, char old_char, char new_char) {
670,438✔
1299
        assert(str);
670,438✔
1300
        assert(old_char != '\0');
670,438✔
1301
        assert(new_char != '\0');
670,438✔
1302
        assert(old_char != new_char);
670,438✔
1303

1304
        for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
675,650✔
1305
                *p = new_char;
5,212✔
1306

1307
        return str;
670,438✔
1308
}
1309

1310
int make_cstring(const void *s, size_t n, MakeCStringMode mode, char **ret) {
2,431✔
1311
        char *b;
2,431✔
1312

1313
        assert(s || n == 0);
2,431✔
1314
        assert(mode >= 0);
2,431✔
1315
        assert(mode < _MAKE_CSTRING_MODE_MAX);
2,431✔
1316

1317
        /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1318
         * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1319

1320
        if (n == 0) {
2,431✔
1321
                if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
20✔
1322
                        return -EINVAL;
1323

1324
                if (!ret)
19✔
1325
                        return 0;
1326

1327
                b = new0(char, 1);
19✔
1328
        } else {
1329
                const uint8_t *nul;
2,411✔
1330

1331
                nul = memchr(s, 0, n);
2,411✔
1332
                if (nul) {
2,411✔
1333
                        if (nul < (const uint8_t*) s + n - 1 || /* embedded NUL? */
15✔
1334
                            mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1335
                                return -EINVAL;
1336

1337
                        n--;
1338
                } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
2,396✔
1339
                        return -EINVAL;
1340

1341
                if (!ret)
2,400✔
1342
                        return 0;
1343

1344
                b = memdup_suffix0(s, n);
2,400✔
1345
        }
1346
        if (!b)
2,419✔
1347
                return -ENOMEM;
1348

1349
        *ret = b;
2,419✔
1350
        return 0;
2,419✔
1351
}
1352

1353
size_t strspn_from_end(const char *str, const char *accept) {
634,943✔
1354
        size_t n = 0;
634,943✔
1355

1356
        if (isempty(str))
634,943✔
1357
                return 0;
1358

1359
        if (isempty(accept))
634,940✔
1360
                return 0;
1361

1362
        for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1,246,008✔
1363
                n++;
611,069✔
1364

1365
        return n;
1366
}
1367

1368
char* strdupspn(const char *a, const char *accept) {
×
1369
        if (isempty(a) || isempty(accept))
×
1370
                return strdup("");
×
1371

1372
        return strndup(a, strspn(a, accept));
×
1373
}
1374

1375
char* strdupcspn(const char *a, const char *reject) {
37,965✔
1376
        if (isempty(a))
37,965✔
1377
                return strdup("");
×
1378
        if (isempty(reject))
37,965✔
1379
                return strdup(a);
×
1380

1381
        return strndup(a, strcspn(a, reject));
37,965✔
1382
}
1383

1384
char* find_line_startswith_internal(const char *haystack, const char *needle) {
2,426✔
1385
        assert(haystack);
2,426✔
1386
        assert(needle);
2,426✔
1387

1388
        /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1389
         * first character after it */
1390

1391
        char *p = (char*) strstr(haystack, needle);
2,426✔
1392
        if (!p)
2,426✔
1393
                return NULL;
1394

1395
        if (p > haystack)
563✔
1396
                while (p[-1] != '\n') {
60✔
1397
                        p = strstr(p + 1, needle);
9✔
1398
                        if (!p)
9✔
1399
                                return NULL;
1400
                }
1401

1402
        return p + strlen(needle);
562✔
1403
}
1404

1405
char* find_line_internal(const char *haystack, const char *needle) {
×
1406
        assert(haystack);
×
1407
        assert(needle);
×
1408

1409
        /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1410
         * beginning of the line */
1411

1412
        char *p = (char*) find_line_startswith(haystack, needle);
×
1413
        if (!p)
×
1414
                return NULL;
1415

1416
        if (*p == 0 || strchr(NEWLINE, *p))
×
1417
                return p - strlen(needle);
×
1418

1419
        return NULL;
1420
}
1421

1422
char* find_line_after_internal(const char *haystack, const char *needle) {
×
1423
        assert(haystack);
×
1424
        assert(needle);
×
1425

1426
        /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1427
         * next line after it */
1428

1429
        char *p = (char*) find_line_startswith(haystack, needle);
×
1430
        if (!p)
×
1431
                return NULL;
1432

1433
        if (*p == 0)
×
1434
                return p;
1435
        if (strchr(NEWLINE, *p))
×
1436
                return p + 1;
×
1437

1438
        return NULL;
1439
}
1440

1441
bool version_is_valid(const char *s) {
19,087✔
1442
        if (isempty(s))
19,087✔
1443
                return false;
1444

1445
        if (!filename_part_is_valid(s))
19,085✔
1446
                return false;
1447

1448
        /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1449
        if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
19,085✔
1450
                return false;
×
1451

1452
        return true;
1453
}
1454

1455
bool version_is_valid_versionspec(const char *s) {
94✔
1456
        if (!filename_part_is_valid(s))
94✔
1457
                return false;
1458

1459
        if (!in_charset(s, ALPHANUMERICAL "-.~^"))
94✔
1460
                return false;
×
1461

1462
        return true;
1463
}
1464

1465
ssize_t strlevenshtein(const char *x, const char *y) {
114✔
1466
        _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL;
228✔
1467
        size_t xl, yl;
114✔
1468

1469
        /* This is inspired from the Linux kernel's Levenshtein implementation */
1470

1471
        if (streq_ptr(x, y))
114✔
1472
                return 0;
1473

1474
        xl = strlen_ptr(x);
110✔
1475
        if (xl > SSIZE_MAX)
109✔
1476
                return -E2BIG;
1477

1478
        yl = strlen_ptr(y);
110✔
1479
        if (yl > SSIZE_MAX)
109✔
1480
                return -E2BIG;
1481

1482
        if (isempty(x))
110✔
1483
                return yl;
3✔
1484
        if (isempty(y))
107✔
1485
                return xl;
1✔
1486

1487
        t0 = new0(size_t, yl + 1);
106✔
1488
        if (!t0)
106✔
1489
                return -ENOMEM;
1490
        t1 = new0(size_t, yl + 1);
106✔
1491
        if (!t1)
106✔
1492
                return -ENOMEM;
1493
        t2 = new0(size_t, yl + 1);
106✔
1494
        if (!t2)
106✔
1495
                return -ENOMEM;
1496

1497
        for (size_t i = 0; i <= yl; i++)
1,113✔
1498
                t1[i] = i;
1,007✔
1499

1500
        for (size_t i = 0; i < xl; i++) {
627✔
1501
                t2[0] = i + 1;
521✔
1502

1503
                for (size_t j = 0; j < yl; j++) {
5,378✔
1504
                        /* Substitution */
1505
                        t2[j+1] = t1[j] + (x[i] != y[j]);
4,857✔
1506

1507
                        /* Swap */
1508
                        if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1)
4,857✔
1509
                                t2[j+1] = t0[j-1] + 1;
10✔
1510

1511
                        /* Deletion */
1512
                        if (t2[j+1] > t1[j+1] + 1)
4,857✔
1513
                                t2[j+1] = t1[j+1] + 1;
176✔
1514

1515
                        /* Insertion */
1516
                        if (t2[j+1] > t2[j] + 1)
4,857✔
1517
                                t2[j+1] = t2[j] + 1;
687✔
1518
                }
1519

1520
                size_t *dummy = t0;
1521
                t0 = t1;
1522
                t1 = t2;
1523
                t2 = dummy;
1524
        }
1525

1526
        return t1[yl];
106✔
1527
}
1528

1529
char* strrstr_internal(const char *haystack, const char *needle) {
25,251✔
1530
        /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
1531

1532
        if (!haystack || !needle)
25,251✔
1533
                return NULL;
1534

1535
        /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
1536
         * last char, not before. */
1537
        if (needle[0] == 0)
25,248✔
1538
                return (char*) strchr(haystack, 0);
2✔
1539

1540
        /* Special case: for single character strings, just use optimized strrchr() */
1541
        if (needle[1] == 0)
25,246✔
1542
                return (char*) strrchr(haystack, needle[0]);
3✔
1543

1544
        for (const char *p = strstr(haystack, needle), *q; p; p = q) {
25,249✔
1545
                q = strstr(p + 1, needle);
18✔
1546
                if (!q)
18✔
1547
                        return (char*) p;
1548
        }
1549
        return NULL;
1550
}
1551

1552
char* strrstr_no_case_internal(const char *haystack, const char *needle) {
19✔
1553
        if (!haystack || !needle)
19✔
1554
                return NULL;
1555

1556
        for (const char *p = strchr(haystack, 0); p > haystack; p--)
69✔
1557
                if (startswith_no_case(p, needle))
63✔
1558
                        return (char*) p;
1559

1560
        return startswith_no_case(haystack, needle) ? (char*) haystack : NULL;
6✔
1561
}
1562

1563
size_t str_common_prefix(const char *a, const char *b) {
16✔
1564
        assert(a);
16✔
1565
        assert(b);
16✔
1566

1567
        /* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
1568
         * strings are fully identical. */
1569

1570
        for (size_t n = 0;; n++) {
39✔
1571
                char c = a[n];
55✔
1572
                if (c != b[n])
55✔
1573
                        return n;
1574
                if (c == 0)
42✔
1575
                        return SIZE_MAX;
1576
        }
1577
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc