dd: make it recognize not only 'k' but 'K' too;
[oweals/busybox.git] / libbb / xgetularg.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * xgetularg* implementations for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include "libbb.h"
17
18 unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
19                                                                    unsigned long lower,
20                                                                    unsigned long upper,
21                                                                    const struct suffix_mult *suffixes)
22 {
23         unsigned long r;
24         int old_errno;
25         char *e;
26
27         assert(arg);
28
29         /* Disallow '-' and any leading whitespace.  Speed isn't critical here
30          * since we're parsing commandline args.  So make sure we get the
31          * actual isspace function rather than a larger macro implementaion. */
32         if ((*arg == '-') || (isspace)(*arg)) {
33                 bb_show_usage();
34         }
35
36         /* Since this is a lib function, we're not allowed to reset errno to 0.
37          * Doing so could break an app that is deferring checking of errno.
38          * So, save the old value so that we can restore it if successful. */
39         old_errno = errno;
40         errno = 0;
41         r = strtoul(arg, &e, base);
42         /* Do the initial validity check.  Note: The standards do not
43          * guarantee that errno is set if no digits were found.  So we
44          * must test for this explicitly. */
45         if (errno || (arg == e)) {      /* error or no digits */
46                 bb_show_usage();
47         }
48         errno = old_errno;      /* Ok.  So restore errno. */
49
50         /* Do optional suffix parsing.  Allow 'empty' suffix tables.
51          * Note that we also all nul suffixes with associated multipliers,
52          * to allow for scaling of the arg by some default multiplier. */
53
54         if (suffixes) {
55                 while (suffixes->suffix) {
56                         if (strcmp(suffixes->suffix, e) == 0) {
57                                 if (ULONG_MAX / suffixes->mult < r) {   /* Overflow! */
58                                         bb_show_usage();
59                                 }
60                                 ++e;
61                                 r *= suffixes->mult;
62                                 break;
63                         }
64                         ++suffixes;
65                 }
66         }
67
68         /* Finally, check for illegal trailing chars and range limits. */
69         /* Note: although we allow leading space (via stroul), trailing space
70          * is an error.  It would be easy enough to allow though if desired. */
71         if (*e || (r < lower) || (r > upper)) {
72                 bb_show_usage();
73         }
74
75         return r;
76 }
77
78 long bb_xgetlarg_bnd_sfx(const char *arg, int base,
79                                                  long lower,
80                                                  long upper,
81                                                  const struct suffix_mult *suffixes)
82 {
83         unsigned long u = LONG_MAX;
84         long r;
85         const char *p = arg;
86
87         if ((*p == '-') && (p[1] != '+')) {
88                 ++p;
89                 ++u;    /* two's complement */
90         }
91
92         r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
93
94         if (*arg == '-') {
95                 r = -r;
96         }
97
98         if ((r < lower) || (r > upper)) {
99                 bb_show_usage();
100         }
101
102         return r;
103 }
104
105 long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
106 {
107         return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
108 }
109
110 unsigned long bb_xgetularg_bnd(const char *arg, int base,
111                                                            unsigned long lower,
112                                                            unsigned long upper)
113 {
114         return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
115 }
116
117 unsigned long bb_xgetularg10_bnd(const char *arg,
118                                                                  unsigned long lower,
119                                                                  unsigned long upper)
120 {
121         return bb_xgetularg_bnd(arg, 10, lower, upper);
122 }
123
124 unsigned long bb_xgetularg10(const char *arg)
125 {
126         return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
127 }