]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / basic / string-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "alloc-util.h"
27 #include "gunicode.h"
28 #include "macro.h"
29 #include "string-util.h"
30 #include "utf8.h"
31 #include "util.h"
32
33 int strcmp_ptr(const char *a, const char *b) {
34
35 /* Like strcmp(), but tries to make sense of NULL pointers */
36 if (a && b)
37 return strcmp(a, b);
38
39 if (!a && b)
40 return -1;
41
42 if (a && !b)
43 return 1;
44
45 return 0;
46 }
47
48 char* endswith(const char *s, const char *postfix) {
49 size_t sl, pl;
50
51 assert(s);
52 assert(postfix);
53
54 sl = strlen(s);
55 pl = strlen(postfix);
56
57 if (pl == 0)
58 return (char*) s + sl;
59
60 if (sl < pl)
61 return NULL;
62
63 if (memcmp(s + sl - pl, postfix, pl) != 0)
64 return NULL;
65
66 return (char*) s + sl - pl;
67 }
68
69 char* endswith_no_case(const char *s, const char *postfix) {
70 size_t sl, pl;
71
72 assert(s);
73 assert(postfix);
74
75 sl = strlen(s);
76 pl = strlen(postfix);
77
78 if (pl == 0)
79 return (char*) s + sl;
80
81 if (sl < pl)
82 return NULL;
83
84 if (strcasecmp(s + sl - pl, postfix) != 0)
85 return NULL;
86
87 return (char*) s + sl - pl;
88 }
89
90 char* first_word(const char *s, const char *word) {
91 size_t sl, wl;
92 const char *p;
93
94 assert(s);
95 assert(word);
96
97 /* Checks if the string starts with the specified word, either
98 * followed by NUL or by whitespace. Returns a pointer to the
99 * NUL or the first character after the whitespace. */
100
101 sl = strlen(s);
102 wl = strlen(word);
103
104 if (sl < wl)
105 return NULL;
106
107 if (wl == 0)
108 return (char*) s;
109
110 if (memcmp(s, word, wl) != 0)
111 return NULL;
112
113 p = s + wl;
114 if (*p == 0)
115 return (char*) p;
116
117 if (!strchr(WHITESPACE, *p))
118 return NULL;
119
120 p += strspn(p, WHITESPACE);
121 return (char*) p;
122 }
123
124 static size_t strcspn_escaped(const char *s, const char *reject) {
125 bool escaped = false;
126 int n;
127
128 for (n=0; s[n]; n++) {
129 if (escaped)
130 escaped = false;
131 else if (s[n] == '\\')
132 escaped = true;
133 else if (strchr(reject, s[n]))
134 break;
135 }
136
137 /* if s ends in \, return index of previous char */
138 return n - escaped;
139 }
140
141 /* Split a string into words. */
142 const char* split(const char **state, size_t *l, const char *separator, bool quoted) {
143 const char *current;
144
145 current = *state;
146
147 if (!*current) {
148 assert(**state == '\0');
149 return NULL;
150 }
151
152 current += strspn(current, separator);
153 if (!*current) {
154 *state = current;
155 return NULL;
156 }
157
158 if (quoted && strchr("\'\"", *current)) {
159 char quotechars[2] = {*current, '\0'};
160
161 *l = strcspn_escaped(current + 1, quotechars);
162 if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] ||
163 (current[*l + 2] && !strchr(separator, current[*l + 2]))) {
164 /* right quote missing or garbage at the end */
165 *state = current;
166 return NULL;
167 }
168 *state = current++ + *l + 2;
169 } else if (quoted) {
170 *l = strcspn_escaped(current, separator);
171 if (current[*l] && !strchr(separator, current[*l])) {
172 /* unfinished escape */
173 *state = current;
174 return NULL;
175 }
176 *state = current + *l;
177 } else {
178 *l = strcspn(current, separator);
179 *state = current + *l;
180 }
181
182 return current;
183 }
184
185 char *strnappend(const char *s, const char *suffix, size_t b) {
186 size_t a;
187 char *r;
188
189 if (!s && !suffix)
190 return strdup("");
191
192 if (!s)
193 return strndup(suffix, b);
194
195 if (!suffix)
196 return strdup(s);
197
198 assert(s);
199 assert(suffix);
200
201 a = strlen(s);
202 if (b > ((size_t) -1) - a)
203 return NULL;
204
205 r = new(char, a+b+1);
206 if (!r)
207 return NULL;
208
209 memcpy(r, s, a);
210 memcpy(r+a, suffix, b);
211 r[a+b] = 0;
212
213 return r;
214 }
215
216 char *strappend(const char *s, const char *suffix) {
217 return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
218 }
219
220 char *strjoin(const char *x, ...) {
221 va_list ap;
222 size_t l;
223 char *r, *p;
224
225 va_start(ap, x);
226
227 if (x) {
228 l = strlen(x);
229
230 for (;;) {
231 const char *t;
232 size_t n;
233
234 t = va_arg(ap, const char *);
235 if (!t)
236 break;
237
238 n = strlen(t);
239 if (n > ((size_t) -1) - l) {
240 va_end(ap);
241 return NULL;
242 }
243
244 l += n;
245 }
246 } else
247 l = 0;
248
249 va_end(ap);
250
251 r = new(char, l+1);
252 if (!r)
253 return NULL;
254
255 if (x) {
256 p = stpcpy(r, x);
257
258 va_start(ap, x);
259
260 for (;;) {
261 const char *t;
262
263 t = va_arg(ap, const char *);
264 if (!t)
265 break;
266
267 p = stpcpy(p, t);
268 }
269
270 va_end(ap);
271 } else
272 r[0] = 0;
273
274 return r;
275 }
276
277 char *strstrip(char *s) {
278 char *e;
279
280 /* Drops trailing whitespace. Modifies the string in
281 * place. Returns pointer to first non-space character */
282
283 s += strspn(s, WHITESPACE);
284
285 for (e = strchr(s, 0); e > s; e --)
286 if (!strchr(WHITESPACE, e[-1]))
287 break;
288
289 *e = 0;
290
291 return s;
292 }
293
294 char *delete_chars(char *s, const char *bad) {
295 char *f, *t;
296
297 /* Drops all whitespace, regardless where in the string */
298
299 for (f = s, t = s; *f; f++) {
300 if (strchr(bad, *f))
301 continue;
302
303 *(t++) = *f;
304 }
305
306 *t = 0;
307
308 return s;
309 }
310
311 char *truncate_nl(char *s) {
312 assert(s);
313
314 s[strcspn(s, NEWLINE)] = 0;
315 return s;
316 }
317
318 char ascii_tolower(char x) {
319
320 if (x >= 'A' && x <= 'Z')
321 return x - 'A' + 'a';
322
323 return x;
324 }
325
326 char *ascii_strlower(char *t) {
327 char *p;
328
329 assert(t);
330
331 for (p = t; *p; p++)
332 *p = ascii_tolower(*p);
333
334 return t;
335 }
336
337 char *ascii_strlower_n(char *t, size_t n) {
338 size_t i;
339
340 if (n <= 0)
341 return t;
342
343 for (i = 0; i < n; i++)
344 t[i] = ascii_tolower(t[i]);
345
346 return t;
347 }
348
349 int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
350
351 for (; n > 0; a++, b++, n--) {
352 int x, y;
353
354 x = (int) (uint8_t) ascii_tolower(*a);
355 y = (int) (uint8_t) ascii_tolower(*b);
356
357 if (x != y)
358 return x - y;
359 }
360
361 return 0;
362 }
363
364 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
365 int r;
366
367 r = ascii_strcasecmp_n(a, b, MIN(n, m));
368 if (r != 0)
369 return r;
370
371 if (n < m)
372 return -1;
373 else if (n > m)
374 return 1;
375 else
376 return 0;
377 }
378
379 bool chars_intersect(const char *a, const char *b) {
380 const char *p;
381
382 /* Returns true if any of the chars in a are in b. */
383 for (p = a; *p; p++)
384 if (strchr(b, *p))
385 return true;
386
387 return false;
388 }
389
390 bool string_has_cc(const char *p, const char *ok) {
391 const char *t;
392
393 assert(p);
394
395 /*
396 * Check if a string contains control characters. If 'ok' is
397 * non-NULL it may be a string containing additional CCs to be
398 * considered OK.
399 */
400
401 for (t = p; *t; t++) {
402 if (ok && strchr(ok, *t))
403 continue;
404
405 if (*t > 0 && *t < ' ')
406 return true;
407
408 if (*t == 127)
409 return true;
410 }
411
412 return false;
413 }
414
415 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
416 size_t x;
417 char *r;
418
419 assert(s);
420 assert(percent <= 100);
421 assert(new_length >= 3);
422
423 if (old_length <= 3 || old_length <= new_length)
424 return strndup(s, old_length);
425
426 r = new0(char, new_length+1);
427 if (!r)
428 return NULL;
429
430 x = (new_length * percent) / 100;
431
432 if (x > new_length - 3)
433 x = new_length - 3;
434
435 memcpy(r, s, x);
436 r[x] = '.';
437 r[x+1] = '.';
438 r[x+2] = '.';
439 memcpy(r + x + 3,
440 s + old_length - (new_length - x - 3),
441 new_length - x - 3);
442
443 return r;
444 }
445
446 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
447 size_t x;
448 char *e;
449 const char *i, *j;
450 unsigned k, len, len2;
451 int r;
452
453 assert(s);
454 assert(percent <= 100);
455 assert(new_length >= 3);
456
457 /* if no multibyte characters use ascii_ellipsize_mem for speed */
458 if (ascii_is_valid(s))
459 return ascii_ellipsize_mem(s, old_length, new_length, percent);
460
461 if (old_length <= 3 || old_length <= new_length)
462 return strndup(s, old_length);
463
464 x = (new_length * percent) / 100;
465
466 if (x > new_length - 3)
467 x = new_length - 3;
468
469 k = 0;
470 for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
471 char32_t c;
472
473 r = utf8_encoded_to_unichar(i, &c);
474 if (r < 0)
475 return NULL;
476 k += unichar_iswide(c) ? 2 : 1;
477 }
478
479 if (k > x) /* last character was wide and went over quota */
480 x ++;
481
482 for (j = s + old_length; k < new_length && j > i; ) {
483 char32_t c;
484
485 j = utf8_prev_char(j);
486 r = utf8_encoded_to_unichar(j, &c);
487 if (r < 0)
488 return NULL;
489 k += unichar_iswide(c) ? 2 : 1;
490 }
491 assert(i <= j);
492
493 /* we don't actually need to ellipsize */
494 if (i == j)
495 return memdup(s, old_length + 1);
496
497 /* make space for ellipsis */
498 j = utf8_next_char(j);
499
500 len = i - s;
501 len2 = s + old_length - j;
502 e = new(char, len + 3 + len2 + 1);
503 if (!e)
504 return NULL;
505
506 /*
507 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
508 old_length, new_length, x, len, len2, k);
509 */
510
511 memcpy(e, s, len);
512 e[len] = 0xe2; /* tri-dot ellipsis: … */
513 e[len + 1] = 0x80;
514 e[len + 2] = 0xa6;
515
516 memcpy(e + len + 3, j, len2 + 1);
517
518 return e;
519 }
520
521 char *ellipsize(const char *s, size_t length, unsigned percent) {
522 return ellipsize_mem(s, strlen(s), length, percent);
523 }
524
525 bool nulstr_contains(const char*nulstr, const char *needle) {
526 const char *i;
527
528 if (!nulstr)
529 return false;
530
531 NULSTR_FOREACH(i, nulstr)
532 if (streq(i, needle))
533 return true;
534
535 return false;
536 }
537
538 char* strshorten(char *s, size_t l) {
539 assert(s);
540
541 if (l < strlen(s))
542 s[l] = 0;
543
544 return s;
545 }
546
547 char *strreplace(const char *text, const char *old_string, const char *new_string) {
548 const char *f;
549 char *t, *r;
550 size_t l, old_len, new_len;
551
552 assert(text);
553 assert(old_string);
554 assert(new_string);
555
556 old_len = strlen(old_string);
557 new_len = strlen(new_string);
558
559 l = strlen(text);
560 r = new(char, l+1);
561 if (!r)
562 return NULL;
563
564 f = text;
565 t = r;
566 while (*f) {
567 char *a;
568 size_t d, nl;
569
570 if (!startswith(f, old_string)) {
571 *(t++) = *(f++);
572 continue;
573 }
574
575 d = t - r;
576 nl = l - old_len + new_len;
577 a = realloc(r, nl + 1);
578 if (!a)
579 goto oom;
580
581 l = nl;
582 r = a;
583 t = r + d;
584
585 t = stpcpy(t, new_string);
586 f += old_len;
587 }
588
589 *t = 0;
590 return r;
591
592 oom:
593 free(r);
594 return NULL;
595 }
596
597 char *strip_tab_ansi(char **ibuf, size_t *_isz) {
598 const char *i, *begin = NULL;
599 enum {
600 STATE_OTHER,
601 STATE_ESCAPE,
602 STATE_BRACKET
603 } state = STATE_OTHER;
604 char *obuf = NULL;
605 size_t osz = 0, isz;
606 FILE *f;
607
608 assert(ibuf);
609 assert(*ibuf);
610
611 /* Strips ANSI color and replaces TABs by 8 spaces */
612
613 isz = _isz ? *_isz : strlen(*ibuf);
614
615 f = open_memstream(&obuf, &osz);
616 if (!f)
617 return NULL;
618
619 for (i = *ibuf; i < *ibuf + isz + 1; i++) {
620
621 switch (state) {
622
623 case STATE_OTHER:
624 if (i >= *ibuf + isz) /* EOT */
625 break;
626 else if (*i == '\x1B')
627 state = STATE_ESCAPE;
628 else if (*i == '\t')
629 fputs(" ", f);
630 else
631 fputc(*i, f);
632 break;
633
634 case STATE_ESCAPE:
635 if (i >= *ibuf + isz) { /* EOT */
636 fputc('\x1B', f);
637 break;
638 } else if (*i == '[') {
639 state = STATE_BRACKET;
640 begin = i + 1;
641 } else {
642 fputc('\x1B', f);
643 fputc(*i, f);
644 state = STATE_OTHER;
645 }
646
647 break;
648
649 case STATE_BRACKET:
650
651 if (i >= *ibuf + isz || /* EOT */
652 (!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
653 fputc('\x1B', f);
654 fputc('[', f);
655 state = STATE_OTHER;
656 i = begin-1;
657 } else if (*i == 'm')
658 state = STATE_OTHER;
659 break;
660 }
661 }
662
663 if (ferror(f)) {
664 fclose(f);
665 free(obuf);
666 return NULL;
667 }
668
669 fclose(f);
670
671 free(*ibuf);
672 *ibuf = obuf;
673
674 if (_isz)
675 *_isz = osz;
676
677 return obuf;
678 }
679
680 char *strextend(char **x, ...) {
681 va_list ap;
682 size_t f, l;
683 char *r, *p;
684
685 assert(x);
686
687 l = f = *x ? strlen(*x) : 0;
688
689 va_start(ap, x);
690 for (;;) {
691 const char *t;
692 size_t n;
693
694 t = va_arg(ap, const char *);
695 if (!t)
696 break;
697
698 n = strlen(t);
699 if (n > ((size_t) -1) - l) {
700 va_end(ap);
701 return NULL;
702 }
703
704 l += n;
705 }
706 va_end(ap);
707
708 r = realloc(*x, l+1);
709 if (!r)
710 return NULL;
711
712 p = r + f;
713
714 va_start(ap, x);
715 for (;;) {
716 const char *t;
717
718 t = va_arg(ap, const char *);
719 if (!t)
720 break;
721
722 p = stpcpy(p, t);
723 }
724 va_end(ap);
725
726 *p = 0;
727 *x = r;
728
729 return r + l;
730 }
731
732 char *strrep(const char *s, unsigned n) {
733 size_t l;
734 char *r, *p;
735 unsigned i;
736
737 assert(s);
738
739 l = strlen(s);
740 p = r = malloc(l * n + 1);
741 if (!r)
742 return NULL;
743
744 for (i = 0; i < n; i++)
745 p = stpcpy(p, s);
746
747 *p = 0;
748 return r;
749 }
750
751 int split_pair(const char *s, const char *sep, char **l, char **r) {
752 char *x, *a, *b;
753
754 assert(s);
755 assert(sep);
756 assert(l);
757 assert(r);
758
759 if (isempty(sep))
760 return -EINVAL;
761
762 x = strstr(s, sep);
763 if (!x)
764 return -EINVAL;
765
766 a = strndup(s, x - s);
767 if (!a)
768 return -ENOMEM;
769
770 b = strdup(x + strlen(sep));
771 if (!b) {
772 free(a);
773 return -ENOMEM;
774 }
775
776 *l = a;
777 *r = b;
778
779 return 0;
780 }
781
782 int free_and_strdup(char **p, const char *s) {
783 char *t;
784
785 assert(p);
786
787 /* Replaces a string pointer with an strdup()ed new string,
788 * possibly freeing the old one. */
789
790 if (streq_ptr(*p, s))
791 return 0;
792
793 if (s) {
794 t = strdup(s);
795 if (!t)
796 return -ENOMEM;
797 } else
798 t = NULL;
799
800 free(*p);
801 *p = t;
802
803 return 1;
804 }
805
806 #pragma GCC push_options
807 #pragma GCC optimize("O0")
808
809 void* memory_erase(void *p, size_t l) {
810 volatile uint8_t* x = (volatile uint8_t*) p;
811
812 /* This basically does what memset() does, but hopefully isn't
813 * optimized away by the compiler. One of those days, when
814 * glibc learns memset_s() we should replace this call by
815 * memset_s(), but until then this has to do. */
816
817 for (; l > 0; l--)
818 *(x++) = 'x';
819
820 return p;
821 }
822
823 #pragma GCC pop_options
824
825 char* string_erase(char *x) {
826
827 if (!x)
828 return NULL;
829
830 /* A delicious drop of snake-oil! To be called on memory where
831 * we stored passphrases or so, after we used them. */
832
833 return memory_erase(x, strlen(x));
834 }
835
836 char *string_free_erase(char *s) {
837 return mfree(string_erase(s));
838 }
839
840 bool string_is_safe(const char *p) {
841 const char *t;
842
843 if (!p)
844 return false;
845
846 for (t = p; *t; t++) {
847 if (*t > 0 && *t < ' ') /* no control characters */
848 return false;
849
850 if (strchr(QUOTES "\\\x7f", *t))
851 return false;
852 }
853
854 return true;
855 }