/src/util-linux/lib/strutils.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2010 Karel Zak <kzak@redhat.com> |
3 | | * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> |
4 | | * |
5 | | * No copyright is claimed. This code is in the public domain; do with |
6 | | * it what you wish. |
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 | 442 | { |
32 | 2.53k | while (power--) { |
33 | 2.22k | if (UINTMAX_MAX / base < *x) |
34 | 130 | return -ERANGE; |
35 | 2.09k | *x *= base; |
36 | 2.09k | } |
37 | 312 | return 0; |
38 | 442 | } |
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 parse_size(const char *str, uintmax_t *res, int *power) |
68 | 4.42k | { |
69 | 4.42k | const char *p; |
70 | 4.42k | char *end; |
71 | 4.42k | uintmax_t x, frac = 0; |
72 | 4.42k | int base = 1024, rc = 0, pwr = 0, frac_zeros = 0; |
73 | | |
74 | 4.42k | static const char *suf = "KMGTPEZY"; |
75 | 4.42k | static const char *suf2 = "kmgtpezy"; |
76 | 4.42k | const char *sp; |
77 | | |
78 | 4.42k | *res = 0; |
79 | | |
80 | 4.42k | if (!str || !*str) { |
81 | 9 | rc = -EINVAL; |
82 | 9 | goto err; |
83 | 9 | } |
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 | 4.42k | p = str; |
92 | 4.42k | while (isspace((unsigned char) *p)) |
93 | 589 | p++; |
94 | 4.42k | if (*p == '-') { |
95 | 1 | rc = -EINVAL; |
96 | 1 | goto err; |
97 | 1 | } |
98 | | |
99 | 4.41k | errno = 0, end = NULL; |
100 | 4.41k | x = strtoumax(str, &end, 0); |
101 | | |
102 | 4.41k | if (end == str || |
103 | 4.41k | (errno != 0 && (x == UINTMAX_MAX || x == 0))) { |
104 | 143 | rc = errno ? -errno : -EINVAL; |
105 | 143 | goto err; |
106 | 143 | } |
107 | 4.27k | if (!end || !*end) |
108 | 3.70k | goto done; /* without suffix */ |
109 | 573 | p = end; |
110 | | |
111 | | /* |
112 | | * Check size suffixes |
113 | | */ |
114 | 1.50k | check_suffix: |
115 | 1.50k | if (*(p + 1) == 'i' && (*(p + 2) == 'B' || *(p + 2) == 'b') && !*(p + 3)) |
116 | 2 | base = 1024; /* XiB, 2^N */ |
117 | 1.49k | else if ((*(p + 1) == 'B' || *(p + 1) == 'b') && !*(p + 2)) |
118 | 4 | base = 1000; /* XB, 10^N */ |
119 | 1.49k | else if (*(p + 1)) { |
120 | 1.22k | struct lconv const *l = localeconv(); |
121 | 1.22k | const char *dp = l ? l->decimal_point : NULL; |
122 | 1.22k | size_t dpsz = dp ? strlen(dp) : 0; |
123 | | |
124 | 1.22k | if (frac == 0 && *p && dp && strncmp(dp, p, dpsz) == 0) { |
125 | 1.05k | const char *fstr = p + dpsz; |
126 | | |
127 | 9.62k | for (p = fstr; *p == '0'; p++) |
128 | 8.57k | frac_zeros++; |
129 | 1.05k | fstr = p; |
130 | 1.05k | if (isdigit(*fstr)) { |
131 | 456 | errno = 0, end = NULL; |
132 | 456 | frac = strtoumax(fstr, &end, 0); |
133 | 456 | if (end == fstr || |
134 | 456 | (errno != 0 && (frac == UINTMAX_MAX || frac == 0))) { |
135 | 1 | rc = errno ? -errno : -EINVAL; |
136 | 1 | goto err; |
137 | 1 | } |
138 | 456 | } else |
139 | 596 | end = (char *) p; |
140 | | |
141 | 1.05k | if (frac && (!end || !*end)) { |
142 | 123 | rc = -EINVAL; |
143 | 123 | goto err; /* without suffix, but with frac */ |
144 | 123 | } |
145 | 928 | p = end; |
146 | 928 | goto check_suffix; |
147 | 1.05k | } |
148 | 175 | rc = -EINVAL; |
149 | 175 | goto err; /* unexpected suffix */ |
150 | 1.22k | } |
151 | | |
152 | 274 | sp = strchr(suf, *p); |
153 | 274 | if (sp) |
154 | 106 | pwr = (sp - suf) + 1; |
155 | 168 | else { |
156 | 168 | sp = strchr(suf2, *p); |
157 | 168 | if (sp) |
158 | 131 | pwr = (sp - suf2) + 1; |
159 | 37 | else { |
160 | 37 | rc = -EINVAL; |
161 | 37 | goto err; |
162 | 37 | } |
163 | 168 | } |
164 | | |
165 | 237 | rc = do_scale_by_power(&x, base, pwr); |
166 | 237 | if (power) |
167 | 237 | *power = pwr; |
168 | 237 | if (frac && pwr) { |
169 | 205 | int i; |
170 | 205 | uintmax_t frac_div = 10, frac_poz = 1, frac_base = 1; |
171 | | |
172 | | /* mega, giga, ... */ |
173 | 205 | 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 | 2.49k | while (frac_div < frac) { |
181 | 2.28k | if (frac_div <= UINTMAX_MAX/10) |
182 | 2.22k | frac_div *= 10; |
183 | 62 | else |
184 | 62 | frac /= 10; |
185 | 2.28k | } |
186 | | |
187 | | /* 'frac' is without zeros (5 means 0.5 as well as 0.05) */ |
188 | 8.37k | for (i = 0; i < frac_zeros; i++) { |
189 | 8.17k | if (frac_div <= UINTMAX_MAX/10) |
190 | 247 | frac_div *= 10; |
191 | 7.92k | else |
192 | 7.92k | frac /= 10; |
193 | 8.17k | } |
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 | 2.40k | do { |
203 | 2.40k | unsigned int seg = frac % 10; /* last digit of the frac */ |
204 | 2.40k | uintmax_t seg_div = frac_div / frac_poz; /* what represents the segment 1000, 100, .. */ |
205 | | |
206 | 2.40k | frac /= 10; /* remove last digit from frac */ |
207 | 2.40k | frac_poz *= 10; |
208 | | |
209 | 2.40k | if (seg && seg_div / seg) |
210 | 2.04k | x += frac_base / (seg_div / seg); |
211 | 2.40k | } while (frac); |
212 | 205 | } |
213 | 3.94k | done: |
214 | 3.94k | *res = x; |
215 | 4.42k | err: |
216 | 4.42k | if (rc < 0) |
217 | 556 | errno = -rc; |
218 | 4.42k | return rc; |
219 | 3.94k | } |
220 | | |
221 | | int strtosize(const char *str, uintmax_t *res) |
222 | 0 | { |
223 | 0 | return 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 | 41 | { |
239 | 41 | const char *p; |
240 | | |
241 | 41 | for (p = str; p && *p && isxdigit((unsigned char) *p); p++); |
242 | | |
243 | 41 | if (end) |
244 | 0 | *end = p; |
245 | | |
246 | 41 | return p && p > str && !*p; |
247 | 41 | } |
248 | | |
249 | | /* |
250 | | * parse_switch(argv[i], "on", "off", "yes", "no", NULL); |
251 | | */ |
252 | | int parse_switch(const char *arg, const char *errmesg, ...) |
253 | 0 | { |
254 | 0 | const char *a, *b; |
255 | 0 | va_list ap; |
256 | |
|
257 | 0 | va_start(ap, errmesg); |
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, "%s: '%s'", errmesg, 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 | 850 | { |
345 | 850 | char *end = NULL; |
346 | 850 | int64_t tmp; |
347 | | |
348 | 850 | 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 | 850 | errno = 0; |
355 | 850 | tmp = (int64_t) strtoimax(str, &end, base); |
356 | 850 | if (tmp < 0) |
357 | 82 | errno = ERANGE; |
358 | 768 | else { |
359 | 768 | errno = 0; |
360 | 768 | *num = strtoumax(str, &end, base); |
361 | 768 | } |
362 | | |
363 | 850 | if (errno != 0) |
364 | 82 | return -errno; |
365 | 768 | if (str == end || (end && *end)) |
366 | 14 | return -(errno = EINVAL); |
367 | 754 | return 0; |
368 | 768 | } |
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 then |
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 | | long double strtold_or_err(const char *str, const char *errmesg) |
460 | 0 | { |
461 | 0 | double num; |
462 | 0 | char *end = NULL; |
463 | |
|
464 | 0 | errno = 0; |
465 | 0 | if (str == NULL || *str == '\0') |
466 | 0 | goto err; |
467 | 0 | num = strtold(str, &end); |
468 | |
|
469 | 0 | if (errno || str == end || (end && *end)) |
470 | 0 | goto err; |
471 | | |
472 | 0 | return num; |
473 | 0 | err: |
474 | 0 | if (errno == ERANGE) |
475 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
476 | | |
477 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
478 | 0 | } |
479 | | |
480 | | uintmax_t strtosize_or_err(const char *str, const char *errmesg) |
481 | 0 | { |
482 | 0 | uintmax_t num; |
483 | |
|
484 | 0 | if (strtosize(str, &num) == 0) |
485 | 0 | return num; |
486 | | |
487 | 0 | if (errno) |
488 | 0 | err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
489 | | |
490 | 0 | errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); |
491 | 0 | } |
492 | | |
493 | | |
494 | | void strtotimeval_or_err(const char *str, struct timeval *tv, const char *errmesg) |
495 | 0 | { |
496 | 0 | long double user_input; |
497 | |
|
498 | 0 | user_input = strtold_or_err(str, errmesg); |
499 | 0 | tv->tv_sec = (time_t) user_input; |
500 | 0 | tv->tv_usec = (suseconds_t)((user_input - tv->tv_sec) * 1000000); |
501 | 0 | } |
502 | | |
503 | | void strtotimespec_or_err(const char *str, struct timespec *ts, const char *errmesg) |
504 | 0 | { |
505 | 0 | long double user_input; |
506 | |
|
507 | 0 | user_input = strtold_or_err(str, errmesg); |
508 | 0 | ts->tv_sec = (time_t) user_input; |
509 | 0 | ts->tv_nsec = (long)((user_input - ts->tv_sec) * 1000000000); |
510 | 0 | } |
511 | | |
512 | | time_t strtotime_or_err(const char *str, const char *errmesg) |
513 | 0 | { |
514 | 0 | int64_t user_input; |
515 | |
|
516 | 0 | user_input = strtos64_or_err(str, errmesg); |
517 | 0 | return (time_t) user_input; |
518 | 0 | } |
519 | | |
520 | | /* |
521 | | * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must |
522 | | * be 11 bytes. |
523 | | */ |
524 | | char *xstrmode(mode_t mode, char *str) |
525 | 0 | { |
526 | 0 | unsigned short i = 0; |
527 | |
|
528 | 0 | if (S_ISDIR(mode)) |
529 | 0 | str[i++] = 'd'; |
530 | 0 | else if (S_ISLNK(mode)) |
531 | 0 | str[i++] = 'l'; |
532 | 0 | else if (S_ISCHR(mode)) |
533 | 0 | str[i++] = 'c'; |
534 | 0 | else if (S_ISBLK(mode)) |
535 | 0 | str[i++] = 'b'; |
536 | 0 | else if (S_ISSOCK(mode)) |
537 | 0 | str[i++] = 's'; |
538 | 0 | else if (S_ISFIFO(mode)) |
539 | 0 | str[i++] = 'p'; |
540 | 0 | else if (S_ISREG(mode)) |
541 | 0 | str[i++] = '-'; |
542 | |
|
543 | 0 | str[i++] = mode & S_IRUSR ? 'r' : '-'; |
544 | 0 | str[i++] = mode & S_IWUSR ? 'w' : '-'; |
545 | 0 | str[i++] = (mode & S_ISUID |
546 | 0 | ? (mode & S_IXUSR ? 's' : 'S') |
547 | 0 | : (mode & S_IXUSR ? 'x' : '-')); |
548 | 0 | str[i++] = mode & S_IRGRP ? 'r' : '-'; |
549 | 0 | str[i++] = mode & S_IWGRP ? 'w' : '-'; |
550 | 0 | str[i++] = (mode & S_ISGID |
551 | 0 | ? (mode & S_IXGRP ? 's' : 'S') |
552 | 0 | : (mode & S_IXGRP ? 'x' : '-')); |
553 | 0 | str[i++] = mode & S_IROTH ? 'r' : '-'; |
554 | 0 | str[i++] = mode & S_IWOTH ? 'w' : '-'; |
555 | 0 | str[i++] = (mode & S_ISVTX |
556 | 0 | ? (mode & S_IXOTH ? 't' : 'T') |
557 | 0 | : (mode & S_IXOTH ? 'x' : '-')); |
558 | 0 | str[i] = '\0'; |
559 | |
|
560 | 0 | return str; |
561 | 0 | } |
562 | | |
563 | | /* |
564 | | * returns exponent (2^x=n) in range KiB..EiB (2^10..2^60) |
565 | | */ |
566 | | static int get_exp(uint64_t n) |
567 | 0 | { |
568 | 0 | int shft; |
569 | |
|
570 | 0 | for (shft = 10; shft <= 60; shft += 10) { |
571 | 0 | if (n < (1ULL << shft)) |
572 | 0 | break; |
573 | 0 | } |
574 | 0 | return shft - 10; |
575 | 0 | } |
576 | | |
577 | | char *size_to_human_string(int options, uint64_t bytes) |
578 | 0 | { |
579 | 0 | char buf[32]; |
580 | 0 | int dec, exp; |
581 | 0 | uint64_t frac; |
582 | 0 | const char *letters = "BKMGTPE"; |
583 | 0 | char suffix[sizeof(" KiB")], *psuf = suffix; |
584 | 0 | char c; |
585 | |
|
586 | 0 | if (options & SIZE_SUFFIX_SPACE) |
587 | 0 | *psuf++ = ' '; |
588 | | |
589 | |
|
590 | 0 | exp = get_exp(bytes); |
591 | 0 | c = *(letters + (exp ? exp / 10 : 0)); |
592 | 0 | dec = exp ? bytes / (1ULL << exp) : bytes; |
593 | 0 | frac = exp ? bytes % (1ULL << exp) : 0; |
594 | |
|
595 | 0 | *psuf++ = c; |
596 | |
|
597 | 0 | if ((options & SIZE_SUFFIX_3LETTER) && (c != 'B')) { |
598 | 0 | *psuf++ = 'i'; |
599 | 0 | *psuf++ = 'B'; |
600 | 0 | } |
601 | |
|
602 | 0 | *psuf = '\0'; |
603 | | |
604 | | /* fprintf(stderr, "exp: %d, unit: %c, dec: %d, frac: %jd\n", |
605 | | * exp, suffix[0], dec, frac); |
606 | | */ |
607 | | |
608 | | /* round */ |
609 | 0 | if (frac) { |
610 | | /* get 3 digits after decimal point */ |
611 | 0 | if (frac >= UINT64_MAX / 1000) |
612 | 0 | frac = ((frac / 1024) * 1000) / (1ULL << (exp - 10)) ; |
613 | 0 | else |
614 | 0 | frac = (frac * 1000) / (1ULL << (exp)) ; |
615 | |
|
616 | 0 | if (options & SIZE_DECIMAL_2DIGITS) { |
617 | | /* round 4/5 and keep 2 digits after decimal point */ |
618 | 0 | frac = (frac + 5) / 10 ; |
619 | 0 | } else { |
620 | | /* round 4/5 and keep 1 digit after decimal point */ |
621 | 0 | frac = ((frac + 50) / 100) * 10 ; |
622 | 0 | } |
623 | | |
624 | | /* rounding could have overflowed */ |
625 | 0 | if (frac == 100) { |
626 | 0 | dec++; |
627 | 0 | frac = 0; |
628 | 0 | } |
629 | 0 | } |
630 | |
|
631 | 0 | if (frac) { |
632 | 0 | struct lconv const *l = localeconv(); |
633 | 0 | char *dp = l ? l->decimal_point : NULL; |
634 | 0 | int len; |
635 | |
|
636 | 0 | if (!dp || !*dp) |
637 | 0 | dp = "."; |
638 | |
|
639 | 0 | len = snprintf(buf, sizeof(buf), "%d%s%02" PRIu64, dec, dp, frac); |
640 | 0 | if (len > 0 && (size_t) len < sizeof(buf)) { |
641 | | /* remove potential extraneous zero */ |
642 | 0 | if (buf[len - 1] == '0') |
643 | 0 | buf[len--] = '\0'; |
644 | | /* append suffix */ |
645 | 0 | xstrncpy(buf+len, suffix, sizeof(buf) - len); |
646 | 0 | } else |
647 | 0 | *buf = '\0'; /* snprintf error */ |
648 | 0 | } else |
649 | 0 | snprintf(buf, sizeof(buf), "%d%s", dec, suffix); |
650 | |
|
651 | 0 | return strdup(buf); |
652 | 0 | } |
653 | | |
654 | | /* |
655 | | * Parses comma delimited list to array with IDs, for example: |
656 | | * |
657 | | * "aaa,bbb,ccc" --> ary[0] = FOO_AAA; |
658 | | * ary[1] = FOO_BBB; |
659 | | * ary[3] = FOO_CCC; |
660 | | * |
661 | | * The function name2id() provides conversion from string to ID. |
662 | | * |
663 | | * Returns: >= 0 : number of items added to ary[] |
664 | | * -1 : parse error or unknown item |
665 | | * -2 : arysz reached |
666 | | */ |
667 | | int string_to_idarray(const char *list, int ary[], size_t arysz, |
668 | | int (name2id)(const char *, size_t)) |
669 | 0 | { |
670 | 0 | const char *begin = NULL, *p; |
671 | 0 | size_t n = 0; |
672 | |
|
673 | 0 | if (!list || !*list || !ary || !arysz || !name2id) |
674 | 0 | return -1; |
675 | | |
676 | 0 | for (p = list; p && *p; p++) { |
677 | 0 | const char *end = NULL; |
678 | 0 | int id; |
679 | |
|
680 | 0 | if (n >= arysz) |
681 | 0 | return -2; |
682 | 0 | if (!begin) |
683 | 0 | begin = p; /* begin of the column name */ |
684 | 0 | if (*p == ',') |
685 | 0 | end = p; /* terminate the name */ |
686 | 0 | if (*(p + 1) == '\0') |
687 | 0 | end = p + 1; /* end of string */ |
688 | 0 | if (!begin || !end) |
689 | 0 | continue; |
690 | 0 | if (end <= begin) |
691 | 0 | return -1; |
692 | | |
693 | 0 | id = name2id(begin, end - begin); |
694 | 0 | if (id == -1) |
695 | 0 | return -1; |
696 | 0 | ary[ n++ ] = id; |
697 | 0 | begin = NULL; |
698 | 0 | if (end && !*end) |
699 | 0 | break; |
700 | 0 | } |
701 | 0 | return n; |
702 | 0 | } |
703 | | |
704 | | /* |
705 | | * Parses the array like string_to_idarray but if format is "+aaa,bbb" |
706 | | * it adds fields to array instead of replacing them. |
707 | | */ |
708 | | int string_add_to_idarray(const char *list, int ary[], size_t arysz, |
709 | | size_t *ary_pos, int (name2id)(const char *, size_t)) |
710 | 0 | { |
711 | 0 | const char *list_add; |
712 | 0 | int r; |
713 | |
|
714 | 0 | if (!list || !*list || !ary_pos || *ary_pos > arysz) |
715 | 0 | return -1; |
716 | | |
717 | 0 | if (list[0] == '+') |
718 | 0 | list_add = &list[1]; |
719 | 0 | else { |
720 | 0 | list_add = list; |
721 | 0 | *ary_pos = 0; |
722 | 0 | } |
723 | |
|
724 | 0 | r = string_to_idarray(list_add, &ary[*ary_pos], arysz - *ary_pos, name2id); |
725 | 0 | if (r > 0) |
726 | 0 | *ary_pos += r; |
727 | 0 | return r; |
728 | 0 | } |
729 | | |
730 | | /* |
731 | | * LIST ::= <item> [, <item>] |
732 | | * |
733 | | * The <item> is translated to 'id' by name2id() function and the 'id' is used |
734 | | * as a position in the 'ary' bit array. It means that the 'id' has to be in |
735 | | * range <0..N> where N < sizeof(ary) * NBBY. |
736 | | * |
737 | | * If allow_range is enabled: |
738 | | * An item ending in '+' also sets all bits in <0..N>. |
739 | | * An item beginning with '+' also sets all bits in <N..allow_minus>. |
740 | | * |
741 | | * Returns: 0 on success, <0 on error. |
742 | | */ |
743 | | int string_to_bitarray(const char *list, |
744 | | char *ary, |
745 | | int (*name2bit)(const char *, size_t), |
746 | | size_t allow_range) |
747 | 0 | { |
748 | 0 | const char *begin = NULL, *p; |
749 | |
|
750 | 0 | if (!list || !name2bit || !ary) |
751 | 0 | return -EINVAL; |
752 | | |
753 | 0 | for (p = list; p && *p; p++) { |
754 | 0 | const char *end = NULL; |
755 | 0 | int bit, set_lower = 0, set_higher = 0; |
756 | |
|
757 | 0 | if (!begin) |
758 | 0 | begin = p; /* begin of the level name */ |
759 | 0 | if (*p == ',') |
760 | 0 | end = p; /* terminate the name */ |
761 | 0 | if (*(p + 1) == '\0') |
762 | 0 | end = p + 1; /* end of string */ |
763 | 0 | if (!begin || !end) |
764 | 0 | continue; |
765 | 0 | if (end <= begin) |
766 | 0 | return -1; |
767 | 0 | if (allow_range) { |
768 | 0 | if (*(end - 1) == '+') { |
769 | 0 | end--; |
770 | 0 | set_lower = 1; |
771 | 0 | } else if (*begin == '+') { |
772 | 0 | begin++; |
773 | 0 | set_higher = 1; |
774 | 0 | } |
775 | 0 | } |
776 | |
|
777 | 0 | bit = name2bit(begin, end - begin); |
778 | 0 | if (bit < 0) |
779 | 0 | return bit; |
780 | 0 | setbit(ary, bit); |
781 | 0 | if (set_lower) |
782 | 0 | while (--bit >= 0) |
783 | 0 | setbit(ary, bit); |
784 | 0 | else if (set_higher) |
785 | 0 | while (++bit < (int) allow_range) |
786 | 0 | setbit(ary, bit); |
787 | 0 | begin = NULL; |
788 | 0 | if (end && !*end) |
789 | 0 | break; |
790 | 0 | } |
791 | 0 | return 0; |
792 | 0 | } |
793 | | |
794 | | /* |
795 | | * LIST ::= <item> [, <item>] |
796 | | * |
797 | | * The <item> is translated to 'id' by name2flag() function and the flags is |
798 | | * set to the 'mask' |
799 | | * |
800 | | * Returns: 0 on success, <0 on error. |
801 | | */ |
802 | | int string_to_bitmask(const char *list, |
803 | | unsigned long *mask, |
804 | | long (*name2flag)(const char *, size_t)) |
805 | 0 | { |
806 | 0 | const char *begin = NULL, *p; |
807 | |
|
808 | 0 | if (!list || !name2flag || !mask) |
809 | 0 | return -EINVAL; |
810 | | |
811 | 0 | for (p = list; p && *p; p++) { |
812 | 0 | const char *end = NULL; |
813 | 0 | long flag; |
814 | |
|
815 | 0 | if (!begin) |
816 | 0 | begin = p; /* begin of the level name */ |
817 | 0 | if (*p == ',') |
818 | 0 | end = p; /* terminate the name */ |
819 | 0 | if (*(p + 1) == '\0') |
820 | 0 | end = p + 1; /* end of string */ |
821 | 0 | if (!begin || !end) |
822 | 0 | continue; |
823 | 0 | if (end <= begin) |
824 | 0 | return -1; |
825 | | |
826 | 0 | flag = name2flag(begin, end - begin); |
827 | 0 | if (flag < 0) |
828 | 0 | return flag; /* error */ |
829 | 0 | *mask |= flag; |
830 | 0 | begin = NULL; |
831 | 0 | if (end && !*end) |
832 | 0 | break; |
833 | 0 | } |
834 | 0 | return 0; |
835 | 0 | } |
836 | | |
837 | | /* |
838 | | * Parse the lower and higher values in a string containing |
839 | | * "lower:higher" or "lower-higher" format. Note that either |
840 | | * the lower or the higher values may be missing, and the def |
841 | | * value will be assigned to it by default. |
842 | | * |
843 | | * Returns: 0 on success, <0 on error. |
844 | | */ |
845 | | int parse_range(const char *str, int *lower, int *upper, int def) |
846 | 0 | { |
847 | 0 | char *end = NULL; |
848 | |
|
849 | 0 | if (!str) |
850 | 0 | return 0; |
851 | | |
852 | 0 | *upper = *lower = def; |
853 | 0 | errno = 0; |
854 | |
|
855 | 0 | if (*str == ':') { /* <:N> */ |
856 | 0 | str++; |
857 | 0 | *upper = strtol(str, &end, 10); |
858 | 0 | if (errno || !end || *end || end == str) |
859 | 0 | return -1; |
860 | 0 | } else { |
861 | 0 | *upper = *lower = strtol(str, &end, 10); |
862 | 0 | if (errno || !end || end == str) |
863 | 0 | return -1; |
864 | | |
865 | 0 | if (*end == ':' && !*(end + 1)) /* <M:> */ |
866 | 0 | *upper = def; |
867 | 0 | else if (*end == '-' || *end == ':') { /* <M:N> <M-N> */ |
868 | 0 | str = end + 1; |
869 | 0 | end = NULL; |
870 | 0 | errno = 0; |
871 | 0 | *upper = strtol(str, &end, 10); |
872 | |
|
873 | 0 | if (errno || !end || *end || end == str) |
874 | 0 | return -1; |
875 | 0 | } |
876 | 0 | } |
877 | 0 | return 0; |
878 | 0 | } |
879 | | |
880 | | static const char *next_path_segment(const char *str, size_t *sz) |
881 | 0 | { |
882 | 0 | const char *start, *p; |
883 | |
|
884 | 0 | start = str; |
885 | 0 | *sz = 0; |
886 | 0 | while (start && *start == '/' && *(start + 1) == '/') |
887 | 0 | start++; |
888 | |
|
889 | 0 | if (!start || !*start) |
890 | 0 | return NULL; |
891 | | |
892 | 0 | for (*sz = 1, p = start + 1; *p && *p != '/'; p++) { |
893 | 0 | (*sz)++; |
894 | 0 | } |
895 | |
|
896 | 0 | return start; |
897 | 0 | } |
898 | | |
899 | | int streq_paths(const char *a, const char *b) |
900 | 0 | { |
901 | 0 | while (a && b) { |
902 | 0 | size_t a_sz, b_sz; |
903 | 0 | const char *a_seg = next_path_segment(a, &a_sz); |
904 | 0 | const char *b_seg = next_path_segment(b, &b_sz); |
905 | | |
906 | | /* |
907 | | fprintf(stderr, "A>>>(%zu) '%s'\n", a_sz, a_seg); |
908 | | fprintf(stderr, "B>>>(%zu) '%s'\n", b_sz, b_seg); |
909 | | */ |
910 | | |
911 | | /* end of the path */ |
912 | 0 | if (a_sz + b_sz == 0) |
913 | 0 | return 1; |
914 | | |
915 | | /* ignore tailing slash */ |
916 | 0 | if (a_sz + b_sz == 1 && |
917 | 0 | ((a_seg && *a_seg == '/') || (b_seg && *b_seg == '/'))) |
918 | 0 | return 1; |
919 | | |
920 | 0 | if (!a_seg || !b_seg) |
921 | 0 | break; |
922 | 0 | if (a_sz != b_sz || strncmp(a_seg, b_seg, a_sz) != 0) |
923 | 0 | break; |
924 | | |
925 | 0 | a = a_seg + a_sz; |
926 | 0 | b = b_seg + b_sz; |
927 | 0 | }; |
928 | |
|
929 | 0 | return 0; |
930 | 0 | } |
931 | | |
932 | | /* concatenate two strings to a new string, the size of the second string is limited by @b */ |
933 | | char *strnconcat(const char *s, const char *suffix, size_t b) |
934 | 0 | { |
935 | 0 | size_t a; |
936 | 0 | char *r; |
937 | |
|
938 | 0 | if (!s && !suffix) |
939 | 0 | return strdup(""); |
940 | 0 | if (!s) |
941 | 0 | return strndup(suffix, b); |
942 | 0 | if (!suffix) |
943 | 0 | return strdup(s); |
944 | | |
945 | 0 | assert(s); |
946 | 0 | assert(suffix); |
947 | | |
948 | 0 | a = strlen(s); |
949 | 0 | if (b > ((size_t) -1) - a) |
950 | 0 | return NULL; |
951 | | |
952 | 0 | r = malloc(a + b + 1); |
953 | 0 | if (!r) |
954 | 0 | return NULL; |
955 | | |
956 | 0 | memcpy(r, s, a); |
957 | 0 | memcpy(r + a, suffix, b); |
958 | 0 | r[a+b] = 0; |
959 | |
|
960 | 0 | return r; |
961 | 0 | } |
962 | | |
963 | | /* concatenate two strings to a new string */ |
964 | | char *strconcat(const char *s, const char *suffix) |
965 | 0 | { |
966 | 0 | return strnconcat(s, suffix, suffix ? strlen(suffix) : 0); |
967 | 0 | } |
968 | | |
969 | | /* concatenate @s and string defined by @format to a new string */ |
970 | | char *strfconcat(const char *s, const char *format, ...) |
971 | 0 | { |
972 | 0 | va_list ap; |
973 | 0 | char *val, *res; |
974 | 0 | int sz; |
975 | |
|
976 | 0 | va_start(ap, format); |
977 | 0 | sz = vasprintf(&val, format, ap); |
978 | 0 | va_end(ap); |
979 | |
|
980 | 0 | if (sz < 0) |
981 | 0 | return NULL; |
982 | | |
983 | 0 | res = strnconcat(s, val, sz); |
984 | 0 | free(val); |
985 | 0 | return res; |
986 | 0 | } |
987 | | |
988 | | int strappend(char **a, const char *b) |
989 | 4.09k | { |
990 | 4.09k | size_t al, bl; |
991 | 4.09k | char *tmp; |
992 | | |
993 | 4.09k | if (!a) |
994 | 0 | return -EINVAL; |
995 | 4.09k | if (!b || !*b) |
996 | 215 | return 0; |
997 | 3.87k | if (!*a) { |
998 | 1.09k | *a = strdup(b); |
999 | 1.09k | return !*a ? -ENOMEM : 0; |
1000 | 1.09k | } |
1001 | | |
1002 | 2.78k | al = strlen(*a); |
1003 | 2.78k | bl = strlen(b); |
1004 | | |
1005 | 2.78k | tmp = realloc(*a, al + bl + 1); |
1006 | 2.78k | if (!tmp) |
1007 | 0 | return -ENOMEM; |
1008 | 2.78k | *a = tmp; |
1009 | 2.78k | memcpy((*a) + al, b, bl + 1); |
1010 | 2.78k | return 0; |
1011 | 2.78k | } |
1012 | | |
1013 | | static size_t strcspn_escaped(const char *s, const char *reject) |
1014 | 0 | { |
1015 | 0 | int escaped = 0; |
1016 | 0 | int n; |
1017 | |
|
1018 | 0 | for (n=0; s[n]; n++) { |
1019 | 0 | if (escaped) |
1020 | 0 | escaped = 0; |
1021 | 0 | else if (s[n] == '\\') |
1022 | 0 | escaped = 1; |
1023 | 0 | else if (strchr(reject, s[n])) |
1024 | 0 | break; |
1025 | 0 | } |
1026 | | |
1027 | | /* if s ends in \, return index of previous char */ |
1028 | 0 | return n - escaped; |
1029 | 0 | } |
1030 | | |
1031 | | /* |
1032 | | * Like strchr() but ignores @c if escaped by '\', '\\' is interpreted like '\'. |
1033 | | * |
1034 | | * For example for @c='X': |
1035 | | * |
1036 | | * "abcdXefgXh" --> "XefgXh" |
1037 | | * "abcd\XefgXh" --> "Xh" |
1038 | | * "abcd\\XefgXh" --> "XefgXh" |
1039 | | * "abcd\\\XefgXh" --> "Xh" |
1040 | | * "abcd\Xefg\Xh" --> (null) |
1041 | | * |
1042 | | * "abcd\\XefgXh" --> "\XefgXh" for @c='\\' |
1043 | | */ |
1044 | | char *ul_strchr_escaped(const char *s, int c) |
1045 | 0 | { |
1046 | 0 | char *p; |
1047 | 0 | int esc = 0; |
1048 | |
|
1049 | 0 | for (p = (char *) s; p && *p; p++) { |
1050 | 0 | if (!esc && *p == '\\') { |
1051 | 0 | esc = 1; |
1052 | 0 | continue; |
1053 | 0 | } |
1054 | 0 | if (*p == c && (!esc || c == '\\')) |
1055 | 0 | return p; |
1056 | 0 | esc = 0; |
1057 | 0 | } |
1058 | | |
1059 | 0 | return NULL; |
1060 | 0 | } |
1061 | | |
1062 | | /* Split a string into words. */ |
1063 | | const char *split(const char **state, size_t *l, const char *separator, int quoted) |
1064 | 0 | { |
1065 | 0 | const char *current; |
1066 | |
|
1067 | 0 | current = *state; |
1068 | |
|
1069 | 0 | if (!*current) { |
1070 | 0 | assert(**state == '\0'); |
1071 | 0 | return NULL; |
1072 | 0 | } |
1073 | | |
1074 | 0 | current += strspn(current, separator); |
1075 | 0 | if (!*current) { |
1076 | 0 | *state = current; |
1077 | 0 | return NULL; |
1078 | 0 | } |
1079 | | |
1080 | 0 | if (quoted && strchr("\'\"", *current)) { |
1081 | 0 | char quotechars[2] = {*current, '\0'}; |
1082 | |
|
1083 | 0 | *l = strcspn_escaped(current + 1, quotechars); |
1084 | 0 | if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] || |
1085 | 0 | (current[*l + 2] && !strchr(separator, current[*l + 2]))) { |
1086 | | /* right quote missing or garbage at the end */ |
1087 | 0 | *state = current; |
1088 | 0 | return NULL; |
1089 | 0 | } |
1090 | 0 | *state = current++ + *l + 2; |
1091 | 0 | } else if (quoted) { |
1092 | 0 | *l = strcspn_escaped(current, separator); |
1093 | 0 | if (current[*l] && !strchr(separator, current[*l])) { |
1094 | | /* unfinished escape */ |
1095 | 0 | *state = current; |
1096 | 0 | return NULL; |
1097 | 0 | } |
1098 | 0 | *state = current + *l; |
1099 | 0 | } else { |
1100 | 0 | *l = strcspn(current, separator); |
1101 | 0 | *state = current + *l; |
1102 | 0 | } |
1103 | | |
1104 | 0 | return current; |
1105 | 0 | } |
1106 | | |
1107 | | /* Rewind file pointer forward to new line. */ |
1108 | | int skip_fline(FILE *fp) |
1109 | 0 | { |
1110 | 0 | int ch; |
1111 | |
|
1112 | 0 | do { |
1113 | 0 | if ((ch = fgetc(fp)) == EOF) |
1114 | 0 | return 1; |
1115 | 0 | if (ch == '\n') |
1116 | 0 | return 0; |
1117 | 0 | } while (1); |
1118 | 0 | } |
1119 | | |
1120 | | |
1121 | | /* compare two strings, but ignoring non-alnum and case of the characters, for example |
1122 | | * "Hello (123)!" is the same as "hello123". |
1123 | | */ |
1124 | | int ul_stralnumcmp(const char *p1, const char *p2) |
1125 | 504k | { |
1126 | 504k | const unsigned char *s1 = (const unsigned char *) p1; |
1127 | 504k | const unsigned char *s2 = (const unsigned char *) p2; |
1128 | 504k | unsigned char c1, c2; |
1129 | | |
1130 | 545k | do { |
1131 | 549k | do { |
1132 | 549k | c1 = (unsigned char) *s1++; |
1133 | 549k | } while (c1 != '\0' && !isalnum((unsigned int) c1)); |
1134 | | |
1135 | 631k | do { |
1136 | 631k | c2 = (unsigned char) *s2++; |
1137 | 631k | } while (c2 != '\0' && !isalnum((unsigned int) c2)); |
1138 | | |
1139 | 545k | if (c1 != '\0') |
1140 | 544k | c1 = tolower(c1); |
1141 | 545k | if (c2 != '\0') |
1142 | 285k | c2 = tolower(c2); |
1143 | 545k | if (c1 == '\0') |
1144 | 420 | return c1 - c2; |
1145 | 545k | } while (c1 == c2); |
1146 | | |
1147 | 504k | return c1 - c2; |
1148 | 504k | } |
1149 | | |
1150 | | /* |
1151 | | * Parses the first option from @optstr. The @optstr pointer is set to the beginning |
1152 | | * of the next option. The options string looks like 'aaa,bbb=data,foo,bar="xxx"'. |
1153 | | * |
1154 | | * Note this function is used by libmount to parse mount options. Be careful when modify. |
1155 | | * |
1156 | | * Returns -EINVAL on parse error, 1 at the end of optstr and 0 on success. |
1157 | | */ |
1158 | | int ul_optstr_next(char **optstr, char **name, size_t *namesz, |
1159 | | char **value, size_t *valsz) |
1160 | 49.3k | { |
1161 | 49.3k | int open_quote = 0; |
1162 | 49.3k | char *start = NULL, *stop = NULL, *p, *sep = NULL; |
1163 | 49.3k | char *optstr0; |
1164 | | |
1165 | 49.3k | assert(optstr); |
1166 | 49.3k | assert(*optstr); |
1167 | | |
1168 | 49.3k | optstr0 = *optstr; |
1169 | | |
1170 | 49.3k | if (name) |
1171 | 49.3k | *name = NULL; |
1172 | 49.3k | if (namesz) |
1173 | 49.3k | *namesz = 0; |
1174 | 49.3k | if (value) |
1175 | 49.3k | *value = NULL; |
1176 | 49.3k | if (valsz) |
1177 | 49.3k | *valsz = 0; |
1178 | | |
1179 | | /* trim leading commas as to not invalidate option |
1180 | | * strings with multiple consecutive commas */ |
1181 | 49.9k | while (optstr0 && *optstr0 == ',') |
1182 | 605 | optstr0++; |
1183 | | |
1184 | 1.91M | for (p = optstr0; p && *p; p++) { |
1185 | 1.90M | if (!start) |
1186 | 34.7k | start = p; /* beginning of the option item */ |
1187 | 1.90M | if (*p == '"') |
1188 | 2.38k | open_quote ^= 1; /* reverse the status */ |
1189 | 1.90M | if (open_quote) |
1190 | 144k | continue; /* still in quoted block */ |
1191 | 1.75M | if (!sep && p > start && *p == '=') |
1192 | 2.98k | sep = p; /* name and value separator */ |
1193 | 1.75M | if (*p == ',') |
1194 | 16.7k | stop = p; /* terminate the option item */ |
1195 | 1.73M | else if (*(p + 1) == '\0') |
1196 | 16.0k | stop = p + 1; /* end of optstr */ |
1197 | 1.75M | if (!start || !stop) |
1198 | 1.72M | continue; |
1199 | 32.7k | if (stop <= start) |
1200 | 0 | return -EINVAL; |
1201 | | |
1202 | 32.7k | if (name) |
1203 | 32.7k | *name = start; |
1204 | 32.7k | if (namesz) |
1205 | 32.7k | *namesz = sep ? sep - start : stop - start; |
1206 | 32.7k | *optstr = *stop ? stop + 1 : stop; |
1207 | | |
1208 | 32.7k | if (sep) { |
1209 | 2.98k | if (value) |
1210 | 2.98k | *value = sep + 1; |
1211 | 2.98k | if (valsz) |
1212 | 2.98k | *valsz = stop - sep - 1; |
1213 | 2.98k | } |
1214 | 32.7k | return 0; |
1215 | 32.7k | } |
1216 | | |
1217 | 16.5k | return 1; /* end of optstr */ |
1218 | 49.3k | } |
1219 | | |
1220 | | #ifdef TEST_PROGRAM_STRUTILS |
1221 | | |
1222 | | #include "cctype.h" |
1223 | | |
1224 | | struct testS { |
1225 | | char *name; |
1226 | | char *value; |
1227 | | }; |
1228 | | |
1229 | | static int test_strdup_to_member(int argc, char *argv[]) |
1230 | | { |
1231 | | struct testS *xx; |
1232 | | |
1233 | | if (argc < 3) |
1234 | | return EXIT_FAILURE; |
1235 | | |
1236 | | xx = calloc(1, sizeof(*xx)); |
1237 | | if (!xx) |
1238 | | err(EXIT_FAILURE, "calloc() failed"); |
1239 | | |
1240 | | strdup_to_struct_member(xx, name, argv[1]); |
1241 | | strdup_to_struct_member(xx, value, argv[2]); |
1242 | | |
1243 | | if (strcmp(xx->name, argv[1]) != 0 && |
1244 | | strcmp(xx->value, argv[2]) != 0) |
1245 | | errx(EXIT_FAILURE, "strdup_to_struct_member() failed"); |
1246 | | |
1247 | | printf("1: '%s', 2: '%s'\n", xx->name, xx->value); |
1248 | | |
1249 | | free(xx->name); |
1250 | | free(xx->value); |
1251 | | free(xx); |
1252 | | return EXIT_SUCCESS; |
1253 | | } |
1254 | | |
1255 | | static int test_strutils_sizes(int argc, char *argv[]) |
1256 | | { |
1257 | | uintmax_t size = 0; |
1258 | | char *hum1, *hum2, *hum3; |
1259 | | |
1260 | | if (argc < 2) |
1261 | | return EXIT_FAILURE; |
1262 | | |
1263 | | if (strtosize(argv[1], &size)) |
1264 | | errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]); |
1265 | | |
1266 | | hum1 = size_to_human_string(SIZE_SUFFIX_1LETTER, size); |
1267 | | hum2 = size_to_human_string(SIZE_SUFFIX_3LETTER | |
1268 | | SIZE_SUFFIX_SPACE, size); |
1269 | | hum3 = size_to_human_string(SIZE_SUFFIX_3LETTER | |
1270 | | SIZE_SUFFIX_SPACE | |
1271 | | SIZE_DECIMAL_2DIGITS, size); |
1272 | | |
1273 | | printf("%25s : %20ju : %8s : %12s : %13s\n", argv[1], size, hum1, hum2, hum3); |
1274 | | free(hum1); |
1275 | | free(hum2); |
1276 | | free(hum3); |
1277 | | |
1278 | | return EXIT_SUCCESS; |
1279 | | } |
1280 | | |
1281 | | static int test_strutils_cmp_paths(int argc, char *argv[]) |
1282 | | { |
1283 | | int rc = streq_paths(argv[1], argv[2]); |
1284 | | |
1285 | | if (argc < 3) |
1286 | | return EXIT_FAILURE; |
1287 | | |
1288 | | printf("%s: '%s' '%s'\n", rc == 1 ? "YES" : "NOT", argv[1], argv[2]); |
1289 | | return EXIT_SUCCESS; |
1290 | | } |
1291 | | |
1292 | | static int test_strutils_normalize(int argc, char *argv[]) |
1293 | | { |
1294 | | unsigned char *src, *dst, *org; |
1295 | | size_t sz, len; |
1296 | | |
1297 | | if (argc < 2) |
1298 | | return EXIT_FAILURE; |
1299 | | |
1300 | | org = (unsigned char *) strdup(argv[1]); |
1301 | | src = (unsigned char *) strdup((char *) org); |
1302 | | len = strlen((char *) src); |
1303 | | dst = malloc(len + 1); |
1304 | | |
1305 | | if (!org || !src || !dst) |
1306 | | goto done; |
1307 | | |
1308 | | /* two buffers */ |
1309 | | sz = __normalize_whitespace(src, len, dst, len + 1); |
1310 | | printf("1: '%s' --> '%s' [sz=%zu]\n", src, dst, sz); |
1311 | | |
1312 | | /* one buffer */ |
1313 | | sz = normalize_whitespace(src); |
1314 | | printf("2: '%s' --> '%s' [sz=%zu]\n", org, src, sz); |
1315 | | |
1316 | | done: |
1317 | | free(src); |
1318 | | free(dst); |
1319 | | free(org); |
1320 | | |
1321 | | return EXIT_SUCCESS; |
1322 | | } |
1323 | | |
1324 | | static int test_strutils_cstrcasecmp(int argc, char *argv[]) |
1325 | | { |
1326 | | char *a, *b; |
1327 | | |
1328 | | if (argc < 3) |
1329 | | return EXIT_FAILURE; |
1330 | | |
1331 | | a = argv[1]; |
1332 | | b = argv[2]; |
1333 | | |
1334 | | if (!a || !b) |
1335 | | return EXIT_FAILURE; |
1336 | | |
1337 | | printf("cmp '%s' '%s' = %d\n", a, b, strcasecmp(a, b)); |
1338 | | printf("c_cmp '%s' '%s' = %d\n", a, b, c_strcasecmp(a, b)); |
1339 | | printf("c_ncmp '%s' '%s' = %d\n", a, b, c_strncasecmp(a, b, strlen(a))); |
1340 | | |
1341 | | return EXIT_SUCCESS; |
1342 | | } |
1343 | | |
1344 | | int main(int argc, char *argv[]) |
1345 | | { |
1346 | | if (argc == 3 && strcmp(argv[1], "--size") == 0) { |
1347 | | return test_strutils_sizes(argc - 1, argv + 1); |
1348 | | |
1349 | | } else if (argc == 4 && strcmp(argv[1], "--cmp-paths") == 0) { |
1350 | | return test_strutils_cmp_paths(argc - 1, argv + 1); |
1351 | | |
1352 | | } else if (argc == 4 && strcmp(argv[1], "--strdup-member") == 0) { |
1353 | | return test_strdup_to_member(argc - 1, argv + 1); |
1354 | | |
1355 | | } else if (argc == 4 && strcmp(argv[1], "--stralnumcmp") == 0) { |
1356 | | printf("%s\n", ul_stralnumcmp(argv[2], argv[3]) == 0 ? |
1357 | | "match" : "dismatch"); |
1358 | | return EXIT_SUCCESS; |
1359 | | |
1360 | | } else if (argc == 4 && strcmp(argv[1], "--cstrcasecmp") == 0) { |
1361 | | return test_strutils_cstrcasecmp(argc - 1, argv + 1); |
1362 | | |
1363 | | } else if (argc == 3 && strcmp(argv[1], "--normalize") == 0) { |
1364 | | return test_strutils_normalize(argc - 1, argv + 1); |
1365 | | |
1366 | | } else if (argc == 3 && strcmp(argv[1], "--strtos64") == 0) { |
1367 | | printf("'%s'-->%jd\n", argv[2], strtos64_or_err(argv[2], "strtos64 failed")); |
1368 | | return EXIT_SUCCESS; |
1369 | | } else if (argc == 3 && strcmp(argv[1], "--strtou64") == 0) { |
1370 | | printf("'%s'-->%ju\n", argv[2], strtou64_or_err(argv[2], "strtou64 failed")); |
1371 | | return EXIT_SUCCESS; |
1372 | | } else if (argc == 3 && strcmp(argv[1], "--strtos32") == 0) { |
1373 | | printf("'%s'-->%d\n", argv[2], strtos32_or_err(argv[2], "strtos32 failed")); |
1374 | | return EXIT_SUCCESS; |
1375 | | } else if (argc == 3 && strcmp(argv[1], "--strtou32") == 0) { |
1376 | | printf("'%s'-->%u\n", argv[2], strtou32_or_err(argv[2], "strtou32 failed")); |
1377 | | return EXIT_SUCCESS; |
1378 | | } else if (argc == 3 && strcmp(argv[1], "--strtos16") == 0) { |
1379 | | printf("'%s'-->%hd\n", argv[2], strtos16_or_err(argv[2], "strtos16 failed")); |
1380 | | return EXIT_SUCCESS; |
1381 | | } else if (argc == 3 && strcmp(argv[1], "--strtou16") == 0) { |
1382 | | printf("'%s'-->%hu\n", argv[2], strtou16_or_err(argv[2], "strtou16 failed")); |
1383 | | return EXIT_SUCCESS; |
1384 | | |
1385 | | } else if (argc == 4 && strcmp(argv[1], "--strchr-escaped") == 0) { |
1386 | | printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3])); |
1387 | | return EXIT_SUCCESS; |
1388 | | |
1389 | | } else { |
1390 | | fprintf(stderr, "usage: %1$s --size <number>[suffix]\n" |
1391 | | " %1$s --cmp-paths <path> <path>\n" |
1392 | | " %1$s --strdup-member <str> <str>\n" |
1393 | | " %1$s --stralnumcmp <str> <str>\n" |
1394 | | " %1$s --cstrcasecmp <str> <str>\n" |
1395 | | " %1$s --normalize <str>\n" |
1396 | | " %1$s --strto{s,u}{16,32,64} <str>\n", |
1397 | | argv[0]); |
1398 | | exit(EXIT_FAILURE); |
1399 | | } |
1400 | | |
1401 | | return EXIT_FAILURE; |
1402 | | } |
1403 | | #endif /* TEST_PROGRAM_STRUTILS */ |