]>
Commit | Line | Data |
---|---|---|
07630cea LP |
1 | #pragma once |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
11c3a366 | 22 | #include <alloca.h> |
07630cea | 23 | #include <stdbool.h> |
11c3a366 | 24 | #include <stddef.h> |
07630cea LP |
25 | #include <string.h> |
26 | ||
27 | #include "macro.h" | |
28 | ||
b11d6a7b LP |
29 | /* What is interpreted as whitespace? */ |
30 | #define WHITESPACE " \t\n\r" | |
31 | #define NEWLINE "\n\r" | |
32 | #define QUOTES "\"\'" | |
33 | #define COMMENTS "#;" | |
34 | #define GLOB_CHARS "*?[" | |
35 | #define DIGITS "0123456789" | |
36 | #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz" | |
37 | #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
38 | #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS | |
39 | #define ALPHANUMERICAL LETTERS DIGITS | |
1a7906ae | 40 | #define HEXDIGITS DIGITS "abcdefABCDEF" |
b11d6a7b | 41 | |
07630cea LP |
42 | #define streq(a,b) (strcmp((a),(b)) == 0) |
43 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) | |
44 | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) | |
45 | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) | |
46 | ||
47 | int strcmp_ptr(const char *a, const char *b) _pure_; | |
48 | ||
49 | static inline bool streq_ptr(const char *a, const char *b) { | |
50 | return strcmp_ptr(a, b) == 0; | |
51 | } | |
52 | ||
53 | static inline const char* strempty(const char *s) { | |
54 | return s ? s : ""; | |
55 | } | |
56 | ||
57 | static inline const char* strnull(const char *s) { | |
58 | return s ? s : "(null)"; | |
59 | } | |
60 | ||
61 | static inline const char *strna(const char *s) { | |
62 | return s ? s : "n/a"; | |
63 | } | |
64 | ||
65 | static inline bool isempty(const char *p) { | |
66 | return !p || !p[0]; | |
67 | } | |
68 | ||
3c6f7c34 LP |
69 | static inline const char *empty_to_null(const char *p) { |
70 | return isempty(p) ? NULL : p; | |
71 | } | |
72 | ||
07b0b339 SK |
73 | static inline const char *strdash_if_empty(const char *str) { |
74 | return isempty(str) ? "-" : str; | |
75 | } | |
76 | ||
07630cea LP |
77 | static inline char *startswith(const char *s, const char *prefix) { |
78 | size_t l; | |
79 | ||
80 | l = strlen(prefix); | |
81 | if (strncmp(s, prefix, l) == 0) | |
82 | return (char*) s + l; | |
83 | ||
84 | return NULL; | |
85 | } | |
86 | ||
87 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
88 | size_t l; | |
89 | ||
90 | l = strlen(prefix); | |
91 | if (strncasecmp(s, prefix, l) == 0) | |
92 | return (char*) s + l; | |
93 | ||
94 | return NULL; | |
95 | } | |
96 | ||
97 | char *endswith(const char *s, const char *postfix) _pure_; | |
98 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
99 | ||
100 | char *first_word(const char *s, const char *word) _pure_; | |
101 | ||
102 | const char* split(const char **state, size_t *l, const char *separator, bool quoted); | |
103 | ||
104 | #define FOREACH_WORD(word, length, s, state) \ | |
105 | _FOREACH_WORD(word, length, s, WHITESPACE, false, state) | |
106 | ||
107 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
108 | _FOREACH_WORD(word, length, s, separator, false, state) | |
109 | ||
07630cea LP |
110 | #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ |
111 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) | |
112 | ||
113 | char *strappend(const char *s, const char *suffix); | |
114 | char *strnappend(const char *s, const char *suffix, size_t length); | |
115 | ||
605405c6 ZJS |
116 | char *strjoin_real(const char *x, ...) _sentinel_; |
117 | #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL) | |
07630cea LP |
118 | |
119 | #define strjoina(a, ...) \ | |
120 | ({ \ | |
121 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
122 | char *_d_, *_p_; \ | |
35207e25 | 123 | size_t _len_ = 0; \ |
07630cea LP |
124 | unsigned _i_; \ |
125 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
126 | _len_ += strlen(_appendees_[_i_]); \ | |
127 | _p_ = _d_ = alloca(_len_ + 1); \ | |
128 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
129 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
130 | *_p_ = 0; \ | |
131 | _d_; \ | |
132 | }) | |
133 | ||
134 | char *strstrip(char *s); | |
135 | char *delete_chars(char *s, const char *bad); | |
7546145e | 136 | char *delete_trailing_chars(char *s, const char *bad); |
07630cea LP |
137 | char *truncate_nl(char *s); |
138 | ||
7546145e LP |
139 | static inline char *skip_leading_chars(const char *s, const char *bad) { |
140 | ||
141 | if (!s) | |
142 | return NULL; | |
143 | ||
144 | if (!bad) | |
145 | bad = WHITESPACE; | |
146 | ||
147 | return (char*) s + strspn(s, bad); | |
148 | } | |
149 | ||
b577e3d5 LP |
150 | char ascii_tolower(char x); |
151 | char *ascii_strlower(char *s); | |
152 | char *ascii_strlower_n(char *s, size_t n); | |
07630cea | 153 | |
846b8fc3 LP |
154 | char ascii_toupper(char x); |
155 | char *ascii_strupper(char *s); | |
156 | ||
522d85ae | 157 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 158 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 159 | |
07630cea LP |
160 | bool chars_intersect(const char *a, const char *b) _pure_; |
161 | ||
162 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
163 | assert(s); | |
164 | assert(charset); | |
165 | return s[strspn(s, charset)] == '\0'; | |
166 | } | |
167 | ||
168 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
169 | ||
170 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
171 | char *ellipsize(const char *s, size_t length, unsigned percent); | |
172 | ||
2d5dece8 | 173 | bool nulstr_contains(const char *nulstr, const char *needle); |
07630cea LP |
174 | |
175 | char* strshorten(char *s, size_t l); | |
176 | ||
177 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
178 | ||
179 | char *strip_tab_ansi(char **p, size_t *l); | |
180 | ||
181 | char *strextend(char **x, ...) _sentinel_; | |
182 | ||
183 | char *strrep(const char *s, unsigned n); | |
184 | ||
185 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
186 | ||
187 | int free_and_strdup(char **p, const char *s); | |
188 | ||
189 | /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ | |
190 | static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { | |
191 | ||
192 | if (needlelen <= 0) | |
193 | return (void*) haystack; | |
194 | ||
195 | if (haystacklen < needlelen) | |
196 | return NULL; | |
197 | ||
198 | assert(haystack); | |
199 | assert(needle); | |
200 | ||
201 | return memmem(haystack, haystacklen, needle, needlelen); | |
202 | } | |
203 | ||
4b9545f1 | 204 | #if !HAVE_EXPLICIT_BZERO |
2d26d8e0 ZJS |
205 | void explicit_bzero(void *p, size_t l); |
206 | #endif | |
207 | ||
9fe4ea21 | 208 | char *string_erase(char *x); |
07630cea LP |
209 | |
210 | char *string_free_erase(char *s); | |
211 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); | |
212 | #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) | |
f3e2e81d LP |
213 | |
214 | bool string_is_safe(const char *p) _pure_; | |
7bf7ce28 LP |
215 | |
216 | static inline size_t strlen_ptr(const char *s) { | |
217 | if (!s) | |
218 | return 0; | |
219 | ||
220 | return strlen(s); | |
221 | } |