1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
17 /* What is interpreted as whitespace? */
18 #define WHITESPACE " \t\n\r"
19 #define NEWLINE "\n\r"
22 #define GLOB_CHARS "*?["
23 #define DIGITS "0123456789"
24 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
25 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
27 #define ALPHANUMERICAL LETTERS DIGITS
28 #define HEXDIGITS DIGITS "abcdefABCDEF"
30 #define streq(a,b) (strcmp((a),(b)) == 0)
31 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
32 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
33 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
35 int strcmp_ptr(const char *a, const char *b) _pure_;
37 static inline bool streq_ptr(const char *a, const char *b) {
38 return strcmp_ptr(a, b) == 0;
41 static inline const char* strempty(const char *s) {
45 static inline const char* strnull(const char *s) {
49 static inline const char *strna(const char *s) {
53 static inline bool isempty(const char *p) {
57 static inline const char *empty_to_null(const char *p) {
58 return isempty(p) ? NULL : p;
61 #if 0 /// UNNEEDED by elogind
62 static inline const char *strdash_if_empty(const char *str) {
63 return isempty(str) ? "-" : str;
67 static inline char *startswith(const char *s, const char *prefix) {
71 if (strncmp(s, prefix, l) == 0)
77 #if 0 /// UNNEEDED by elogind
78 static inline char *startswith_no_case(const char *s, const char *prefix) {
82 if (strncasecmp(s, prefix, l) == 0)
89 char *endswith(const char *s, const char *postfix) _pure_;
90 char *endswith_no_case(const char *s, const char *postfix) _pure_;
92 char *first_word(const char *s, const char *word) _pure_;
94 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
96 #define FOREACH_WORD(word, length, s, state) \
97 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
99 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
100 _FOREACH_WORD(word, length, s, separator, false, state)
102 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
103 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
105 char *strappend(const char *s, const char *suffix);
106 char *strnappend(const char *s, const char *suffix, size_t length);
108 char *strjoin_real(const char *x, ...) _sentinel_;
109 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
111 #define strjoina(a, ...) \
113 const char *_appendees_[] = { a, __VA_ARGS__ }; \
117 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
118 _len_ += strlen(_appendees_[_i_]); \
119 _p_ = _d_ = alloca(_len_ + 1); \
120 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
121 _p_ = stpcpy(_p_, _appendees_[_i_]); \
126 char *strstrip(char *s);
127 #if 0 /// UNNEEDED by elogind
128 char *delete_chars(char *s, const char *bad);
130 char *delete_trailing_chars(char *s, const char *bad);
131 char *truncate_nl(char *s);
133 #if 0 /// UNNEEDED by elogind
134 static inline char *skip_leading_chars(const char *s, const char *bad) {
142 return (char*) s + strspn(s, bad);
145 char ascii_tolower(char x);
146 char *ascii_strlower(char *s);
147 char *ascii_strlower_n(char *s, size_t n);
149 char ascii_toupper(char x);
150 char *ascii_strupper(char *s);
152 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
153 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
155 bool chars_intersect(const char *a, const char *b) _pure_;
158 static inline bool _pure_ in_charset(const char *s, const char* charset) {
161 return s[strspn(s, charset)] == '\0';
164 bool string_has_cc(const char *p, const char *ok) _pure_;
166 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
167 char *ellipsize(const char *s, size_t length, unsigned percent);
169 bool nulstr_contains(const char *nulstr, const char *needle);
171 char* strshorten(char *s, size_t l);
173 char *strreplace(const char *text, const char *old_string, const char *new_string);
175 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
177 char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
179 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
181 char *strrep(const char *s, unsigned n);
183 int split_pair(const char *s, const char *sep, char **l, char **r);
185 int free_and_strdup(char **p, const char *s);
187 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
188 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
191 return (void*) haystack;
193 if (haystacklen < needlelen)
199 return memmem(haystack, haystacklen, needle, needlelen);
202 #if !HAVE_EXPLICIT_BZERO
203 void explicit_bzero(void *p, size_t l);
206 char *string_erase(char *x);
208 char *string_free_erase(char *s);
209 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
210 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
212 bool string_is_safe(const char *p) _pure_;
214 static inline size_t strlen_ptr(const char *s) {