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