]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
27 #include "alloc-util.h"
30 #include "string-util.h"
34 int strcmp_ptr(const char *a
, const char *b
) {
36 /* Like strcmp(), but tries to make sense of NULL pointers */
49 char* endswith(const char *s
, const char *postfix
) {
59 return (char*) s
+ sl
;
64 if (memcmp(s
+ sl
- pl
, postfix
, pl
) != 0)
67 return (char*) s
+ sl
- pl
;
70 char* endswith_no_case(const char *s
, const char *postfix
) {
80 return (char*) s
+ sl
;
85 if (strcasecmp(s
+ sl
- pl
, postfix
) != 0)
88 return (char*) s
+ sl
- pl
;
91 char* first_word(const char *s
, const char *word
) {
98 /* Checks if the string starts with the specified word, either
99 * followed by NUL or by whitespace. Returns a pointer to the
100 * NUL or the first character after the whitespace. */
111 if (memcmp(s
, word
, wl
) != 0)
118 if (!strchr(WHITESPACE
, *p
))
121 p
+= strspn(p
, WHITESPACE
);
125 static size_t strcspn_escaped(const char *s
, const char *reject
) {
126 bool escaped
= false;
129 for (n
=0; s
[n
]; n
++) {
132 else if (s
[n
] == '\\')
134 else if (strchr(reject
, s
[n
]))
138 /* if s ends in \, return index of previous char */
142 /* Split a string into words. */
143 const char* split(const char **state
, size_t *l
, const char *separator
, bool quoted
) {
149 assert(**state
== '\0');
153 current
+= strspn(current
, separator
);
159 if (quoted
&& strchr("\'\"", *current
)) {
160 char quotechars
[2] = {*current
, '\0'};
162 *l
= strcspn_escaped(current
+ 1, quotechars
);
163 if (current
[*l
+ 1] == '\0' || current
[*l
+ 1] != quotechars
[0] ||
164 (current
[*l
+ 2] && !strchr(separator
, current
[*l
+ 2]))) {
165 /* right quote missing or garbage at the end */
169 *state
= current
++ + *l
+ 2;
171 *l
= strcspn_escaped(current
, separator
);
172 if (current
[*l
] && !strchr(separator
, current
[*l
])) {
173 /* unfinished escape */
177 *state
= current
+ *l
;
179 *l
= strcspn(current
, separator
);
180 *state
= current
+ *l
;
186 char *strnappend(const char *s
, const char *suffix
, size_t b
) {
194 return strndup(suffix
, b
);
203 if (b
> ((size_t) -1) - a
)
206 r
= new(char, a
+b
+1);
211 memcpy(r
+a
, suffix
, b
);
217 char *strappend(const char *s
, const char *suffix
) {
218 return strnappend(s
, suffix
, suffix
? strlen(suffix
) : 0);
221 char *strjoin_real(const char *x
, ...) {
235 t
= va_arg(ap
, const char *);
240 if (n
> ((size_t) -1) - l
) {
264 t
= va_arg(ap
, const char *);
278 char *strstrip(char *s
) {
281 /* Drops trailing whitespace. Modifies the string in
282 * place. Returns pointer to first non-space character */
284 s
+= strspn(s
, WHITESPACE
);
286 for (e
= strchr(s
, 0); e
> s
; e
--)
287 if (!strchr(WHITESPACE
, e
[-1]))
295 char *delete_chars(char *s
, const char *bad
) {
298 /* Drops all whitespace, regardless where in the string */
300 for (f
= s
, t
= s
; *f
; f
++) {
312 char *truncate_nl(char *s
) {
315 s
[strcspn(s
, NEWLINE
)] = 0;
319 char ascii_tolower(char x
) {
321 if (x
>= 'A' && x
<= 'Z')
322 return x
- 'A' + 'a';
327 char ascii_toupper(char x
) {
329 if (x
>= 'a' && x
<= 'z')
330 return x
- 'a' + 'A';
335 char *ascii_strlower(char *t
) {
341 *p
= ascii_tolower(*p
);
346 char *ascii_strupper(char *t
) {
352 *p
= ascii_toupper(*p
);
357 char *ascii_strlower_n(char *t
, size_t n
) {
363 for (i
= 0; i
< n
; i
++)
364 t
[i
] = ascii_tolower(t
[i
]);
369 int ascii_strcasecmp_n(const char *a
, const char *b
, size_t n
) {
371 for (; n
> 0; a
++, b
++, n
--) {
374 x
= (int) (uint8_t) ascii_tolower(*a
);
375 y
= (int) (uint8_t) ascii_tolower(*b
);
384 int ascii_strcasecmp_nn(const char *a
, size_t n
, const char *b
, size_t m
) {
387 r
= ascii_strcasecmp_n(a
, b
, MIN(n
, m
));
399 bool chars_intersect(const char *a
, const char *b
) {
402 /* Returns true if any of the chars in a are in b. */
410 bool string_has_cc(const char *p
, const char *ok
) {
416 * Check if a string contains control characters. If 'ok' is
417 * non-NULL it may be a string containing additional CCs to be
421 for (t
= p
; *t
; t
++) {
422 if (ok
&& strchr(ok
, *t
))
425 if (*t
> 0 && *t
< ' ')
435 static char *ascii_ellipsize_mem(const char *s
, size_t old_length
, size_t new_length
, unsigned percent
) {
440 assert(percent
<= 100);
441 assert(new_length
>= 3);
443 if (old_length
<= 3 || old_length
<= new_length
)
444 return strndup(s
, old_length
);
446 r
= new0(char, new_length
+3);
450 x
= (new_length
* percent
) / 100;
452 if (x
> new_length
- 3)
456 r
[x
] = 0xe2; /* tri-dot ellipsis: … */
460 s
+ old_length
- (new_length
- x
- 1),
466 char *ellipsize_mem(const char *s
, size_t old_length
, size_t new_length
, unsigned percent
) {
470 unsigned k
, len
, len2
;
474 assert(percent
<= 100);
475 assert(new_length
>= 3);
477 /* if no multibyte characters use ascii_ellipsize_mem for speed */
478 if (ascii_is_valid(s
))
479 return ascii_ellipsize_mem(s
, old_length
, new_length
, percent
);
481 if (old_length
<= 3 || old_length
<= new_length
)
482 return strndup(s
, old_length
);
484 x
= (new_length
* percent
) / 100;
486 if (x
> new_length
- 3)
490 for (i
= s
; k
< x
&& i
< s
+ old_length
; i
= utf8_next_char(i
)) {
493 r
= utf8_encoded_to_unichar(i
, &c
);
496 k
+= unichar_iswide(c
) ? 2 : 1;
499 if (k
> x
) /* last character was wide and went over quota */
502 for (j
= s
+ old_length
; k
< new_length
&& j
> i
; ) {
505 j
= utf8_prev_char(j
);
506 r
= utf8_encoded_to_unichar(j
, &c
);
509 k
+= unichar_iswide(c
) ? 2 : 1;
513 /* we don't actually need to ellipsize */
515 return memdup(s
, old_length
+ 1);
517 /* make space for ellipsis */
518 j
= utf8_next_char(j
);
521 len2
= s
+ old_length
- j
;
522 e
= new(char, len
+ 3 + len2
+ 1);
527 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
528 old_length, new_length, x, len, len2, k);
532 e
[len
] = 0xe2; /* tri-dot ellipsis: … */
536 memcpy(e
+ len
+ 3, j
, len2
+ 1);
541 char *ellipsize(const char *s
, size_t length
, unsigned percent
) {
542 return ellipsize_mem(s
, strlen(s
), length
, percent
);
545 bool nulstr_contains(const char *nulstr
, const char *needle
) {
551 NULSTR_FOREACH(i
, nulstr
)
552 if (streq(i
, needle
))
558 char* strshorten(char *s
, size_t l
) {
567 char *strreplace(const char *text
, const char *old_string
, const char *new_string
) {
570 size_t l
, old_len
, new_len
;
576 old_len
= strlen(old_string
);
577 new_len
= strlen(new_string
);
590 if (!startswith(f
, old_string
)) {
596 nl
= l
- old_len
+ new_len
;
597 a
= realloc(r
, nl
+ 1);
605 t
= stpcpy(t
, new_string
);
616 char *strip_tab_ansi(char **ibuf
, size_t *_isz
) {
617 const char *i
, *begin
= NULL
;
622 } state
= STATE_OTHER
;
630 /* Strips ANSI color and replaces TABs by 8 spaces */
632 isz
= _isz
? *_isz
: strlen(*ibuf
);
634 f
= open_memstream(&obuf
, &osz
);
638 /* Note we use the _unlocked() stdio variants on f for performance
639 * reasons. It's safe to do so since we created f here and it
640 * doesn't leave our scope.
643 for (i
= *ibuf
; i
< *ibuf
+ isz
+ 1; i
++) {
648 if (i
>= *ibuf
+ isz
) /* EOT */
650 else if (*i
== '\x1B')
651 state
= STATE_ESCAPE
;
653 fputs_unlocked(" ", f
);
655 fputc_unlocked(*i
, f
);
659 if (i
>= *ibuf
+ isz
) { /* EOT */
660 fputc_unlocked('\x1B', f
);
662 } else if (*i
== '[') {
663 state
= STATE_BRACKET
;
666 fputc_unlocked('\x1B', f
);
667 fputc_unlocked(*i
, f
);
675 if (i
>= *ibuf
+ isz
|| /* EOT */
676 (!(*i
>= '0' && *i
<= '9') && *i
!= ';' && *i
!= 'm')) {
677 fputc_unlocked('\x1B', f
);
678 fputc_unlocked('[', f
);
681 } else if (*i
== 'm')
703 char *strextend(char **x
, ...) {
710 l
= f
= *x
? strlen(*x
) : 0;
717 t
= va_arg(ap
, const char *);
722 if (n
> ((size_t) -1) - l
) {
731 r
= realloc(*x
, l
+1);
741 t
= va_arg(ap
, const char *);
755 char *strrep(const char *s
, unsigned n
) {
763 p
= r
= malloc(l
* n
+ 1);
767 for (i
= 0; i
< n
; i
++)
774 int split_pair(const char *s
, const char *sep
, char **l
, char **r
) {
789 a
= strndup(s
, x
- s
);
793 b
= strdup(x
+ strlen(sep
));
805 int free_and_strdup(char **p
, const char *s
) {
810 /* Replaces a string pointer with an strdup()ed new string,
811 * possibly freeing the old one. */
813 if (streq_ptr(*p
, s
))
829 #if !HAVE_DECL_EXPLICIT_BZERO
831 * Pointer to memset is volatile so that compiler must de-reference
832 * the pointer and can't assume that it points to any function in
833 * particular (such as memset, which it then might further "optimize")
834 * This approach is inspired by openssl's crypto/mem_clr.c.
836 typedef void *(*memset_t
)(void *,int,size_t);
838 static volatile memset_t memset_func
= memset
;
840 void explicit_bzero(void *p
, size_t l
) {
841 memset_func(p
, '\0', l
);
845 char* string_erase(char *x
) {
849 /* A delicious drop of snake-oil! To be called on memory where
850 * we stored passphrases or so, after we used them. */
851 explicit_bzero(x
, strlen(x
));
855 char *string_free_erase(char *s
) {
856 return mfree(string_erase(s
));
859 bool string_is_safe(const char *p
) {
865 for (t
= p
; *t
; t
++) {
866 if (*t
> 0 && *t
< ' ') /* no control characters */
869 if (strchr(QUOTES
"\\\x7f", *t
))