/src/util-linux/include/strutils.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * No copyright is claimed. This code is in the public domain; do with |
3 | | * it what you wish. |
4 | | */ |
5 | | #ifndef UTIL_LINUX_STRUTILS |
6 | | #define UTIL_LINUX_STRUTILS |
7 | | |
8 | | #include <stdlib.h> |
9 | | #include <inttypes.h> |
10 | | #include <string.h> |
11 | | #include <strings.h> |
12 | | #include <sys/types.h> |
13 | | #include <ctype.h> |
14 | | #include <stdio.h> |
15 | | #include <errno.h> |
16 | | #include <time.h> |
17 | | #include <stdbool.h> |
18 | | |
19 | | #include "c.h" |
20 | | |
21 | | /* initialize a custom exit code for all *_or_err functions */ |
22 | | extern void strutils_set_exitcode(int exit_code); |
23 | | |
24 | | extern int ul_parse_size(const char *str, uintmax_t *res, int *power); |
25 | | extern int strtosize(const char *str, uintmax_t *res); |
26 | | extern uintmax_t strtosize_or_err(const char *str, const char *errmesg); |
27 | | |
28 | | extern int ul_strtos64(const char *str, int64_t *num, int base); |
29 | | extern int ul_strtou64(const char *str, uint64_t *num, int base); |
30 | | extern int ul_strtos32(const char *str, int32_t *num, int base); |
31 | | extern int ul_strtou32(const char *str, uint32_t *num, int base); |
32 | | extern int ul_strtold(const char *str, long double *num); |
33 | | |
34 | | extern int64_t str2num_or_err(const char *str, int base, const char *errmesg, int64_t low, int64_t up); |
35 | | extern uint64_t str2unum_or_err(const char *str, int base, const char *errmesg, uint64_t up); |
36 | | |
37 | 0 | #define strtos64_or_err(_s, _e) str2num_or_err(_s, 10, _e, 0, 0) |
38 | | #define strtou64_or_err(_s, _e) str2unum_or_err(_s, 10, _e, 0) |
39 | | #define strtox64_or_err(_s, _e) str2unum_or_err(_s, 16, _e, 0) |
40 | | |
41 | | #define strtos32_or_err(_s, _e) (int32_t) str2num_or_err(_s, 10, _e, INT32_MIN, INT32_MAX) |
42 | | #define strtou32_or_err(_s, _e) (uint32_t) str2unum_or_err(_s, 10, _e, UINT32_MAX) |
43 | | #define strtox32_or_err(_s, _e) (uint32_t) str2unum_or_err(_s, 16, _e, UINT32_MAX) |
44 | | |
45 | | #define strtos16_or_err(_s, _e) (int16_t) str2num_or_err(_s, 10, _e, INT16_MIN, INT16_MAX) |
46 | | #define strtou16_or_err(_s, _e) (uint16_t) str2unum_or_err(_s, 10, _e, UINT16_MAX) |
47 | | #define strtox16_or_err(_s, _e) (uint16_t) str2unum_or_err(_s, 16, _e, UINT16_MAX) |
48 | | |
49 | | extern double strtod_or_err(const char *str, const char *errmesg); |
50 | | extern long double strtold_or_err(const char *str, const char *errmesg); |
51 | | |
52 | | #define strtol_or_err(_s, _e) (long) str2num_or_err(_s, 10, _e, LONG_MIN, LONG_MAX) |
53 | | #define strtopid_or_err(_s, _e) (pid_t) str2num_or_err(_s, 10, _e, 1, SINT_MAX(pid_t)) |
54 | | #define strtoul_or_err(_s, _e) (unsigned long) str2unum_or_err(_s, 10, _e, ULONG_MAX) |
55 | | |
56 | | extern void strtotimeval_or_err(const char *str, struct timeval *tv, |
57 | | const char *errmesg); |
58 | | extern void strtotimespec_or_err(const char *str, struct timespec *ts, |
59 | | const char *errmesg); |
60 | | extern time_t strtotime_or_err(const char *str, const char *errmesg); |
61 | | |
62 | | extern bool hyperlinkwanted(const char *mode); |
63 | | |
64 | | extern int isdigit_strend(const char *str, const char **end); |
65 | | #define isdigit_string(_s) isdigit_strend(_s, NULL) |
66 | | |
67 | | extern int isxdigit_strend(const char *str, const char **end); |
68 | | #define isxdigit_string(_s) isxdigit_strend(_s, NULL) |
69 | | |
70 | | |
71 | | extern int ul_parse_switch(const char *arg, ...); |
72 | | |
73 | | #ifndef HAVE_MEMPCPY |
74 | | extern void *mempcpy(void *restrict dest, const void *restrict src, size_t n); |
75 | | #endif |
76 | | #ifndef HAVE_STRNLEN |
77 | | extern size_t strnlen(const char *s, size_t maxlen); |
78 | | #endif |
79 | | #ifndef HAVE_STRNDUP |
80 | | extern char *strndup(const char *s, size_t n); |
81 | | #endif |
82 | | #ifndef HAVE_STRNCHR |
83 | | extern char *strnchr(const char *s, size_t maxlen, int c); |
84 | | #endif |
85 | | |
86 | | /* caller guarantees n > 0 */ |
87 | | static inline int xstrncpy(char *dest, const char *src, size_t n) |
88 | 0 | { |
89 | 0 | size_t len = src ? strlen(src) : 0; |
90 | |
|
91 | 0 | if (!len) |
92 | 0 | return 0; |
93 | 0 | len = min(len, n - 1); |
94 | 0 | memcpy(dest, src, len); |
95 | 0 | dest[len] = 0; |
96 | 0 | return len; |
97 | 0 | } Unexecuted instantiation: gen_uuid.c:xstrncpy Unexecuted instantiation: probe.c:xstrncpy Unexecuted instantiation: partitions.c:xstrncpy Unexecuted instantiation: path.c:xstrncpy Unexecuted instantiation: sysfs.c:xstrncpy Unexecuted instantiation: devname.c:xstrncpy Unexecuted instantiation: devno.c:xstrncpy Unexecuted instantiation: buffer.c:xstrncpy Unexecuted instantiation: canonicalize.c:xstrncpy Unexecuted instantiation: mbsalign.c:xstrncpy Unexecuted instantiation: strv.c:xstrncpy Unexecuted instantiation: strutils.c:xstrncpy |
98 | | |
99 | | /* This is like strncpy(), but based on memcpy(), so compilers and static |
100 | | * analyzers do not complain when sizeof(destination) is the same as 'n' and |
101 | | * result is not terminated by zero. |
102 | | * |
103 | | * Use this function to copy string to logs with fixed sizes (wtmp/utmp. ...) |
104 | | * where string terminator is optional. |
105 | | */ |
106 | | static inline void * __attribute__((nonnull (1))) |
107 | | str2memcpy(void *dest, const char *src, size_t n) |
108 | 0 | { |
109 | 0 | size_t bytes = strlen(src) + 1; |
110 | 0 |
|
111 | 0 | if (bytes > n) |
112 | 0 | bytes = n; |
113 | 0 |
|
114 | 0 | memcpy(dest, src, bytes); |
115 | 0 | return dest; |
116 | 0 | } Unexecuted instantiation: gen_uuid.c:str2memcpy Unexecuted instantiation: probe.c:str2memcpy Unexecuted instantiation: partitions.c:str2memcpy Unexecuted instantiation: path.c:str2memcpy Unexecuted instantiation: sysfs.c:str2memcpy Unexecuted instantiation: devname.c:str2memcpy Unexecuted instantiation: devno.c:str2memcpy Unexecuted instantiation: buffer.c:str2memcpy Unexecuted instantiation: canonicalize.c:str2memcpy Unexecuted instantiation: mbsalign.c:str2memcpy Unexecuted instantiation: strv.c:str2memcpy Unexecuted instantiation: strutils.c:str2memcpy |
117 | | |
118 | | static inline char * __attribute__((nonnull (1))) |
119 | | mem2strcpy(char *dest, const void *src, size_t n, size_t nmax) |
120 | 0 | { |
121 | 0 | if (n + 1 > nmax) |
122 | 0 | n = nmax - 1; |
123 | 0 |
|
124 | 0 | memset(dest, '\0', nmax); |
125 | 0 | memcpy(dest, src, n); |
126 | 0 | return dest; |
127 | 0 | } Unexecuted instantiation: gen_uuid.c:mem2strcpy Unexecuted instantiation: probe.c:mem2strcpy Unexecuted instantiation: partitions.c:mem2strcpy Unexecuted instantiation: path.c:mem2strcpy Unexecuted instantiation: sysfs.c:mem2strcpy Unexecuted instantiation: devname.c:mem2strcpy Unexecuted instantiation: devno.c:mem2strcpy Unexecuted instantiation: buffer.c:mem2strcpy Unexecuted instantiation: canonicalize.c:mem2strcpy Unexecuted instantiation: mbsalign.c:mem2strcpy Unexecuted instantiation: strv.c:mem2strcpy Unexecuted instantiation: strutils.c:mem2strcpy |
128 | | |
129 | | /* Reallocate @str according to @newstr and copy @newstr to @str; returns new @str. |
130 | | * The @str is not modified if reallocation failed (like classic realloc()). |
131 | | */ |
132 | | static inline char * __attribute__((warn_unused_result)) |
133 | | strrealloc(char *str, const char *newstr) |
134 | 0 | { |
135 | 0 | size_t nsz, osz; |
136 | 0 |
|
137 | 0 | if (!str) |
138 | 0 | return newstr ? strdup(newstr) : NULL; |
139 | 0 | if (!newstr) |
140 | 0 | return NULL; |
141 | 0 |
|
142 | 0 | osz = strlen(str); |
143 | 0 | nsz = strlen(newstr); |
144 | 0 |
|
145 | 0 | if (nsz > osz) |
146 | 0 | str = realloc(str, nsz + 1); |
147 | 0 | if (str) |
148 | 0 | memcpy(str, newstr, nsz + 1); |
149 | 0 | return str; |
150 | 0 | } Unexecuted instantiation: gen_uuid.c:strrealloc Unexecuted instantiation: probe.c:strrealloc Unexecuted instantiation: partitions.c:strrealloc Unexecuted instantiation: path.c:strrealloc Unexecuted instantiation: sysfs.c:strrealloc Unexecuted instantiation: devname.c:strrealloc Unexecuted instantiation: devno.c:strrealloc Unexecuted instantiation: buffer.c:strrealloc Unexecuted instantiation: canonicalize.c:strrealloc Unexecuted instantiation: mbsalign.c:strrealloc Unexecuted instantiation: strv.c:strrealloc Unexecuted instantiation: strutils.c:strrealloc |
151 | | |
152 | | /* Copy string @str to struct @stru to member addressed by @offset */ |
153 | | static inline int strdup_to_offset(void *stru, size_t offset, const char *str) |
154 | 0 | { |
155 | 0 | char **o; |
156 | 0 | char *p = NULL; |
157 | 0 |
|
158 | 0 | if (!stru) |
159 | 0 | return -EINVAL; |
160 | 0 |
|
161 | 0 | o = (char **) ((char *) stru + offset); |
162 | 0 | if (str) { |
163 | 0 | p = strdup(str); |
164 | 0 | if (!p) |
165 | 0 | return -ENOMEM; |
166 | 0 | } |
167 | 0 |
|
168 | 0 | free(*o); |
169 | 0 | *o = p; |
170 | 0 | return 0; |
171 | 0 | } Unexecuted instantiation: gen_uuid.c:strdup_to_offset Unexecuted instantiation: probe.c:strdup_to_offset Unexecuted instantiation: partitions.c:strdup_to_offset Unexecuted instantiation: path.c:strdup_to_offset Unexecuted instantiation: sysfs.c:strdup_to_offset Unexecuted instantiation: devname.c:strdup_to_offset Unexecuted instantiation: devno.c:strdup_to_offset Unexecuted instantiation: buffer.c:strdup_to_offset Unexecuted instantiation: canonicalize.c:strdup_to_offset Unexecuted instantiation: mbsalign.c:strdup_to_offset Unexecuted instantiation: strv.c:strdup_to_offset Unexecuted instantiation: strutils.c:strdup_to_offset |
172 | | |
173 | | /* Copy string __str to struct member _m of the struct _s */ |
174 | | #define strdup_to_struct_member(_s, _m, _str) \ |
175 | | strdup_to_offset((void *) _s, offsetof(__typeof__(*(_s)), _m), _str) |
176 | | |
177 | | /* Copy string addressed by @offset between two structs */ |
178 | | static inline int strdup_between_offsets(void *stru_dst, void *stru_src, size_t offset) |
179 | 0 | { |
180 | 0 | char **src; |
181 | 0 | char **dst; |
182 | 0 | char *p = NULL; |
183 | 0 |
|
184 | 0 | if (!stru_src || !stru_dst) |
185 | 0 | return -EINVAL; |
186 | 0 |
|
187 | 0 | src = (char **) ((char *) stru_src + offset); |
188 | 0 | dst = (char **) ((char *) stru_dst + offset); |
189 | 0 |
|
190 | 0 | if (*src) { |
191 | 0 | p = strdup(*src); |
192 | 0 | if (!p) |
193 | 0 | return -ENOMEM; |
194 | 0 | } |
195 | 0 |
|
196 | 0 | free(*dst); |
197 | 0 | *dst = p; |
198 | 0 | return 0; |
199 | 0 | } Unexecuted instantiation: gen_uuid.c:strdup_between_offsets Unexecuted instantiation: probe.c:strdup_between_offsets Unexecuted instantiation: partitions.c:strdup_between_offsets Unexecuted instantiation: path.c:strdup_between_offsets Unexecuted instantiation: sysfs.c:strdup_between_offsets Unexecuted instantiation: devname.c:strdup_between_offsets Unexecuted instantiation: devno.c:strdup_between_offsets Unexecuted instantiation: buffer.c:strdup_between_offsets Unexecuted instantiation: canonicalize.c:strdup_between_offsets Unexecuted instantiation: mbsalign.c:strdup_between_offsets Unexecuted instantiation: strv.c:strdup_between_offsets Unexecuted instantiation: strutils.c:strdup_between_offsets |
200 | | |
201 | | /* Copy string addressed by struct member between two instances of the same |
202 | | * struct type */ |
203 | | #define strdup_between_structs(_dst, _src, _m) \ |
204 | | strdup_between_offsets((void *)_dst, (void *)_src, offsetof(__typeof__(*(_src)), _m)) |
205 | | |
206 | | static inline int is_nonnull_offset(const void *stru, size_t offset) |
207 | 0 | { |
208 | 0 | const char **o; |
209 | 0 |
|
210 | 0 | if (!stru) |
211 | 0 | return -EINVAL; |
212 | 0 |
|
213 | 0 | o = (const char **) ((const char *) stru + offset); |
214 | 0 | return *o != NULL; |
215 | 0 | } Unexecuted instantiation: gen_uuid.c:is_nonnull_offset Unexecuted instantiation: probe.c:is_nonnull_offset Unexecuted instantiation: partitions.c:is_nonnull_offset Unexecuted instantiation: path.c:is_nonnull_offset Unexecuted instantiation: sysfs.c:is_nonnull_offset Unexecuted instantiation: devname.c:is_nonnull_offset Unexecuted instantiation: devno.c:is_nonnull_offset Unexecuted instantiation: buffer.c:is_nonnull_offset Unexecuted instantiation: canonicalize.c:is_nonnull_offset Unexecuted instantiation: mbsalign.c:is_nonnull_offset Unexecuted instantiation: strv.c:is_nonnull_offset Unexecuted instantiation: strutils.c:is_nonnull_offset |
216 | | |
217 | | #define is_nonnull_member(_stru, _m) \ |
218 | | is_nonnull_offset((void *) _stru, offsetof(__typeof__(*(_stru)), _m)) |
219 | | |
220 | | static inline int strcmp_offsets(const void *sa, const void *sb, size_t offset) |
221 | 0 | { |
222 | 0 | const char **a = (const char **) ((const char *) sa + offset), |
223 | 0 | **b = (const char **) ((const char *) sb + offset); |
224 | 0 |
|
225 | 0 | if (!*a && !*b) |
226 | 0 | return 0; |
227 | 0 | if (!*a) |
228 | 0 | return -1; |
229 | 0 | if (!*b) |
230 | 0 | return 1; |
231 | 0 | return strcmp(*a, *b); |
232 | 0 | } Unexecuted instantiation: gen_uuid.c:strcmp_offsets Unexecuted instantiation: probe.c:strcmp_offsets Unexecuted instantiation: partitions.c:strcmp_offsets Unexecuted instantiation: path.c:strcmp_offsets Unexecuted instantiation: sysfs.c:strcmp_offsets Unexecuted instantiation: devname.c:strcmp_offsets Unexecuted instantiation: devno.c:strcmp_offsets Unexecuted instantiation: buffer.c:strcmp_offsets Unexecuted instantiation: canonicalize.c:strcmp_offsets Unexecuted instantiation: mbsalign.c:strcmp_offsets Unexecuted instantiation: strv.c:strcmp_offsets Unexecuted instantiation: strutils.c:strcmp_offsets |
233 | | |
234 | | #define strcmp_members(_a, _b, _m) \ |
235 | | strcmp_offsets((void *) _a, (void *) _b, offsetof(__typeof__(*(_a)), _m)) |
236 | | |
237 | | extern char *xstrmode(mode_t mode, char *str); |
238 | | |
239 | | /* Options for size_to_human_string() */ |
240 | | enum |
241 | | { |
242 | | SIZE_SUFFIX_1LETTER = 0, |
243 | | SIZE_SUFFIX_3LETTER = (1 << 0), |
244 | | SIZE_SUFFIX_SPACE = (1 << 1), |
245 | | SIZE_DECIMAL_2DIGITS = (1 << 2) |
246 | | }; |
247 | | |
248 | | extern char *size_to_human_string(int options, uint64_t bytes); |
249 | | |
250 | | extern int string_to_idarray(const char *list, int ary[], size_t arysz, |
251 | | int (name2id)(const char *, size_t)); |
252 | | extern int string_add_to_idarray(const char *list, int ary[], |
253 | | size_t arysz, size_t *ary_pos, |
254 | | int (name2id)(const char *, size_t)); |
255 | | |
256 | | extern int string_to_bitarray(const char *list, char *ary, |
257 | | int (*name2bit)(const char *, size_t), |
258 | | size_t allow_range); |
259 | | |
260 | | extern int string_to_bitmask(const char *list, |
261 | | unsigned long *mask, |
262 | | long (*name2flag)(const char *, size_t)); |
263 | | extern int ul_parse_range(const char *str, int *lower, int *upper, int def); |
264 | | |
265 | | extern int streq_paths(const char *a, const char *b); |
266 | | |
267 | | /* |
268 | | * Match string beginning. |
269 | | */ |
270 | | static inline const char *startswith(const char *s, const char *prefix) |
271 | 0 | { |
272 | 0 | size_t sz = prefix ? strlen(prefix) : 0; |
273 | |
|
274 | 0 | if (s && sz && strncmp(s, prefix, sz) == 0) |
275 | 0 | return s + sz; |
276 | 0 | return NULL; |
277 | 0 | } Unexecuted instantiation: gen_uuid.c:startswith Unexecuted instantiation: probe.c:startswith Unexecuted instantiation: partitions.c:startswith Unexecuted instantiation: path.c:startswith Unexecuted instantiation: sysfs.c:startswith Unexecuted instantiation: devname.c:startswith Unexecuted instantiation: devno.c:startswith Unexecuted instantiation: buffer.c:startswith Unexecuted instantiation: canonicalize.c:startswith Unexecuted instantiation: mbsalign.c:startswith Unexecuted instantiation: strv.c:startswith Unexecuted instantiation: strutils.c:startswith |
278 | | |
279 | | /* |
280 | | * Case insensitive match string beginning. |
281 | | */ |
282 | | static inline const char *startswith_no_case(const char *s, const char *prefix) |
283 | 0 | { |
284 | 0 | size_t sz = prefix ? strlen(prefix) : 0; |
285 | 0 |
|
286 | 0 | if (s && sz && strncasecmp(s, prefix, sz) == 0) |
287 | 0 | return s + sz; |
288 | 0 | return NULL; |
289 | 0 | } Unexecuted instantiation: gen_uuid.c:startswith_no_case Unexecuted instantiation: probe.c:startswith_no_case Unexecuted instantiation: partitions.c:startswith_no_case Unexecuted instantiation: path.c:startswith_no_case Unexecuted instantiation: sysfs.c:startswith_no_case Unexecuted instantiation: devname.c:startswith_no_case Unexecuted instantiation: devno.c:startswith_no_case Unexecuted instantiation: buffer.c:startswith_no_case Unexecuted instantiation: canonicalize.c:startswith_no_case Unexecuted instantiation: mbsalign.c:startswith_no_case Unexecuted instantiation: strv.c:startswith_no_case Unexecuted instantiation: strutils.c:startswith_no_case |
290 | | |
291 | | /* |
292 | | * Match string ending. |
293 | | */ |
294 | | static inline const char *endswith(const char *s, const char *postfix) |
295 | 0 | { |
296 | 0 | size_t sl = s ? strlen(s) : 0; |
297 | 0 | size_t pl = postfix ? strlen(postfix) : 0; |
298 | 0 |
|
299 | 0 | if (pl == 0) |
300 | 0 | return s + sl; |
301 | 0 | if (sl < pl) |
302 | 0 | return NULL; |
303 | 0 | if (memcmp(s + sl - pl, postfix, pl) != 0) |
304 | 0 | return NULL; |
305 | 0 | return s + sl - pl; |
306 | 0 | } Unexecuted instantiation: gen_uuid.c:endswith Unexecuted instantiation: probe.c:endswith Unexecuted instantiation: partitions.c:endswith Unexecuted instantiation: path.c:endswith Unexecuted instantiation: sysfs.c:endswith Unexecuted instantiation: devname.c:endswith Unexecuted instantiation: devno.c:endswith Unexecuted instantiation: buffer.c:endswith Unexecuted instantiation: canonicalize.c:endswith Unexecuted instantiation: mbsalign.c:endswith Unexecuted instantiation: strv.c:endswith Unexecuted instantiation: strutils.c:endswith |
307 | | |
308 | | /* |
309 | | * Skip leading white space. |
310 | | */ |
311 | | static inline const char *skip_space(const char *p) |
312 | 0 | { |
313 | 0 | while (isspace(*p)) |
314 | 0 | ++p; |
315 | 0 | return p; |
316 | 0 | } Unexecuted instantiation: gen_uuid.c:skip_space Unexecuted instantiation: probe.c:skip_space Unexecuted instantiation: partitions.c:skip_space Unexecuted instantiation: path.c:skip_space Unexecuted instantiation: sysfs.c:skip_space Unexecuted instantiation: devname.c:skip_space Unexecuted instantiation: devno.c:skip_space Unexecuted instantiation: buffer.c:skip_space Unexecuted instantiation: canonicalize.c:skip_space Unexecuted instantiation: mbsalign.c:skip_space Unexecuted instantiation: strv.c:skip_space Unexecuted instantiation: strutils.c:skip_space |
317 | | |
318 | | static inline const char *skip_blank(const char *p) |
319 | 0 | { |
320 | 0 | while (isblank(*p)) |
321 | 0 | ++p; |
322 | 0 | return p; |
323 | 0 | } Unexecuted instantiation: gen_uuid.c:skip_blank Unexecuted instantiation: probe.c:skip_blank Unexecuted instantiation: partitions.c:skip_blank Unexecuted instantiation: path.c:skip_blank Unexecuted instantiation: sysfs.c:skip_blank Unexecuted instantiation: devname.c:skip_blank Unexecuted instantiation: devno.c:skip_blank Unexecuted instantiation: buffer.c:skip_blank Unexecuted instantiation: canonicalize.c:skip_blank Unexecuted instantiation: mbsalign.c:skip_blank Unexecuted instantiation: strv.c:skip_blank Unexecuted instantiation: strutils.c:skip_blank |
324 | | |
325 | | |
326 | | /* Removes whitespace from the right-hand side of a string (trailing |
327 | | * whitespace). |
328 | | * |
329 | | * Returns size of the new string (without \0). |
330 | | */ |
331 | | static inline size_t rtrim_whitespace(unsigned char *str) |
332 | 0 | { |
333 | 0 | size_t i; |
334 | |
|
335 | 0 | if (!str) |
336 | 0 | return 0; |
337 | 0 | i = strlen((char *) str); |
338 | 0 | while (i) { |
339 | 0 | i--; |
340 | 0 | if (!isspace(str[i])) { |
341 | 0 | i++; |
342 | 0 | break; |
343 | 0 | } |
344 | 0 | } |
345 | 0 | str[i] = '\0'; |
346 | 0 | return i; |
347 | 0 | } Unexecuted instantiation: gen_uuid.c:rtrim_whitespace Unexecuted instantiation: probe.c:rtrim_whitespace Unexecuted instantiation: partitions.c:rtrim_whitespace Unexecuted instantiation: path.c:rtrim_whitespace Unexecuted instantiation: sysfs.c:rtrim_whitespace Unexecuted instantiation: devname.c:rtrim_whitespace Unexecuted instantiation: devno.c:rtrim_whitespace Unexecuted instantiation: buffer.c:rtrim_whitespace Unexecuted instantiation: canonicalize.c:rtrim_whitespace Unexecuted instantiation: mbsalign.c:rtrim_whitespace Unexecuted instantiation: strv.c:rtrim_whitespace Unexecuted instantiation: strutils.c:rtrim_whitespace |
348 | | |
349 | | /* Removes whitespace from the left-hand side of a string. |
350 | | * |
351 | | * Returns size of the new string (without \0). |
352 | | */ |
353 | | static inline size_t ltrim_whitespace(unsigned char *str) |
354 | 0 | { |
355 | 0 | size_t len; |
356 | 0 | unsigned char *p; |
357 | |
|
358 | 0 | if (!str) |
359 | 0 | return 0; |
360 | 0 | for (p = str; *p && isspace(*p); p++); |
361 | |
|
362 | 0 | len = strlen((char *) p); |
363 | |
|
364 | 0 | if (p > str) |
365 | 0 | memmove(str, p, len + 1); |
366 | |
|
367 | 0 | return len; |
368 | 0 | } Unexecuted instantiation: gen_uuid.c:ltrim_whitespace Unexecuted instantiation: probe.c:ltrim_whitespace Unexecuted instantiation: partitions.c:ltrim_whitespace Unexecuted instantiation: path.c:ltrim_whitespace Unexecuted instantiation: sysfs.c:ltrim_whitespace Unexecuted instantiation: devname.c:ltrim_whitespace Unexecuted instantiation: devno.c:ltrim_whitespace Unexecuted instantiation: buffer.c:ltrim_whitespace Unexecuted instantiation: canonicalize.c:ltrim_whitespace Unexecuted instantiation: mbsalign.c:ltrim_whitespace Unexecuted instantiation: strv.c:ltrim_whitespace Unexecuted instantiation: strutils.c:ltrim_whitespace |
369 | | |
370 | | /* Removes left-hand, right-hand and repeating whitespaces. |
371 | | */ |
372 | | static inline size_t __normalize_whitespace( |
373 | | const unsigned char *src, |
374 | | size_t sz, |
375 | | unsigned char *dst, |
376 | | size_t len) |
377 | 0 | { |
378 | 0 | size_t i, x = 0; |
379 | 0 | int nsp = 0, intext = 0; |
380 | 0 |
|
381 | 0 | if (!sz) |
382 | 0 | goto done; |
383 | 0 |
|
384 | 0 | for (i = 0, x = 0; i < sz && x < len - 1; ) { |
385 | 0 | if (isspace(src[i])) |
386 | 0 | nsp++; |
387 | 0 | else |
388 | 0 | nsp = 0, intext = 1; |
389 | 0 |
|
390 | 0 | if (nsp > 1 || (nsp && !intext)) |
391 | 0 | i++; |
392 | 0 | else |
393 | 0 | dst[x++] = src[i++]; |
394 | 0 | } |
395 | 0 | if (nsp && x > 0) /* trailing space */ |
396 | 0 | x--; |
397 | 0 | done: |
398 | 0 | dst[x] = '\0'; |
399 | 0 | return x; |
400 | 0 | } Unexecuted instantiation: gen_uuid.c:__normalize_whitespace Unexecuted instantiation: probe.c:__normalize_whitespace Unexecuted instantiation: partitions.c:__normalize_whitespace Unexecuted instantiation: path.c:__normalize_whitespace Unexecuted instantiation: sysfs.c:__normalize_whitespace Unexecuted instantiation: devname.c:__normalize_whitespace Unexecuted instantiation: devno.c:__normalize_whitespace Unexecuted instantiation: buffer.c:__normalize_whitespace Unexecuted instantiation: canonicalize.c:__normalize_whitespace Unexecuted instantiation: mbsalign.c:__normalize_whitespace Unexecuted instantiation: strv.c:__normalize_whitespace Unexecuted instantiation: strutils.c:__normalize_whitespace |
401 | | |
402 | | static inline size_t normalize_whitespace(unsigned char *str) |
403 | 0 | { |
404 | 0 | size_t sz = strlen((char *) str); |
405 | 0 | return __normalize_whitespace(str, sz, str, sz + 1); |
406 | 0 | } Unexecuted instantiation: gen_uuid.c:normalize_whitespace Unexecuted instantiation: probe.c:normalize_whitespace Unexecuted instantiation: partitions.c:normalize_whitespace Unexecuted instantiation: path.c:normalize_whitespace Unexecuted instantiation: sysfs.c:normalize_whitespace Unexecuted instantiation: devname.c:normalize_whitespace Unexecuted instantiation: devno.c:normalize_whitespace Unexecuted instantiation: buffer.c:normalize_whitespace Unexecuted instantiation: canonicalize.c:normalize_whitespace Unexecuted instantiation: mbsalign.c:normalize_whitespace Unexecuted instantiation: strv.c:normalize_whitespace Unexecuted instantiation: strutils.c:normalize_whitespace |
407 | | |
408 | | static inline void strrep(char *s, int find, int replace) |
409 | 0 | { |
410 | 0 | while (s && *s && (s = strchr(s, find)) != NULL) |
411 | 0 | *s++ = replace; |
412 | 0 | } Unexecuted instantiation: gen_uuid.c:strrep Unexecuted instantiation: probe.c:strrep Unexecuted instantiation: partitions.c:strrep Unexecuted instantiation: path.c:strrep Unexecuted instantiation: sysfs.c:strrep Unexecuted instantiation: devname.c:strrep Unexecuted instantiation: devno.c:strrep Unexecuted instantiation: buffer.c:strrep Unexecuted instantiation: canonicalize.c:strrep Unexecuted instantiation: mbsalign.c:strrep Unexecuted instantiation: strv.c:strrep Unexecuted instantiation: strutils.c:strrep |
413 | | |
414 | | static inline void strrem(char *s, int rem) |
415 | 0 | { |
416 | 0 | char *p; |
417 | 0 |
|
418 | 0 | if (!s) |
419 | 0 | return; |
420 | 0 | for (p = s; *s; s++) { |
421 | 0 | if (*s != rem) |
422 | 0 | *p++ = *s; |
423 | 0 | } |
424 | 0 | *p = '\0'; |
425 | 0 | } Unexecuted instantiation: gen_uuid.c:strrem Unexecuted instantiation: probe.c:strrem Unexecuted instantiation: partitions.c:strrem Unexecuted instantiation: path.c:strrem Unexecuted instantiation: sysfs.c:strrem Unexecuted instantiation: devname.c:strrem Unexecuted instantiation: devno.c:strrem Unexecuted instantiation: buffer.c:strrem Unexecuted instantiation: canonicalize.c:strrem Unexecuted instantiation: mbsalign.c:strrem Unexecuted instantiation: strv.c:strrem Unexecuted instantiation: strutils.c:strrem |
426 | | |
427 | | /* returns next string after \0 if before @end */ |
428 | | static inline char *ul_next_string(char *p, char *end) |
429 | 0 | { |
430 | 0 | char *last; |
431 | 0 |
|
432 | 0 | if (!p || !end || p >= end) |
433 | 0 | return NULL; |
434 | 0 |
|
435 | 0 | for (last = p; p < end; p++) { |
436 | 0 | if (*last == '\0' && p != last) |
437 | 0 | return p; |
438 | 0 | last = p; |
439 | 0 | } |
440 | 0 |
|
441 | 0 | return NULL; |
442 | 0 | } Unexecuted instantiation: gen_uuid.c:ul_next_string Unexecuted instantiation: probe.c:ul_next_string Unexecuted instantiation: partitions.c:ul_next_string Unexecuted instantiation: path.c:ul_next_string Unexecuted instantiation: sysfs.c:ul_next_string Unexecuted instantiation: devname.c:ul_next_string Unexecuted instantiation: devno.c:ul_next_string Unexecuted instantiation: buffer.c:ul_next_string Unexecuted instantiation: canonicalize.c:ul_next_string Unexecuted instantiation: mbsalign.c:ul_next_string Unexecuted instantiation: strv.c:ul_next_string Unexecuted instantiation: strutils.c:ul_next_string |
443 | | |
444 | | extern char *strnconcat(const char *s, const char *suffix, size_t b); |
445 | | extern char *strconcat(const char *s, const char *suffix); |
446 | | extern char *strfconcat(const char *s, const char *format, ...) |
447 | | __attribute__ ((__format__ (__printf__, 2, 3))); |
448 | | |
449 | | extern int strappend(char **a, const char *b); |
450 | | extern int strfappend(char **a, const char *format, ...) |
451 | | __attribute__ ((__format__ (__printf__, 2, 3))); |
452 | | extern int strvfappend(char **a, const char *format, va_list ap) |
453 | | __attribute__ ((__format__ (__printf__, 2, 0))); |
454 | | |
455 | | extern const char *split(const char **state, size_t *l, const char *separator, int quoted); |
456 | | |
457 | | extern char *ul_strchr_escaped(const char *s, int c); |
458 | | |
459 | | extern int skip_fline(FILE *fp); |
460 | | extern int ul_stralnumcmp(const char *p1, const char *p2); |
461 | | |
462 | | extern int ul_optstr_next(char **optstr, char **name, size_t *namesz, char **value, size_t *valsz); |
463 | | extern int ul_optstr_is_valid(const char *optstr); |
464 | | |
465 | | #endif |