/src/util-linux/lib/strutils.c
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 | | * Authors: Karel Zak <kzak@redhat.com> [2010] |
6 | | * Davidlohr Bueso <dave@gnu.org> [2010] |
7 | | */ |
8 | | #include <stdio.h> |
9 | | #include <stdlib.h> |
10 | | #include <inttypes.h> |
11 | | #include <ctype.h> |
12 | | #include <errno.h> |
13 | | #include <sys/stat.h> |
14 | | #include <string.h> |
15 | | #include <strings.h> |
16 | | #include <assert.h> |
17 | | |
18 | | #include "c.h" |
19 | | #include "nls.h" |
20 | | #include "strutils.h" |
21 | | #include "bitops.h" |
22 | | #include "pathnames.h" |
23 | | |
24 | | static int STRTOXX_EXIT_CODE = EXIT_FAILURE; |
25 | | |
26 | 0 | void strutils_set_exitcode(int ex) { |
27 | 0 | STRTOXX_EXIT_CODE = ex; |
28 | 0 | } |
29 | | |
30 | | static int do_scale_by_power (uintmax_t *x, int base, int power) |
31 | 0 | { |
32 | 0 | while (power--) { |
33 | 0 | if (UINTMAX_MAX / base < *x) |
34 | 0 | return -ERANGE; |
35 | 0 | *x *= base; |
36 | 0 | } |
37 | 0 | return 0; |
38 | 0 | } |
39 | | |
40 | | /* |
41 | | * strtosize() - convert string to size (uintmax_t). |
42 | | * |
43 | | * Supported suffixes: |
44 | | * |
45 | | * XiB or X for 2^N |
46 | | * where X = {K,M,G,T,P,E,Z,Y} |
47 | | * or X = {k,m,g,t,p,e} (undocumented for backward compatibility only) |
48 | | * for example: |
49 | | * 10KiB = 10240 |
50 | | * 10K = 10240 |
51 | | * |
52 | | * XB for 10^N |
53 | | * where X = {K,M,G,T,P,E,Z,Y} |
54 | | * for example: |
55 | | * 10KB = 10000 |
56 | | * |
57 | | * The optional 'power' variable returns number associated with used suffix |
58 | | * {K,M,G,T,P,E,Z,Y} = {1,2,3,4,5,6,7,8}. |
59 | | * |
60 | | * The function also supports decimal point, for example: |
61 | | * 0.5MB = 500000 |
62 | | * 0.5MiB = 512000 |
63 | | * |
64 | | * Note that the function does not accept numbers with '-' (negative sign) |
65 | | * prefix. |
66 | | */ |
67 | | int ul_parse_size(const char *str, uintmax_t *res, int *power) |
68 | 0 | { |
69 | 0 | const char *p; |
70 | 0 | char *end; |
71 | 0 | uintmax_t x, frac = 0; |
72 | 0 | int base = 1024, rc = 0, pwr = 0, frac_zeros = 0; |
73 | |
|
74 | 0 | static const char *suf = "KMGTPEZY"; |
75 | 0 | static const char *suf2 = "kmgtpezy"; |
76 | 0 | const char *sp; |
77 | |
|
78 | 0 | *res = 0; |
79 | |
|
80 | 0 | if (!str || !*str) { |
81 | 0 | rc = -EINVAL; |
82 | 0 | goto err; |
83 | 0 | } |
84 | | |
85 | | /* Only positive numbers are acceptable |
86 | | * |
87 | | * Note that this check is not perfect, it would be better to |
88 | | * use lconv->negative_sign. But coreutils use the same solution, |
89 | | * so it's probably good enough... |
90 | | */ |
91 | 0 | p = str; |
92 | 0 | while (isspace((unsigned char) *p)) |
93 | 0 | p++; |
94 | 0 | if (*p == '-') { |
95 | 0 | rc = -EINVAL; |
96 | 0 | goto err; |
97 | 0 | } |
98 | | |
99 | 0 | errno = 0, end = NULL; |
100 | 0 | x = strtoumax(str, &end, 0); |
101 | |
|
102 | 0 | if (end == str || |
103 | 0 | (errno != 0 && (x == UINTMAX_MAX || x == 0))) { |
104 | 0 | rc = errno ? -errno : -EINVAL; |
105 | 0 | goto err; |
106 | 0 | } |
107 | 0 | if (!end || !*end) |
108 | 0 | goto done; /* without suffix */ |
109 | 0 | p = end; |
110 | | |
111 | | /* |
112 | | * Check size suffixes |
113 | | */ |
114 | 0 | check_suffix: |
115 | 0 | if (*(p + 1) == 'i' && (*(p + 2) == 'B' || *(p + 2) == 'b') && !*(p + 3)) |
116 | 0 | base = 1024; /* XiB, 2^N */ |
117 | 0 | else if ((*(p + 1) == 'B' || *(p + 1) == 'b') && !*(p + 2)) |
118 | 0 | base = 1000; /* XB, 10^N */ |
119 | 0 | else if (*(p + 1)) { |
120 | 0 | struct lconv const *l = localeconv(); |
121 | 0 | const char *dp = l ? l->decimal_point : NULL; |
122 | 0 | size_t dpsz = dp ? strlen(dp) : 0; |
123 | |
|
124 | 0 | if (frac == 0 && *p && dp && strncmp(dp, p, dpsz) == 0) { |
125 | 0 | const char *fstr = p + dpsz; |
126 | |
|
127 | 0 | for (p = fstr; *p == '0'; p++) |
128 | 0 | frac_zeros++; |
129 | 0 | fstr = p; |
130 | 0 | if (isdigit(*fstr)) { |
131 | 0 | errno = 0, end = NULL; |
132 | 0 | frac = strtoumax(fstr, &end, 0); |
133 | 0 | if (end == fstr || |
134 | 0 | (errno != 0 && (frac == UINTMAX_MAX || frac == 0))) { |
135 | 0 | rc = errno ? -errno : -EINVAL; |
136 | 0 | goto err; |
137 | 0 | } |
138 | 0 | } else |
139 | 0 | end = (char *) p; |
140 | | |
141 | 0 | if (frac && (!end || !*end)) { |
142 | 0 | rc = -EINVAL; |
143 | 0 | goto err; /* without suffix, but with frac */ |
144 | 0 | } |
145 | 0 | p = end; |
146 | 0 | goto check_suffix; |
147 | 0 | } |
148 | 0 | rc = -EINVAL; |
149 | 0 | goto err; /* unexpected suffix */ |
150 | 0 | } |
151 | | |
152 | 0 | sp = strchr(suf, *p); |
153 | 0 | if (sp) |
154 | 0 | pwr = (sp - suf) + 1; |
155 | 0 | else { |
156 | 0 | sp = strchr(suf2, *p); |
157 | 0 | if (sp) |
158 | 0 | pwr = (sp - suf2) + 1; |
159 | 0 | else { |
160 | 0 | rc = -EINVAL; |
161 | 0 | goto err; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | 0 | rc = do_scale_by_power(&x, base, pwr); |
166 | 0 | if (power) |
167 | 0 | *power = pwr; |
168 | 0 | if (frac && pwr) { |
169 | 0 | int i; |
170 | 0 | uintmax_t frac_div = 10, frac_poz = 1, frac_base = 1; |
171 | | |
172 | | /* mega, giga, ... */ |
173 | 0 | do_scale_by_power(&frac_base, base, pwr); |
174 | | |
175 | | /* maximal divisor for last digit (e.g. for 0.05 is |
176 | | * frac_div=100, for 0.054 is frac_div=1000, etc.) |
177 | | * |
178 | | * Reduce frac if too large. |
179 | | */ |
180 | 0 | while (frac_div < frac) { |
181 | 0 | if (frac_div <= UINTMAX_MAX/10) |
182 | 0 | frac_div *= 10; |
183 | 0 | else |
184 | 0 | frac /= 10; |
185 | 0 | } |
186 | | |
187 | | /* 'frac' is without zeros (5 means 0.5 as well as 0.05) */ |
188 | 0 | for (i = 0; i < frac_zeros; i++) { |
189 | 0 | if (frac_div <= UINTMAX_MAX/10) |
190 | 0 | frac_div *= 10; |
191 | 0 | else |
192 | 0 | frac /= 10; |
193 | 0 | } |
194 | | |
195 | | /* |
196 | | * Go backwardly from last digit and add to result what the |
197 | | * digit represents in the frac_base. For example 0.25G |
198 | | * |
199 | | * 5 means 1GiB / (100/5) |
200 | | * 2 means 1GiB / (10/2) |
201 | | */ |
202 | 0 | do { |
203 | 0 | unsigned int seg = frac % 10; /* last digit of the frac */ |
204 | 0 | uintmax_t seg_div = frac_div / frac_poz; /* what represents the segment 1000, 100, .. */ |
205 | |
|
206 | 0 | frac /= 10; /* remove last digit from frac */ |
207 | 0 | frac_poz *= 10; |
208 | |
|
209 | 0 | if (seg && seg_div / seg) |
210 | 0 | x += frac_base / (seg_div / seg); |
211 | 0 | } while (frac); |
212 | 0 | } |
213 | 0 | done: |
214 | 0 | *res = x; |
215 | 0 | err: |
216 | 0 | if (rc < 0) |
217 | 0 | errno = -rc; |
218 | 0 | return rc; |
219 | 0 | } |
220 | | |
221 | | int strtosize(const char *str, uintmax_t *res) |
222 | 0 | { |
223 | 0 | return ul_parse_size(str, res, NULL); |
224 | 0 | } |
225 | | |
226 | | int isdigit_strend(const char *str, const char **end) |
227 | 0 | { |
228 | 0 | const char *p; |
229 | |
|
230 | 0 | for (p = str; p && *p && isdigit((unsigned char) *p); p++); |
231 | |
|
232 | 0 | if (end) |
233 | 0 | *end = p; |
234 | 0 | return p && p > str && !*p; |
235 | 0 | } |
236 | | |
237 | | int isxdigit_strend(const char *str, const char **end) |
238 | 0 | { |
239 | 0 | const char *p; |
240 | |
|
241 | 0 | for (p = str; p && *p && isxdigit((unsigned char) *p); p++); |
242 | |
|
243 | 0 | if (end) |
244 | 0 | *end = p; |
245 | |
|
246 | 0 | return p && p > str && !*p; |
247 | 0 | } |
248 | | |
249 | | /* |
250 | | * For example: ul_parse_switch(argv[i], "on", "off", "yes", "no", NULL); |
251 | | */ |
252 | | int ul_parse_switch(const char *arg, ...) |
253 | 0 | { |
254 | 0 | const char *a, *b; |
255 | 0 | va_list ap; |
256 | |
|
257 | 0 | va_start(ap, arg); |
258 | 0 | do { |
259 | 0 | a = va_arg(ap, char *); |
260 | 0 | if (!a) |
261 | 0 | break; |
262 | 0 | b = va_arg(ap, char *); |
263 | 0 | if (!b) |
264 | 0 | break; |
265 | | |
266 | 0 | if (strcmp(arg, a) == 0) { |
267 | 0 | va_end(ap); |
268 | 0 | return 1; |
269 | 0 | } |
270 | | |
271 | 0 | if (strcmp(arg, b) == 0) { |
272 | 0 | va_end(ap); |
273 | 0 | return 0; |
274 | 0 | } |
275 | 0 | } while (1); |
276 | 0 | va_end(ap); |
277 | |
|
278 | 0 | errx(STRTOXX_EXIT_CODE, _("unsupported argument: %s"), arg); |
279 | 0 | } |
280 | | |
281 | | #ifndef HAVE_MEMPCPY |
282 | | void *mempcpy(void *restrict dest, const void *restrict src, size_t n) |
283 | | { |
284 | | return ((char *)memcpy(dest, src, n)) + n; |
285 | | } |
286 | | #endif |
287 | | |
288 | | #ifndef HAVE_STRNLEN |
289 | | size_t strnlen(const char *s, size_t maxlen) |
290 | | { |
291 | | size_t i; |
292 | | |
293 | | for (i = 0; i < maxlen; i++) { |
294 | | if (s[i] == '\0') |
295 | | return i; |
296 | | } |
297 | | return maxlen; |
298 | | } |
299 | | #endif |
300 | | |
301 | | #ifndef HAVE_STRNCHR |
302 | | char *strnchr(const char *s, size_t maxlen, int c) |
303 | 0 | { |
304 | 0 | for (; maxlen-- && *s != '\0'; ++s) |
305 | 0 | if (*s == (char)c) |
306 | 0 | return (char *)s; |
307 | 0 | return NULL; |
308 | 0 | } |
309 | | #endif |
310 | | |
311 | | #ifndef HAVE_STRNDUP |
312 | | char *strndup(const char *s, size_t n) |
313 | | { |
314 | | size_t len = strnlen(s, n); |
315 | | char *new = malloc((len + 1) * sizeof(char)); |
316 | | if (!new) |
317 | | return NULL; |
318 | | new[len] = '\0'; |
319 | | return (char *) memcpy(new, s, len); |
320 | | } |
321 | | #endif |
322 | | |
323 | | /* |
324 | | * convert strings to numbers; returns <0 on error, and 0 on success |
325 | | */ |
326 | | int ul_strtos64(const char *str, int64_t *num, int base) |
327 | 0 | { |
328 | 0 | char *end = NULL; |
329 | |
|
330 | 0 | if (str == NULL || *str == '\0') |
331 | 0 | return -(errno = EINVAL); |
332 | | |
333 | 0 | errno = 0; |
334 | 0 | *num = (int64_t) strtoimax(str, &end, base); |
335 | |
|
336 | 0 | if (errno != 0) |
337 | 0 | return -errno; |
338 | 0 | if (str == end || (end && *end)) |
339 | 0 | return -(errno = EINVAL); |
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | | int ul_strtou64(const char *str, uint64_t *num, int base) |
344 | 0 | { |
345 | 0 | char *end = NULL; |
346 | 0 | int64_t tmp; |
347 | |
|
348 | 0 | if (str == NULL || *str == '\0') |
349 | 0 | return -(errno = EINVAL); |
350 | | |
351 | | /* we need to ignore negative numbers, note that for invalid negative |
352 | | * number strtoimax() returns negative number too, so we do not |
353 | | * need to check errno here */ |
354 | 0 | errno = 0; |
355 | 0 | tmp = (int64_t) strtoimax(str, &end, base); |
356 | 0 | if (tmp < 0) |
357 | 0 | errno = ERANGE; |
358 | 0 | else { |
359 | 0 | errno = 0; |
360 | 0 | *num = strtoumax(str, &end, base); |
361 | 0 | } |
362 | |
|
363 | 0 | if (errno != 0) |
364 | 0 | return -errno; |
365 | 0 | if (str == end || (end && *end)) |
366 | 0 | return -(errno = EINVAL); |
367 | 0 | return 0; |
368 | 0 | } |
369 | | |
370 | | int ul_strtos32(const char *str, int32_t *num, int base) |
371 | 0 | { |
372 | 0 | int64_t tmp; |
373 | 0 | int rc; |
374 | |
|
375 | 0 | rc = ul_strtos64(str, &tmp, base); |
376 | 0 | if (rc == 0 && (tmp < INT32_MIN || tmp > INT32_MAX)) |
377 | 0 | rc = -(errno = ERANGE); |
378 | 0 | if (rc == 0) |
379 | 0 | *num = (int32_t) tmp; |
380 | 0 | return rc; |
381 | 0 | } |
382 | | |
383 | | int ul_strtou32(const char *str, uint32_t *num, int base) |
384 | 0 | { |
385 | 0 | uint64_t tmp; |
386 | 0 | int rc; |
387 | |
|
388 | 0 | rc = ul_strtou64(str, &tmp, base); |
389 | 0 | if (rc == 0 && tmp > UINT32_MAX) |
390 | 0 | rc = -(errno = ERANGE); |
391 | 0 | if (rc == 0) |
392 | 0 | *num = (uint32_t) tmp; |
393 | 0 | return rc; |
394 | 0 | } |
395 | | |
396 | | /* |
397 | | * Convert strings to numbers in defined range and print message on error. |
398 | | * |
399 | | * These functions are used when we read input from users (getopt() etc.). It's |
400 | | * better to consolidate the code and keep it all based on 64-bit numbers than |
401 | | * implement it for 32 and 16-bit numbers too. |
402 | | */ |
403 | | int64_t str2num_or_err(const char *str, int base, const char *errmesg, |
404 | | int64_t low, int64_t up) |
405 | 0 | { |
406 | 0 | int64_t num = 0; |
407 | 0 | int rc; |
408 | |
|
409 | 0 | rc = ul_strtos64(str, &num, base); |
410 | 0 | if (rc == 0 && ((low && num < low) || (up && num > up))) |
411 | 0 | rc = -(errno = ERANGE); |
412 | |
|
413 | 0 | if (rc) { |
414 | 0 | if (errno == ERANGE) |
415 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
416 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
417 | 0 | } |
418 | 0 | return num; |
419 | 0 | } |
420 | | |
421 | | uint64_t str2unum_or_err(const char *str, int base, const char *errmesg, uint64_t up) |
422 | 0 | { |
423 | 0 | uint64_t num = 0; |
424 | 0 | int rc; |
425 | |
|
426 | 0 | rc = ul_strtou64(str, &num, base); |
427 | 0 | if (rc == 0 && (up && num > up)) |
428 | 0 | rc = -(errno = ERANGE); |
429 | |
|
430 | 0 | if (rc) { |
431 | 0 | if (errno == ERANGE) |
432 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
433 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
434 | 0 | } |
435 | 0 | return num; |
436 | 0 | } |
437 | | |
438 | | double strtod_or_err(const char *str, const char *errmesg) |
439 | 0 | { |
440 | 0 | double num; |
441 | 0 | char *end = NULL; |
442 | |
|
443 | 0 | errno = 0; |
444 | 0 | if (str == NULL || *str == '\0') |
445 | 0 | goto err; |
446 | 0 | num = strtod(str, &end); |
447 | |
|
448 | 0 | if (errno || str == end || (end && *end)) |
449 | 0 | goto err; |
450 | | |
451 | 0 | return num; |
452 | 0 | err: |
453 | 0 | if (errno == ERANGE) |
454 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
455 | | |
456 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
457 | 0 | } |
458 | | |
459 | | int ul_strtold(const char *str, long double *num) |
460 | 0 | { |
461 | 0 | char *end = NULL; |
462 | |
|
463 | 0 | errno = 0; |
464 | 0 | if (str == NULL || *str == '\0') |
465 | 0 | return -(errno = EINVAL); |
466 | 0 | *num = strtold(str, &end); |
467 | |
|
468 | 0 | if (errno != 0) |
469 | 0 | return -errno; |
470 | 0 | if (str == end || (end && *end)) |
471 | 0 | return -(errno = EINVAL); |
472 | 0 | return 0; |
473 | 0 | } |
474 | | |
475 | | long double strtold_or_err(const char *str, const char *errmesg) |
476 | 0 | { |
477 | 0 | long double num = 0; |
478 | |
|
479 | 0 | if (ul_strtold(str, &num) == 0) |
480 | 0 | return num; |
481 | 0 | if (errno == ERANGE) |
482 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
483 | | |
484 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
485 | 0 | } |
486 | | |
487 | | uintmax_t strtosize_or_err(const char *str, const char *errmesg) |
488 | 0 | { |
489 | 0 | uintmax_t num; |
490 | |
|
491 | 0 | if (strtosize(str, &num) == 0) |
492 | 0 | return num; |
493 | | |
494 | 0 | if (errno) |
495 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
496 | | |
497 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
498 | 0 | } |
499 | | |
500 | | |
501 | | void strtotimeval_or_err(const char *str, struct timeval *tv, const char *errmesg) |
502 | 0 | { |
503 | 0 | long double user_input; |
504 | |
|
505 | 0 | user_input = strtold_or_err(str, errmesg); |
506 | 0 | tv->tv_sec = (time_t) user_input; |
507 | 0 | tv->tv_usec = (suseconds_t)((user_input - tv->tv_sec) * 1000000); |
508 | 0 | } |
509 | | |
510 | | void strtotimespec_or_err(const char *str, struct timespec *ts, const char *errmesg) |
511 | 0 | { |
512 | 0 | long double user_input; |
513 | |
|
514 | 0 | user_input = strtold_or_err(str, errmesg); |
515 | 0 | ts->tv_sec = (time_t) user_input; |
516 | 0 | ts->tv_nsec = (long)((user_input - ts->tv_sec) * 1000000000); |
517 | 0 | } |
518 | | |
519 | | time_t strtotime_or_err(const char *str, const char *errmesg) |
520 | 0 | { |
521 | 0 | int64_t user_input; |
522 | |
|
523 | 0 | user_input = strtos64_or_err(str, errmesg); |
524 | 0 | return (time_t) user_input; |
525 | 0 | } |
526 | | |
527 | | bool hyperlinkwanted(const char *mode) |
528 | 0 | { |
529 | 0 | if (mode && strcmp(mode, "never") == 0) |
530 | 0 | return false; |
531 | | |
532 | 0 | if (mode && strcmp(mode, "always") == 0) |
533 | 0 | return true; |
534 | | |
535 | 0 | if (!mode || strcmp(mode, "auto") == 0) |
536 | 0 | return isatty(STDOUT_FILENO) ? true : false; |
537 | | |
538 | 0 | errx(EXIT_FAILURE, _("invalid argument of --hyperlink: %s"), mode); |
539 | 0 | } |
540 | | |
541 | | /* |
542 | | * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must |
543 | | * be 11 bytes. |
544 | | */ |
545 | | char *xstrmode(mode_t mode, char *str) |
546 | 0 | { |
547 | 0 | unsigned short i = 0; |
548 | |
|
549 | 0 | if (S_ISDIR(mode)) |
550 | 0 | str[i++] = 'd'; |
551 | 0 | else if (S_ISLNK(mode)) |
552 | 0 | str[i++] = 'l'; |
553 | 0 | else if (S_ISCHR(mode)) |
554 | 0 | str[i++] = 'c'; |
555 | 0 | else if (S_ISBLK(mode)) |
556 | 0 | str[i++] = 'b'; |
557 | 0 | else if (S_ISSOCK(mode)) |
558 | 0 | str[i++] = 's'; |
559 | 0 | else if (S_ISFIFO(mode)) |
560 | 0 | str[i++] = 'p'; |
561 | 0 | else if (S_ISREG(mode)) |
562 | 0 | str[i++] = '-'; |
563 | |
|
564 | 0 | str[i++] = mode & S_IRUSR ? 'r' : '-'; |
565 | 0 | str[i++] = mode & S_IWUSR ? 'w' : '-'; |
566 | 0 | str[i++] = (mode & S_ISUID |
567 | 0 | ? (mode & S_IXUSR ? 's' : 'S') |
568 | 0 | : (mode & S_IXUSR ? 'x' : '-')); |
569 | 0 | str[i++] = mode & S_IRGRP ? 'r' : '-'; |
570 | 0 | str[i++] = mode & S_IWGRP ? 'w' : '-'; |
571 | 0 | str[i++] = (mode & S_ISGID |
572 | 0 | ? (mode & S_IXGRP ? 's' : 'S') |
573 | 0 | : (mode & S_IXGRP ? 'x' : '-')); |
574 | 0 | str[i++] = mode & S_IROTH ? 'r' : '-'; |
575 | 0 | str[i++] = mode & S_IWOTH ? 'w' : '-'; |
576 | 0 | str[i++] = (mode & S_ISVTX |
577 | 0 | ? (mode & S_IXOTH ? 't' : 'T') |
578 | 0 | : (mode & S_IXOTH ? 'x' : '-')); |
579 | 0 | str[i] = '\0'; |
580 | |
|
581 | 0 | return str; |
582 | 0 | } |
583 | | |
584 | | /* |
585 | | * returns exponent (2^x=n) in range KiB..EiB (2^10..2^60) |
586 | | */ |
587 | | static int get_exp(uint64_t n) |
588 | 0 | { |
589 | 0 | int shft; |
590 | |
|
591 | 0 | for (shft = 10; shft <= 60; shft += 10) { |
592 | 0 | if (n < (1ULL << shft)) |
593 | 0 | break; |
594 | 0 | } |
595 | 0 | return shft - 10; |
596 | 0 | } |
597 | | |
598 | | char *size_to_human_string(int options, uint64_t bytes) |
599 | 0 | { |
600 | 0 | char buf[32]; |
601 | 0 | int dec, exp; |
602 | 0 | uint64_t frac; |
603 | 0 | const char *letters = "BKMGTPE"; |
604 | 0 | char suffix[sizeof(" KiB")], *psuf = suffix; |
605 | 0 | char c; |
606 | |
|
607 | 0 | if (options & SIZE_SUFFIX_SPACE) |
608 | 0 | *psuf++ = ' '; |
609 | | |
610 | |
|
611 | 0 | exp = get_exp(bytes); |
612 | 0 | c = *(letters + (exp ? exp / 10 : 0)); |
613 | 0 | dec = exp ? bytes / (1ULL << exp) : bytes; |
614 | 0 | frac = exp ? bytes % (1ULL << exp) : 0; |
615 | |
|
616 | 0 | *psuf++ = c; |
617 | |
|
618 | 0 | if ((options & SIZE_SUFFIX_3LETTER) && (c != 'B')) { |
619 | 0 | *psuf++ = 'i'; |
620 | 0 | *psuf++ = 'B'; |
621 | 0 | } |
622 | |
|
623 | 0 | *psuf = '\0'; |
624 | | |
625 | | /* fprintf(stderr, "exp: %d, unit: %c, dec: %d, frac: %jd\n", |
626 | | * exp, suffix[0], dec, frac); |
627 | | */ |
628 | | |
629 | | /* round */ |
630 | 0 | if (frac) { |
631 | | /* get 3 digits after decimal point */ |
632 | 0 | if (frac >= UINT64_MAX / 1000) |
633 | 0 | frac = ((frac / 1024) * 1000) / (1ULL << (exp - 10)) ; |
634 | 0 | else |
635 | 0 | frac = (frac * 1000) / (1ULL << (exp)) ; |
636 | |
|
637 | 0 | if (options & SIZE_DECIMAL_2DIGITS) { |
638 | | /* round 4/5 and keep 2 digits after decimal point */ |
639 | 0 | frac = (frac + 5) / 10 ; |
640 | 0 | } else { |
641 | | /* round 4/5 and keep 1 digit after decimal point */ |
642 | 0 | frac = ((frac + 50) / 100) * 10 ; |
643 | 0 | } |
644 | | |
645 | | /* rounding could have overflowed */ |
646 | 0 | if (frac == 100) { |
647 | 0 | dec++; |
648 | 0 | frac = 0; |
649 | 0 | } |
650 | 0 | } |
651 | |
|
652 | 0 | if (frac) { |
653 | 0 | struct lconv const *l = localeconv(); |
654 | 0 | char *dp = l ? l->decimal_point : NULL; |
655 | 0 | int len; |
656 | |
|
657 | 0 | if (!dp || !*dp) |
658 | 0 | dp = "."; |
659 | |
|
660 | 0 | len = snprintf(buf, sizeof(buf), "%d%s%02" PRIu64, dec, dp, frac); |
661 | 0 | if (len > 0 && (size_t) len < sizeof(buf)) { |
662 | | /* remove potential extraneous zero */ |
663 | 0 | if (buf[len - 1] == '0') |
664 | 0 | buf[len--] = '\0'; |
665 | | /* append suffix */ |
666 | 0 | xstrncpy(buf+len, suffix, sizeof(buf) - len); |
667 | 0 | } else |
668 | 0 | *buf = '\0'; /* snprintf error */ |
669 | 0 | } else |
670 | 0 | snprintf(buf, sizeof(buf), "%d%s", dec, suffix); |
671 | |
|
672 | 0 | return strdup(buf); |
673 | 0 | } |
674 | | |
675 | | /* |
676 | | * Parses comma delimited list to array with IDs, for example: |
677 | | * |
678 | | * "aaa,bbb,ccc" --> ary[0] = FOO_AAA; |
679 | | * ary[1] = FOO_BBB; |
680 | | * ary[3] = FOO_CCC; |
681 | | * |
682 | | * The function name2id() provides conversion from string to ID. |
683 | | * |
684 | | * Returns: >= 0 : number of items added to ary[] |
685 | | * -1 : parse error or unknown item |
686 | | * -2 : arysz reached |
687 | | */ |
688 | | int string_to_idarray(const char *list, int ary[], size_t arysz, |
689 | | int (name2id)(const char *, size_t)) |
690 | 0 | { |
691 | 0 | const char *begin = NULL, *p; |
692 | 0 | size_t n = 0; |
693 | |
|
694 | 0 | if (!list || !*list || !ary || !arysz || !name2id) |
695 | 0 | return -1; |
696 | | |
697 | 0 | for (p = list; p && *p; p++) { |
698 | 0 | const char *end = NULL; |
699 | 0 | int id; |
700 | |
|
701 | 0 | if (n >= arysz) |
702 | 0 | return -2; |
703 | 0 | if (!begin) |
704 | 0 | begin = p; /* begin of the column name */ |
705 | 0 | if (*p == ',') |
706 | 0 | end = p; /* terminate the name */ |
707 | 0 | if (*(p + 1) == '\0') |
708 | 0 | end = p + 1; /* end of string */ |
709 | 0 | if (!begin || !end) |
710 | 0 | continue; |
711 | 0 | if (end <= begin) |
712 | 0 | return -1; |
713 | | |
714 | 0 | id = name2id(begin, end - begin); |
715 | 0 | if (id == -1) |
716 | 0 | return -1; |
717 | 0 | ary[ n++ ] = id; |
718 | 0 | begin = NULL; |
719 | 0 | if (end && !*end) |
720 | 0 | break; |
721 | 0 | } |
722 | 0 | return n; |
723 | 0 | } |
724 | | |
725 | | /* |
726 | | * Parses the array like string_to_idarray but if format is "+aaa,bbb" |
727 | | * it adds fields to array instead of replacing them. |
728 | | */ |
729 | | int string_add_to_idarray(const char *list, int ary[], size_t arysz, |
730 | | size_t *ary_pos, int (name2id)(const char *, size_t)) |
731 | 0 | { |
732 | 0 | const char *list_add; |
733 | 0 | int r; |
734 | |
|
735 | 0 | if (!list || !*list || !ary_pos || *ary_pos > arysz) |
736 | 0 | return -1; |
737 | | |
738 | 0 | if (list[0] == '+') |
739 | 0 | list_add = &list[1]; |
740 | 0 | else { |
741 | 0 | list_add = list; |
742 | 0 | *ary_pos = 0; |
743 | 0 | } |
744 | |
|
745 | 0 | r = string_to_idarray(list_add, &ary[*ary_pos], arysz - *ary_pos, name2id); |
746 | 0 | if (r > 0) |
747 | 0 | *ary_pos += r; |
748 | 0 | return r; |
749 | 0 | } |
750 | | |
751 | | /* |
752 | | * LIST ::= <item> [, <item>] |
753 | | * |
754 | | * The <item> is translated to 'id' by name2id() function and the 'id' is used |
755 | | * as a position in the 'ary' bit array. It means that the 'id' has to be in |
756 | | * range <0..N> where N < sizeof(ary) * NBBY. |
757 | | * |
758 | | * If allow_range is enabled: |
759 | | * An item ending in '+' also sets all bits in <0..N>. |
760 | | * An item beginning with '+' also sets all bits in <N..allow_minus>. |
761 | | * |
762 | | * Returns: 0 on success, <0 on error. |
763 | | */ |
764 | | int string_to_bitarray(const char *list, |
765 | | char *ary, |
766 | | int (*name2bit)(const char *, size_t), |
767 | | size_t allow_range) |
768 | 0 | { |
769 | 0 | const char *begin = NULL, *p; |
770 | |
|
771 | 0 | if (!list || !name2bit || !ary) |
772 | 0 | return -EINVAL; |
773 | | |
774 | 0 | for (p = list; p && *p; p++) { |
775 | 0 | const char *end = NULL; |
776 | 0 | int bit, set_lower = 0, set_higher = 0; |
777 | |
|
778 | 0 | if (!begin) |
779 | 0 | begin = p; /* begin of the level name */ |
780 | 0 | if (*p == ',') |
781 | 0 | end = p; /* terminate the name */ |
782 | 0 | if (*(p + 1) == '\0') |
783 | 0 | end = p + 1; /* end of string */ |
784 | 0 | if (!begin || !end) |
785 | 0 | continue; |
786 | 0 | if (end <= begin) |
787 | 0 | return -1; |
788 | 0 | if (allow_range) { |
789 | 0 | if (*(end - 1) == '+') { |
790 | 0 | end--; |
791 | 0 | set_lower = 1; |
792 | 0 | } else if (*begin == '+') { |
793 | 0 | begin++; |
794 | 0 | set_higher = 1; |
795 | 0 | } |
796 | 0 | } |
797 | |
|
798 | 0 | bit = name2bit(begin, end - begin); |
799 | 0 | if (bit < 0) |
800 | 0 | return bit; |
801 | 0 | setbit(ary, bit); |
802 | 0 | if (set_lower) |
803 | 0 | while (--bit >= 0) |
804 | 0 | setbit(ary, bit); |
805 | 0 | else if (set_higher) |
806 | 0 | while (++bit < (int) allow_range) |
807 | 0 | setbit(ary, bit); |
808 | 0 | begin = NULL; |
809 | 0 | if (end && !*end) |
810 | 0 | break; |
811 | 0 | } |
812 | 0 | return 0; |
813 | 0 | } |
814 | | |
815 | | /* |
816 | | * LIST ::= <item> [, <item>] |
817 | | * |
818 | | * The <item> is translated to 'id' by name2flag() function and the flags is |
819 | | * set to the 'mask' |
820 | | * |
821 | | * Returns: 0 on success, <0 on error. |
822 | | */ |
823 | | int string_to_bitmask(const char *list, |
824 | | unsigned long *mask, |
825 | | long (*name2flag)(const char *, size_t)) |
826 | 0 | { |
827 | 0 | const char *begin = NULL, *p; |
828 | |
|
829 | 0 | if (!list || !name2flag || !mask) |
830 | 0 | return -EINVAL; |
831 | | |
832 | 0 | for (p = list; p && *p; p++) { |
833 | 0 | const char *end = NULL; |
834 | 0 | long flag; |
835 | |
|
836 | 0 | if (!begin) |
837 | 0 | begin = p; /* begin of the level name */ |
838 | 0 | if (*p == ',') |
839 | 0 | end = p; /* terminate the name */ |
840 | 0 | if (*(p + 1) == '\0') |
841 | 0 | end = p + 1; /* end of string */ |
842 | 0 | if (!begin || !end) |
843 | 0 | continue; |
844 | 0 | if (end <= begin) |
845 | 0 | return -1; |
846 | | |
847 | 0 | flag = name2flag(begin, end - begin); |
848 | 0 | if (flag < 0) |
849 | 0 | return flag; /* error */ |
850 | 0 | *mask |= flag; |
851 | 0 | begin = NULL; |
852 | 0 | if (end && !*end) |
853 | 0 | break; |
854 | 0 | } |
855 | 0 | return 0; |
856 | 0 | } |
857 | | |
858 | | /* |
859 | | * Parse the lower and higher values in a string containing |
860 | | * "lower:higher" or "lower-higher" format. Note that either |
861 | | * the lower or the higher values may be missing, and the def |
862 | | * value will be assigned to it by default. |
863 | | * |
864 | | * Returns: 0 on success, <0 on error. |
865 | | */ |
866 | | int ul_parse_range(const char *str, int *lower, int *upper, int def) |
867 | 0 | { |
868 | 0 | char *end = NULL; |
869 | |
|
870 | 0 | if (!str) |
871 | 0 | return 0; |
872 | | |
873 | 0 | *upper = *lower = def; |
874 | 0 | errno = 0; |
875 | |
|
876 | 0 | if (*str == ':') { /* <:N> */ |
877 | 0 | str++; |
878 | 0 | *upper = strtol(str, &end, 10); |
879 | 0 | if (errno || !end || *end || end == str) |
880 | 0 | return -1; |
881 | 0 | } else { |
882 | 0 | *upper = *lower = strtol(str, &end, 10); |
883 | 0 | if (errno || !end || end == str) |
884 | 0 | return -1; |
885 | | |
886 | 0 | if (*end == ':' && !*(end + 1)) /* <M:> */ |
887 | 0 | *upper = def; |
888 | 0 | else if (*end == '-' || *end == ':') { /* <M:N> <M-N> */ |
889 | 0 | str = end + 1; |
890 | 0 | end = NULL; |
891 | 0 | errno = 0; |
892 | 0 | *upper = strtol(str, &end, 10); |
893 | |
|
894 | 0 | if (errno || !end || *end || end == str) |
895 | 0 | return -1; |
896 | 0 | } |
897 | 0 | } |
898 | 0 | return 0; |
899 | 0 | } |
900 | | |
901 | | static const char *next_path_segment(const char *str, size_t *sz) |
902 | 0 | { |
903 | 0 | const char *start, *p; |
904 | |
|
905 | 0 | start = str; |
906 | 0 | *sz = 0; |
907 | 0 | while (start && *start == '/' && *(start + 1) == '/') |
908 | 0 | start++; |
909 | |
|
910 | 0 | if (!start || !*start) |
911 | 0 | return NULL; |
912 | | |
913 | 0 | for (*sz = 1, p = start + 1; *p && *p != '/'; p++) { |
914 | 0 | (*sz)++; |
915 | 0 | } |
916 | |
|
917 | 0 | return start; |
918 | 0 | } |
919 | | |
920 | | int streq_paths(const char *a, const char *b) |
921 | 0 | { |
922 | 0 | while (a && b) { |
923 | 0 | size_t a_sz, b_sz; |
924 | 0 | const char *a_seg = next_path_segment(a, &a_sz); |
925 | 0 | const char *b_seg = next_path_segment(b, &b_sz); |
926 | | |
927 | | /* |
928 | | fprintf(stderr, "A>>>(%zu) '%s'\n", a_sz, a_seg); |
929 | | fprintf(stderr, "B>>>(%zu) '%s'\n", b_sz, b_seg); |
930 | | */ |
931 | | |
932 | | /* end of the path */ |
933 | 0 | if (a_sz + b_sz == 0) |
934 | 0 | return 1; |
935 | | |
936 | | /* ignore trailing slash */ |
937 | 0 | if (a_sz + b_sz == 1 && |
938 | 0 | ((a_seg && *a_seg == '/') || (b_seg && *b_seg == '/'))) |
939 | 0 | return 1; |
940 | | |
941 | 0 | if (!a_seg || !b_seg) |
942 | 0 | break; |
943 | 0 | if (a_sz != b_sz || strncmp(a_seg, b_seg, a_sz) != 0) |
944 | 0 | break; |
945 | | |
946 | 0 | a = a_seg + a_sz; |
947 | 0 | b = b_seg + b_sz; |
948 | 0 | }; |
949 | |
|
950 | 0 | return 0; |
951 | 0 | } |
952 | | |
953 | | /* concatenate two strings to a new string, the size of the second string is limited by @b */ |
954 | | char *strnconcat(const char *s, const char *suffix, size_t b) |
955 | 0 | { |
956 | 0 | size_t a; |
957 | 0 | char *r; |
958 | |
|
959 | 0 | if (!s && !suffix) |
960 | 0 | return strdup(""); |
961 | 0 | if (!s) |
962 | 0 | return strndup(suffix, b); |
963 | 0 | if (!suffix) |
964 | 0 | return strdup(s); |
965 | | |
966 | 0 | assert(s); |
967 | 0 | assert(suffix); |
968 | | |
969 | 0 | a = strlen(s); |
970 | 0 | if (b > ((size_t) -1) - a) |
971 | 0 | return NULL; |
972 | | |
973 | 0 | r = malloc(a + b + 1); |
974 | 0 | if (!r) |
975 | 0 | return NULL; |
976 | | |
977 | 0 | memcpy(r, s, a); |
978 | 0 | memcpy(r + a, suffix, b); |
979 | 0 | r[a+b] = 0; |
980 | |
|
981 | 0 | return r; |
982 | 0 | } |
983 | | |
984 | | /* concatenate two strings to a new string */ |
985 | | char *strconcat(const char *s, const char *suffix) |
986 | 0 | { |
987 | 0 | return strnconcat(s, suffix, suffix ? strlen(suffix) : 0); |
988 | 0 | } |
989 | | |
990 | | /* concatenate @s and string defined by @format to a new string */ |
991 | | char *strfconcat(const char *s, const char *format, ...) |
992 | 0 | { |
993 | 0 | va_list ap; |
994 | 0 | char *val, *res; |
995 | 0 | int sz; |
996 | |
|
997 | 0 | va_start(ap, format); |
998 | 0 | sz = vasprintf(&val, format, ap); |
999 | 0 | va_end(ap); |
1000 | |
|
1001 | 0 | if (sz < 0) |
1002 | 0 | return NULL; |
1003 | | |
1004 | 0 | res = strnconcat(s, val, sz); |
1005 | 0 | free(val); |
1006 | 0 | return res; |
1007 | 0 | } |
1008 | | |
1009 | | int strappend(char **a, const char *b) |
1010 | 0 | { |
1011 | 0 | size_t al, bl; |
1012 | 0 | char *tmp; |
1013 | |
|
1014 | 0 | if (!a) |
1015 | 0 | return -EINVAL; |
1016 | 0 | if (!b || !*b) |
1017 | 0 | return 0; |
1018 | 0 | if (!*a) { |
1019 | 0 | *a = strdup(b); |
1020 | 0 | return !*a ? -ENOMEM : 0; |
1021 | 0 | } |
1022 | | |
1023 | 0 | al = strlen(*a); |
1024 | 0 | bl = strlen(b); |
1025 | |
|
1026 | 0 | tmp = realloc(*a, al + bl + 1); |
1027 | 0 | if (!tmp) |
1028 | 0 | return -ENOMEM; |
1029 | 0 | *a = tmp; |
1030 | 0 | memcpy((*a) + al, b, bl + 1); |
1031 | 0 | return 0; |
1032 | 0 | } |
1033 | | |
1034 | | /* the hybrid version of strfconcat and strappend. */ |
1035 | | int strfappend(char **a, const char *format, ...) |
1036 | 0 | { |
1037 | 0 | va_list ap; |
1038 | 0 | int res; |
1039 | |
|
1040 | 0 | va_start(ap, format); |
1041 | 0 | res = strvfappend(a, format, ap); |
1042 | 0 | va_end(ap); |
1043 | |
|
1044 | 0 | return res; |
1045 | 0 | } |
1046 | | |
1047 | | extern int strvfappend(char **a, const char *format, va_list ap) |
1048 | 0 | { |
1049 | 0 | char *val; |
1050 | 0 | int sz; |
1051 | 0 | int res; |
1052 | |
|
1053 | 0 | sz = vasprintf(&val, format, ap); |
1054 | 0 | if (sz < 0) |
1055 | 0 | return -errno; |
1056 | | |
1057 | 0 | res = strappend(a, val); |
1058 | 0 | free(val); |
1059 | 0 | return res; |
1060 | 0 | } |
1061 | | |
1062 | | static size_t strcspn_escaped(const char *s, const char *reject) |
1063 | 0 | { |
1064 | 0 | int escaped = 0; |
1065 | 0 | int n; |
1066 | |
|
1067 | 0 | for (n=0; s[n]; n++) { |
1068 | 0 | if (escaped) |
1069 | 0 | escaped = 0; |
1070 | 0 | else if (s[n] == '\\') |
1071 | 0 | escaped = 1; |
1072 | 0 | else if (strchr(reject, s[n])) |
1073 | 0 | break; |
1074 | 0 | } |
1075 | | |
1076 | | /* if s ends in \, return index of previous char */ |
1077 | 0 | return n - escaped; |
1078 | 0 | } |
1079 | | |
1080 | | /* |
1081 | | * Like strchr() but ignores @c if escaped by '\', '\\' is interpreted like '\'. |
1082 | | * |
1083 | | * For example for @c='X': |
1084 | | * |
1085 | | * "abcdXefgXh" --> "XefgXh" |
1086 | | * "abcd\XefgXh" --> "Xh" |
1087 | | * "abcd\\XefgXh" --> "XefgXh" |
1088 | | * "abcd\\\XefgXh" --> "Xh" |
1089 | | * "abcd\Xefg\Xh" --> (null) |
1090 | | * |
1091 | | * "abcd\\XefgXh" --> "\XefgXh" for @c='\\' |
1092 | | */ |
1093 | | char *ul_strchr_escaped(const char *s, int c) |
1094 | 0 | { |
1095 | 0 | char *p; |
1096 | 0 | int esc = 0; |
1097 | |
|
1098 | 0 | for (p = (char *) s; p && *p; p++) { |
1099 | 0 | if (!esc && *p == '\\') { |
1100 | 0 | esc = 1; |
1101 | 0 | continue; |
1102 | 0 | } |
1103 | 0 | if (*p == c && (!esc || c == '\\')) |
1104 | 0 | return p; |
1105 | 0 | esc = 0; |
1106 | 0 | } |
1107 | | |
1108 | 0 | return NULL; |
1109 | 0 | } |
1110 | | |
1111 | | /* Split a string into words. */ |
1112 | | const char *split(const char **state, size_t *l, const char *separator, int quoted) |
1113 | 0 | { |
1114 | 0 | const char *current; |
1115 | |
|
1116 | 0 | current = *state; |
1117 | |
|
1118 | 0 | if (!*current) { |
1119 | 0 | assert(**state == '\0'); |
1120 | 0 | return NULL; |
1121 | 0 | } |
1122 | | |
1123 | 0 | current += strspn(current, separator); |
1124 | 0 | if (!*current) { |
1125 | 0 | *state = current; |
1126 | 0 | return NULL; |
1127 | 0 | } |
1128 | | |
1129 | 0 | if (quoted && strchr("\'\"", *current)) { |
1130 | 0 | char quotechars[2] = {*current, '\0'}; |
1131 | |
|
1132 | 0 | *l = strcspn_escaped(current + 1, quotechars); |
1133 | 0 | if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] || |
1134 | 0 | (current[*l + 2] && !strchr(separator, current[*l + 2]))) { |
1135 | | /* right quote missing or garbage at the end */ |
1136 | 0 | *state = current; |
1137 | 0 | return NULL; |
1138 | 0 | } |
1139 | 0 | *state = current++ + *l + 2; |
1140 | 0 | } else if (quoted) { |
1141 | 0 | *l = strcspn_escaped(current, separator); |
1142 | 0 | if (current[*l] && !strchr(separator, current[*l])) { |
1143 | | /* unfinished escape */ |
1144 | 0 | *state = current; |
1145 | 0 | return NULL; |
1146 | 0 | } |
1147 | 0 | *state = current + *l; |
1148 | 0 | } else { |
1149 | 0 | *l = strcspn(current, separator); |
1150 | 0 | *state = current + *l; |
1151 | 0 | } |
1152 | | |
1153 | 0 | return current; |
1154 | 0 | } |
1155 | | |
1156 | | /* Rewind file pointer forward to new line. */ |
1157 | | int skip_fline(FILE *fp) |
1158 | 0 | { |
1159 | 0 | int ch; |
1160 | |
|
1161 | 0 | do { |
1162 | 0 | if ((ch = fgetc(fp)) == EOF) |
1163 | 0 | return 1; |
1164 | 0 | if (ch == '\n') |
1165 | 0 | return 0; |
1166 | 0 | } while (1); |
1167 | 0 | } |
1168 | | |
1169 | | |
1170 | | /* compare two strings, but ignoring non-alnum and case of the characters, for example |
1171 | | * "Hello (123)!" is the same as "hello123". |
1172 | | */ |
1173 | | int ul_stralnumcmp(const char *p1, const char *p2) |
1174 | 0 | { |
1175 | 0 | const unsigned char *s1 = (const unsigned char *) p1; |
1176 | 0 | const unsigned char *s2 = (const unsigned char *) p2; |
1177 | 0 | unsigned char c1, c2; |
1178 | |
|
1179 | 0 | do { |
1180 | 0 | do { |
1181 | 0 | c1 = (unsigned char) *s1++; |
1182 | 0 | } while (c1 != '\0' && !isalnum((unsigned int) c1)); |
1183 | |
|
1184 | 0 | do { |
1185 | 0 | c2 = (unsigned char) *s2++; |
1186 | 0 | } while (c2 != '\0' && !isalnum((unsigned int) c2)); |
1187 | |
|
1188 | 0 | if (c1 != '\0') |
1189 | 0 | c1 = tolower(c1); |
1190 | 0 | if (c2 != '\0') |
1191 | 0 | c2 = tolower(c2); |
1192 | 0 | if (c1 == '\0') |
1193 | 0 | return c1 - c2; |
1194 | 0 | } while (c1 == c2); |
1195 | | |
1196 | 0 | return c1 - c2; |
1197 | 0 | } |
1198 | | |
1199 | | /* |
1200 | | * Parses the first option from @optstr. The @optstr pointer is set to the beginning |
1201 | | * of the next option. The options string looks like 'aaa,bbb=data,foo,bar="xxx"'. |
1202 | | * |
1203 | | * Note this function is used by libmount to parse mount options. Be careful when modify. |
1204 | | * |
1205 | | * Returns -EINVAL on parse error, 1 at the end of optstr and 0 on success. |
1206 | | */ |
1207 | | int ul_optstr_next(char **optstr, char **name, size_t *namesz, |
1208 | | char **value, size_t *valsz) |
1209 | 0 | { |
1210 | 0 | int open_quote = 0; |
1211 | 0 | char *start = NULL, *stop = NULL, *p, *sep = NULL; |
1212 | 0 | char *optstr0; |
1213 | |
|
1214 | 0 | assert(optstr); |
1215 | 0 | assert(*optstr); |
1216 | | |
1217 | 0 | optstr0 = *optstr; |
1218 | |
|
1219 | 0 | if (name) |
1220 | 0 | *name = NULL; |
1221 | 0 | if (namesz) |
1222 | 0 | *namesz = 0; |
1223 | 0 | if (value) |
1224 | 0 | *value = NULL; |
1225 | 0 | if (valsz) |
1226 | 0 | *valsz = 0; |
1227 | | |
1228 | | /* trim leading commas as to not invalidate option |
1229 | | * strings with multiple consecutive commas */ |
1230 | 0 | while (optstr0 && *optstr0 == ',') |
1231 | 0 | optstr0++; |
1232 | |
|
1233 | 0 | for (p = optstr0; p && *p; p++) { |
1234 | 0 | if (!start && *p == '=') |
1235 | 0 | return -EINVAL; |
1236 | 0 | if (!start) |
1237 | 0 | start = p; /* beginning of the option item */ |
1238 | 0 | if (*p == '"' && (p == optstr0 || *(p - 1) != '\\')) |
1239 | 0 | open_quote ^= 1; /* reverse the status */ |
1240 | 0 | if (open_quote) |
1241 | 0 | continue; /* still in quoted block */ |
1242 | 0 | if (!sep && p > start && *p == '=') |
1243 | 0 | sep = p; /* name and value separator */ |
1244 | 0 | if (*p == ',' && (p == optstr0 || *(p - 1) != '\\')) |
1245 | 0 | stop = p; /* terminate the option item */ |
1246 | 0 | else if (*(p + 1) == '\0') |
1247 | 0 | stop = p + 1; /* end of optstr */ |
1248 | 0 | if (!start || !stop) |
1249 | 0 | continue; |
1250 | 0 | if (stop <= start) |
1251 | 0 | return -EINVAL; |
1252 | | |
1253 | 0 | if (name) |
1254 | 0 | *name = start; |
1255 | 0 | if (namesz) |
1256 | 0 | *namesz = sep ? sep - start : stop - start; |
1257 | 0 | *optstr = *stop ? stop + 1 : stop; |
1258 | |
|
1259 | 0 | if (sep) { |
1260 | 0 | if (value) |
1261 | 0 | *value = sep + 1; |
1262 | 0 | if (valsz) |
1263 | 0 | *valsz = stop - sep - 1; |
1264 | 0 | } |
1265 | 0 | return 0; |
1266 | 0 | } |
1267 | | |
1268 | 0 | return 1; /* end of optstr */ |
1269 | 0 | } |
1270 | | |
1271 | | int ul_optstr_is_valid(const char *optstr) |
1272 | 0 | { |
1273 | 0 | int rc; |
1274 | 0 | char *p = (char *) optstr; |
1275 | |
|
1276 | 0 | while ((rc = ul_optstr_next(&p, NULL, NULL, NULL, NULL)) == 0); |
1277 | 0 | return rc < 0 ? 0 : 1; |
1278 | 0 | } |
1279 | | |
1280 | | #ifdef TEST_PROGRAM_STRUTILS |
1281 | | |
1282 | | #include "cctype.h" |
1283 | | |
1284 | | struct testS { |
1285 | | char *name; |
1286 | | char *value; |
1287 | | }; |
1288 | | |
1289 | | static int test_strdup_to_member(int argc, char *argv[]) |
1290 | | { |
1291 | | struct testS *xx; |
1292 | | |
1293 | | if (argc < 3) |
1294 | | return EXIT_FAILURE; |
1295 | | |
1296 | | xx = calloc(1, sizeof(*xx)); |
1297 | | if (!xx) |
1298 | | err(EXIT_FAILURE, "calloc() failed"); |
1299 | | |
1300 | | strdup_to_struct_member(xx, name, argv[1]); |
1301 | | strdup_to_struct_member(xx, value, argv[2]); |
1302 | | |
1303 | | if (strcmp(xx->name, argv[1]) != 0 && |
1304 | | strcmp(xx->value, argv[2]) != 0) |
1305 | | errx(EXIT_FAILURE, "strdup_to_struct_member() failed"); |
1306 | | |
1307 | | printf("1: '%s', 2: '%s'\n", xx->name, xx->value); |
1308 | | |
1309 | | free(xx->name); |
1310 | | free(xx->value); |
1311 | | free(xx); |
1312 | | return EXIT_SUCCESS; |
1313 | | } |
1314 | | |
1315 | | static int test_strutils_sizes(int argc, char *argv[]) |
1316 | | { |
1317 | | uintmax_t size = 0; |
1318 | | char *hum1, *hum2, *hum3; |
1319 | | |
1320 | | if (argc < 2) |
1321 | | return EXIT_FAILURE; |
1322 | | |
1323 | | if (strtosize(argv[1], &size)) |
1324 | | errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]); |
1325 | | |
1326 | | hum1 = size_to_human_string(SIZE_SUFFIX_1LETTER, size); |
1327 | | hum2 = size_to_human_string(SIZE_SUFFIX_3LETTER | |
1328 | | SIZE_SUFFIX_SPACE, size); |
1329 | | hum3 = size_to_human_string(SIZE_SUFFIX_3LETTER | |
1330 | | SIZE_SUFFIX_SPACE | |
1331 | | SIZE_DECIMAL_2DIGITS, size); |
1332 | | |
1333 | | printf("%25s : %20ju : %8s : %12s : %13s\n", argv[1], size, hum1, hum2, hum3); |
1334 | | free(hum1); |
1335 | | free(hum2); |
1336 | | free(hum3); |
1337 | | |
1338 | | return EXIT_SUCCESS; |
1339 | | } |
1340 | | |
1341 | | static int test_strutils_cmp_paths(int argc, char *argv[]) |
1342 | | { |
1343 | | int rc = streq_paths(argv[1], argv[2]); |
1344 | | |
1345 | | if (argc < 3) |
1346 | | return EXIT_FAILURE; |
1347 | | |
1348 | | printf("%s: '%s' '%s'\n", rc == 1 ? "YES" : "NOT", argv[1], argv[2]); |
1349 | | return EXIT_SUCCESS; |
1350 | | } |
1351 | | |
1352 | | static int test_strutils_normalize(int argc, char *argv[]) |
1353 | | { |
1354 | | unsigned char *src, *dst, *org; |
1355 | | size_t sz, len; |
1356 | | |
1357 | | if (argc < 2) |
1358 | | return EXIT_FAILURE; |
1359 | | |
1360 | | org = (unsigned char *) strdup(argv[1]); |
1361 | | src = (unsigned char *) strdup((char *) org); |
1362 | | len = strlen((char *) src); |
1363 | | dst = malloc(len + 1); |
1364 | | |
1365 | | if (!org || !src || !dst) |
1366 | | goto done; |
1367 | | |
1368 | | /* two buffers */ |
1369 | | sz = __normalize_whitespace(src, len, dst, len + 1); |
1370 | | printf("1: '%s' --> '%s' [sz=%zu]\n", src, dst, sz); |
1371 | | |
1372 | | /* one buffer */ |
1373 | | sz = normalize_whitespace(src); |
1374 | | printf("2: '%s' --> '%s' [sz=%zu]\n", org, src, sz); |
1375 | | |
1376 | | done: |
1377 | | free(src); |
1378 | | free(dst); |
1379 | | free(org); |
1380 | | |
1381 | | return EXIT_SUCCESS; |
1382 | | } |
1383 | | |
1384 | | static int test_strutils_cstrcasecmp(int argc, char *argv[]) |
1385 | | { |
1386 | | char *a, *b; |
1387 | | |
1388 | | if (argc < 3) |
1389 | | return EXIT_FAILURE; |
1390 | | |
1391 | | a = argv[1]; |
1392 | | b = argv[2]; |
1393 | | |
1394 | | if (!a || !b) |
1395 | | return EXIT_FAILURE; |
1396 | | |
1397 | | printf("cmp '%s' '%s' = %d\n", a, b, strcasecmp(a, b)); |
1398 | | printf("c_cmp '%s' '%s' = %d\n", a, b, c_strcasecmp(a, b)); |
1399 | | printf("c_ncmp '%s' '%s' = %d\n", a, b, c_strncasecmp(a, b, strlen(a))); |
1400 | | |
1401 | | return EXIT_SUCCESS; |
1402 | | } |
1403 | | |
1404 | | int main(int argc, char *argv[]) |
1405 | | { |
1406 | | if (argc == 3 && strcmp(argv[1], "--size") == 0) { |
1407 | | return test_strutils_sizes(argc - 1, argv + 1); |
1408 | | |
1409 | | } else if (argc == 3 && strcmp(argv[1], "--parse-switch") == 0) { |
1410 | | printf("'%s'-->%d\n", argv[2], ul_parse_switch(argv[2], |
1411 | | "on", "off", |
1412 | | "enable", "disable", |
1413 | | "yes", "no", |
1414 | | "1", "0", |
1415 | | NULL)); |
1416 | | return EXIT_SUCCESS; |
1417 | | } else if (argc == 4 && strcmp(argv[1], "--cmp-paths") == 0) { |
1418 | | return test_strutils_cmp_paths(argc - 1, argv + 1); |
1419 | | |
1420 | | } else if (argc == 4 && strcmp(argv[1], "--strdup-member") == 0) { |
1421 | | return test_strdup_to_member(argc - 1, argv + 1); |
1422 | | |
1423 | | } else if (argc == 4 && strcmp(argv[1], "--stralnumcmp") == 0) { |
1424 | | printf("%s\n", ul_stralnumcmp(argv[2], argv[3]) == 0 ? |
1425 | | "match" : "dismatch"); |
1426 | | return EXIT_SUCCESS; |
1427 | | |
1428 | | } else if (argc == 4 && strcmp(argv[1], "--cstrcasecmp") == 0) { |
1429 | | return test_strutils_cstrcasecmp(argc - 1, argv + 1); |
1430 | | |
1431 | | } else if (argc == 3 && strcmp(argv[1], "--normalize") == 0) { |
1432 | | return test_strutils_normalize(argc - 1, argv + 1); |
1433 | | |
1434 | | } else if (argc == 3 && strcmp(argv[1], "--strtos64") == 0) { |
1435 | | printf("'%s'-->%jd\n", argv[2], strtos64_or_err(argv[2], "strtos64 failed")); |
1436 | | return EXIT_SUCCESS; |
1437 | | } else if (argc == 3 && strcmp(argv[1], "--strtou64") == 0) { |
1438 | | printf("'%s'-->%ju\n", argv[2], strtou64_or_err(argv[2], "strtou64 failed")); |
1439 | | return EXIT_SUCCESS; |
1440 | | } else if (argc == 3 && strcmp(argv[1], "--strtos32") == 0) { |
1441 | | printf("'%s'-->%d\n", argv[2], strtos32_or_err(argv[2], "strtos32 failed")); |
1442 | | return EXIT_SUCCESS; |
1443 | | } else if (argc == 3 && strcmp(argv[1], "--strtou32") == 0) { |
1444 | | printf("'%s'-->%u\n", argv[2], strtou32_or_err(argv[2], "strtou32 failed")); |
1445 | | return EXIT_SUCCESS; |
1446 | | } else if (argc == 3 && strcmp(argv[1], "--strtos16") == 0) { |
1447 | | printf("'%s'-->%hd\n", argv[2], strtos16_or_err(argv[2], "strtos16 failed")); |
1448 | | return EXIT_SUCCESS; |
1449 | | } else if (argc == 3 && strcmp(argv[1], "--strtou16") == 0) { |
1450 | | printf("'%s'-->%hu\n", argv[2], strtou16_or_err(argv[2], "strtou16 failed")); |
1451 | | return EXIT_SUCCESS; |
1452 | | |
1453 | | } else if (argc == 4 && strcmp(argv[1], "--strchr-escaped") == 0) { |
1454 | | printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3])); |
1455 | | return EXIT_SUCCESS; |
1456 | | |
1457 | | } else if (argc == 2 && strcmp(argv[1], "--next-string") == 0) { |
1458 | | char *buf = "abc\0Y\0\0xyz\0X"; |
1459 | | char *end = buf + 12; |
1460 | | char *p = buf; |
1461 | | |
1462 | | do { |
1463 | | printf("str: '%s'\n", p); |
1464 | | } while ((p = ul_next_string(p, end))); |
1465 | | |
1466 | | return EXIT_SUCCESS; |
1467 | | |
1468 | | } else if (argc == 3 && strcmp(argv[1], "--optstr") == 0) { |
1469 | | |
1470 | | size_t namesz, valsz; |
1471 | | char *name = NULL, *val = NULL; |
1472 | | char *p = argv[2]; |
1473 | | int rc; |
1474 | | |
1475 | | if (!ul_optstr_is_valid(p)) |
1476 | | errx(EXIT_FAILURE, _("unsupported option format: %s"), p); |
1477 | | |
1478 | | while ((rc = ul_optstr_next(&p, &name, &namesz, &val, &valsz)) == 0) { |
1479 | | printf("'%.*s' : '%.*s'\n", (int) namesz, name, |
1480 | | (int) valsz, val); |
1481 | | } |
1482 | | if (rc == 1) |
1483 | | return EXIT_SUCCESS; |
1484 | | } else { |
1485 | | fprintf(stderr, "usage: %1$s --size <number>[suffix]\n" |
1486 | | " %1$s --parse-switch <str>\n" |
1487 | | " %1$s --cmp-paths <path> <path>\n" |
1488 | | " %1$s --strdup-member <str> <str>\n" |
1489 | | " %1$s --stralnumcmp <str> <str>\n" |
1490 | | " %1$s --cstrcasecmp <str> <str>\n" |
1491 | | " %1$s --normalize <str>\n" |
1492 | | " %1$s --strto{s,u}{16,32,64} <str>\n" |
1493 | | " %1$s --optstr <str>\n", |
1494 | | argv[0]); |
1495 | | exit(EXIT_FAILURE); |
1496 | | } |
1497 | | |
1498 | | return EXIT_FAILURE; |
1499 | | } |
1500 | | #endif /* TEST_PROGRAM_STRUTILS */ |