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