1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/*
* Copyright (C) 2010 Karel Zak <kzak@redhat.com>
* Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include "c.h"
#include "nls.h"
#include "strutils.h"
#include "bitops.h"
static int do_scale_by_power (uintmax_t *x, int base, int power)
{
while (power--) {
if (UINTMAX_MAX / base < *x)
return -2;
*x *= base;
}
return 0;
}
/*
* strtosize() - convert string to size (uintmax_t).
*
* Supported suffixes:
*
* XiB or X for 2^N
* where X = {K,M,G,T,P,E,Z,Y}
* or X = {k,m,g,t,p,e} (undocumented for backward compatibility only)
* for example:
* 10KiB = 10240
* 10K = 10240
*
* XB for 10^N
* where X = {K,M,G,T,P,E,Z,Y}
* for example:
* 10KB = 10000
*
* The optinal 'power' variable returns number associated with used suffix
* {K,M,G,T,P,E,Z,Y} = {1,2,3,4,5,6,7,8}.
*
* Note that the function does not accept numbers with '-' (negative sign)
* prefix.
*/
int parse_size(const char *str, uintmax_t *res, int *power)
{
char *p;
uintmax_t x;
int base = 1024, rc = 0, pwr = 0;
static const char *suf = "KMGTPEYZ";
static const char *suf2 = "kmgtpeyz";
const char *sp;
*res = 0;
if (!str || !*str)
goto err;
/* Only positive numbers are acceptable
*
* Note that this check is not perfect, it would be better to
* use lconv->negative_sign. But coreutils use the same solution,
* so it's probably good enough...
*/
p = (char *) str;
while (isspace((unsigned char) *p))
p++;
if (*p == '-')
goto err;
p = NULL;
errno = 0;
x = strtoumax(str, &p, 0);
if (p == str ||
(errno != 0 && (x == UINTMAX_MAX || x == 0)))
goto err;
if (!p || !*p)
goto done; /* without suffix */
/*
* Check size suffixes
*/
if (*(p + 1) == 'i' && *(p + 2) == 'B' && !*(p + 3))
base = 1024; /* XiB, 2^N */
else if (*(p + 1) == 'B' && !*(p + 2))
base = 1000; /* XB, 10^N */
else if (*(p + 1))
goto err; /* unexpected suffix */
sp = strchr(suf, *p);
if (sp)
pwr = (sp - suf) + 1;
else {
sp = strchr(suf2, *p);
if (sp)
pwr = (sp - suf2) + 1;
else
goto err;
}
rc = do_scale_by_power(&x, base, pwr);
if (power)
*power = pwr;
done:
*res = x;
return rc;
err:
return -1;
}
int strtosize(const char *str, uintmax_t *res)
{
return parse_size(str, res, NULL);
}
int isdigit_string(const char *str)
{
const char *p;
for (p = str; p && *p && isdigit((unsigned char) *p); p++);
return p && p > str && !*p;
}
#ifndef HAVE_MEMPCPY
void *mempcpy(void *restrict dest, const void *restrict src, size_t n)
{
return ((char *)memcpy(dest, src, n)) + n;
}
#endif
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t maxlen)
{
int i;
for (i = 0; i < maxlen; i++) {
if (s[i] == '\0')
return i + 1;
}
return maxlen;
}
#endif
#ifndef HAVE_STRNCHR
char *strnchr(const char *s, size_t maxlen, int c)
{
for (; maxlen-- && *s != '\0'; ++s)
if (*s == (char)c)
return (char *)s;
return NULL;
}
#endif
#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n)
{
size_t len = strnlen(s, n);
char *new = (char *) malloc((len + 1) * sizeof(char));
if (!new)
return NULL;
new[len] = '\0';
return (char *) memcpy(new, s, len);
}
#endif
int16_t strtos16_or_err(const char *str, const char *errmesg)
{
int32_t num = strtos32_or_err(str, errmesg);
if (num < INT16_MIN || num > INT16_MAX)
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
return num;
}
uint16_t strtou16_or_err(const char *str, const char *errmesg)
{
uint32_t num = strtou32_or_err(str, errmesg);
if (num > UINT16_MAX)
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
return num;
}
int32_t strtos32_or_err(const char *str, const char *errmesg)
{
int64_t num = strtos64_or_err(str, errmesg);
if (num < INT32_MIN || num > INT32_MAX)
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
return num;
}
uint32_t strtou32_or_err(const char *str, const char *errmesg)
{
uint64_t num = strtou64_or_err(str, errmesg);
if (num > UINT32_MAX)
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
return num;
}
int64_t strtos64_or_err(const char *str, const char *errmesg)
{
int64_t num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtoimax(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
uint64_t strtou64_or_err(const char *str, const char *errmesg)
{
uintmax_t num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtoumax(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
double strtod_or_err(const char *str, const char *errmesg)
{
double num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtod(str, &end);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
long strtol_or_err(const char *str, const char *errmesg)
{
long num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtol(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
unsigned long strtoul_or_err(const char *str, const char *errmesg)
{
unsigned long num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtoul(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
uintmax_t strtosize_or_err(const char *str, const char *errmesg)
{
uintmax_t num;
if (strtosize(str, &num) == 0)
return num;
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
void strtotimeval_or_err(const char *str, struct timeval *tv, const char *errmesg)
{
double user_input;
user_input = strtod_or_err(str, errmesg);
tv->tv_sec = (time_t) user_input;
tv->tv_usec = (long)((user_input - tv->tv_sec) * 1000000);
}
/*
* Converts stat->st_mode to ls(1)-like mode string. The size of "str" must
* be 10 bytes.
*/
void strmode(mode_t mode, char *str)
{
if (S_ISDIR(mode))
str[0] = 'd';
else if (S_ISLNK(mode))
str[0] = 'l';
else if (S_ISCHR(mode))
str[0] = 'c';
else if (S_ISBLK(mode))
str[0] = 'b';
else if (S_ISSOCK(mode))
str[0] = 's';
else if (S_ISFIFO(mode))
str[0] = 'p';
else if (S_ISREG(mode))
str[0] = '-';
str[1] = mode & S_IRUSR ? 'r' : '-';
str[2] = mode & S_IWUSR ? 'w' : '-';
str[3] = (mode & S_ISUID
? (mode & S_IXUSR ? 's' : 'S')
: (mode & S_IXUSR ? 'x' : '-'));
str[4] = mode & S_IRGRP ? 'r' : '-';
str[5] = mode & S_IWGRP ? 'w' : '-';
str[6] = (mode & S_ISGID
? (mode & S_IXGRP ? 's' : 'S')
: (mode & S_IXGRP ? 'x' : '-'));
str[7] = mode & S_IROTH ? 'r' : '-';
str[8] = mode & S_IWOTH ? 'w' : '-';
str[9] = (mode & S_ISVTX
? (mode & S_IXOTH ? 't' : 'T')
: (mode & S_IXOTH ? 'x' : '-'));
str[10] = '\0';
}
/*
* returns exponent (2^x=n) in range KiB..PiB
*/
static int get_exp(uint64_t n)
{
int shft;
for (shft = 10; shft <= 60; shft += 10) {
if (n < (1ULL << shft))
break;
}
return shft - 10;
}
char *size_to_human_string(int options, uint64_t bytes)
{
char buf[32];
int dec, exp;
uint64_t frac;
const char *letters = "BKMGTPE";
char suffix[sizeof(" KiB")], *psuf = suffix;
char c;
if (options & SIZE_SUFFIX_SPACE)
*psuf++ = ' ';
exp = get_exp(bytes);
c = *(letters + (exp ? exp / 10 : 0));
dec = exp ? bytes / (1ULL << exp) : bytes;
frac = exp ? bytes % (1ULL << exp) : 0;
*psuf++ = c;
if ((options & SIZE_SUFFIX_3LETTER) && (c != 'B')) {
*psuf++ = 'i';
*psuf++ = 'B';
}
*psuf = '\0';
/* fprintf(stderr, "exp: %d, unit: %c, dec: %d, frac: %jd\n",
* exp, suffix[0], dec, frac);
*/
if (frac) {
/* round */
frac = (frac / (1ULL << (exp - 10)) + 50) / 100;
if (frac == 10)
dec++, frac = 0;
}
if (frac) {
struct lconv const *l = localeconv();
char *dp = l ? l->decimal_point : NULL;
if (!dp || !*dp)
dp = ".";
snprintf(buf, sizeof(buf), "%d%s%jd%s", dec, dp, frac, suffix);
} else
snprintf(buf, sizeof(buf), "%d%s", dec, suffix);
return strdup(buf);
}
/*
* Parses comma delimited list to array with IDs, for example:
*
* "aaa,bbb,ccc" --> ary[0] = FOO_AAA;
* ary[1] = FOO_BBB;
* ary[3] = FOO_CCC;
*
* The function name2id() provides conversion from string to ID.
*
* Returns: >= 0 : number of items added to ary[]
* -1 : parse error or unknown item
* -2 : arysz reached
*/
int string_to_idarray(const char *list, int ary[], size_t arysz,
int (name2id)(const char *, size_t))
{
const char *begin = NULL, *p;
size_t n = 0;
if (!list || !*list || !ary || !arysz || !name2id)
return -1;
for (p = list; p && *p; p++) {
const char *end = NULL;
int id;
if (n >= arysz)
return -2;
if (!begin)
begin = p; /* begin of the column name */
if (*p == ',')
end = p; /* terminate the name */
if (*(p + 1) == '\0')
end = p + 1; /* end of string */
if (!begin || !end)
continue;
if (end <= begin)
return -1;
id = name2id(begin, end - begin);
if (id == -1)
return -1;
ary[ n++ ] = id;
begin = NULL;
if (end && !*end)
break;
}
return n;
}
/*
* Parses the array like string_to_idarray but if format is "+aaa,bbb"
* it adds fields to array instead of replacing them.
*/
int string_add_to_idarray(const char *list, int ary[], size_t arysz,
int *ary_pos, int (name2id)(const char *, size_t))
{
const char *list_add;
int r;
if (!list || !*list || !ary_pos ||
*ary_pos < 0 || (size_t) *ary_pos > arysz)
return -1;
if (list[0] == '+')
list_add = &list[1];
else {
list_add = list;
*ary_pos = 0;
}
r = string_to_idarray(list_add, &ary[*ary_pos], arysz - *ary_pos, name2id);
if (r > 0)
*ary_pos += r;
return r;
}
/*
* LIST ::= <item> [, <item>]
*
* The <item> is translated to 'id' by name2id() function and the 'id' is used
* as a position in the 'ary' bit array. It means that the 'id' has to be in
* range <0..N> where N < sizeof(ary) * NBBY.
*
* Returns: 0 on success, <0 on error.
*/
int string_to_bitarray(const char *list,
char *ary,
int (*name2bit)(const char *, size_t))
{
const char *begin = NULL, *p;
if (!list || !name2bit || !ary)
return -EINVAL;
for (p = list; p && *p; p++) {
const char *end = NULL;
int bit;
if (!begin)
begin = p; /* begin of the level name */
if (*p == ',')
end = p; /* terminate the name */
if (*(p + 1) == '\0')
end = p + 1; /* end of string */
if (!begin || !end)
continue;
if (end <= begin)
return -1;
bit = name2bit(begin, end - begin);
if (bit < 0)
return bit;
setbit(ary, bit);
begin = NULL;
if (end && !*end)
break;
}
return 0;
}
/*
* LIST ::= <item> [, <item>]
*
* The <item> is translated to 'id' by name2flag() function and the flags is
* set to the 'mask'
*
* Returns: 0 on success, <0 on error.
*/
int string_to_bitmask(const char *list,
unsigned long *mask,
long (*name2flag)(const char *, size_t))
{
const char *begin = NULL, *p;
if (!list || !name2flag || !mask)
return -EINVAL;
for (p = list; p && *p; p++) {
const char *end = NULL;
long flag;
if (!begin)
begin = p; /* begin of the level name */
if (*p == ',')
end = p; /* terminate the name */
if (*(p + 1) == '\0')
end = p + 1; /* end of string */
if (!begin || !end)
continue;
if (end <= begin)
return -1;
flag = name2flag(begin, end - begin);
if (flag < 0)
return flag; /* error */
*mask |= flag;
begin = NULL;
if (end && !*end)
break;
}
return 0;
}
/*
* Parse the lower and higher values in a string containing
* "lower:higher" or "lower-higher" format. Note that either
* the lower or the higher values may be missing, and the def
* value will be assigned to it by default.
*
* Returns: 0 on success, <0 on error.
*/
int parse_range(const char *str, int *lower, int *upper, int def)
{
char *end = NULL;
if (!str)
return 0;
*upper = *lower = def;
errno = 0;
if (*str == ':') { /* <:N> */
str++;
*upper = strtol(str, &end, 10);
if (errno || !end || *end || end == str)
return -1;
} else {
*upper = *lower = strtol(str, &end, 10);
if (errno || !end || end == str)
return -1;
if (*end == ':' && !*(end + 1)) /* <M:> */
*upper = 0;
else if (*end == '-' || *end == ':') { /* <M:N> <M-N> */
str = end + 1;
end = NULL;
errno = 0;
*upper = strtol(str, &end, 10);
if (errno || !end || *end || end == str)
return -1;
}
}
return 0;
}
/*
* Compare two strings for equality, ignoring at most one trailing
* slash.
*/
int streq_except_trailing_slash(const char *s1, const char *s2)
{
int equal;
if (!s1 && !s2)
return 1;
if (!s1 || !s2)
return 0;
equal = !strcmp(s1, s2);
if (!equal) {
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
if (len1 && *(s1 + len1 - 1) == '/')
len1--;
if (len2 && *(s2 + len2 - 1) == '/')
len2--;
if (len1 != len2)
return 0;
equal = !strncmp(s1, s2, len1);
}
return equal;
}
#ifdef TEST_PROGRAM
int main(int argc, char *argv[])
{
uintmax_t size = 0;
char *hum, *hum2;
if (argc < 2) {
fprintf(stderr, "usage: %s <number>[suffix]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (strtosize(argv[1], &size))
errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]);
hum = size_to_human_string(SIZE_SUFFIX_1LETTER, size);
hum2 = size_to_human_string(SIZE_SUFFIX_3LETTER |
SIZE_SUFFIX_SPACE, size);
printf("%25s : %20ju : %8s : %12s\n", argv[1], size, hum, hum2);
free(hum);
free(hum2);
return EXIT_SUCCESS;
}
#endif /* TEST_PROGRAM */
|