Tomato 1.28
[tomato.git] / release / src / router / busybox / libbb / xatonum_template.c
blob5e0bb59e6b1bbbd1198a48a0860c3158ede01691
1 /*
3 * Licensed under GPLv2, see file LICENSE in this tarball for details.
4 */
5 /*
6 You need to define the following (example):
8 #define type long
9 #define xstrtou(rest) xstrtoul##rest
10 #define xstrto(rest) xstrtol##rest
11 #define xatou(rest) xatoul##rest
12 #define xato(rest) xatol##rest
13 #define XSTR_UTYPE_MAX ULONG_MAX
14 #define XSTR_TYPE_MAX LONG_MAX
15 #define XSTR_TYPE_MIN LONG_MIN
16 #define XSTR_STRTOU strtoul
19 unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
20 unsigned type lower,
21 unsigned type upper,
22 const struct suffix_mult *suffixes)
24 unsigned type r;
25 int old_errno;
26 char *e;
28 /* Disallow '-' and any leading whitespace. Make sure we get the
29 * actual isspace function rather than a macro implementaion. */
30 if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
31 goto inval;
33 /* Since this is a lib function, we're not allowed to reset errno to 0.
34 * Doing so could break an app that is deferring checking of errno.
35 * So, save the old value so that we can restore it if successful. */
36 old_errno = errno;
37 errno = 0;
38 r = XSTR_STRTOU(numstr, &e, base);
39 /* Do the initial validity check. Note: The standards do not
40 * guarantee that errno is set if no digits were found. So we
41 * must test for this explicitly. */
42 if (errno || numstr == e)
43 goto inval; /* error / no digits / illegal trailing chars */
45 errno = old_errno; /* Ok. So restore errno. */
47 /* Do optional suffix parsing. Allow 'empty' suffix tables.
48 * Note that we also allow nul suffixes with associated multipliers,
49 * to allow for scaling of the numstr by some default multiplier. */
50 if (suffixes) {
51 while (suffixes->mult) {
52 if (strcmp(suffixes->suffix, e) == 0) {
53 if (XSTR_UTYPE_MAX / suffixes->mult < r)
54 goto range; /* overflow! */
55 r *= suffixes->mult;
56 goto chk_range;
58 ++suffixes;
62 /* Note: trailing space is an error.
63 It would be easy enough to allow though if desired. */
64 if (*e)
65 goto inval;
66 chk_range:
67 /* Finally, check for range limits. */
68 if (r >= lower && r <= upper)
69 return r;
70 range:
71 bb_error_msg_and_die("number %s is not in %llu..%llu range",
72 numstr, (unsigned long long)lower,
73 (unsigned long long)upper);
74 inval:
75 bb_error_msg_and_die("invalid number '%s'", numstr);
78 unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
79 unsigned type lower,
80 unsigned type upper)
82 return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
85 unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
86 const struct suffix_mult *suffixes)
88 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
91 unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
93 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
96 unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
97 unsigned type lower,
98 unsigned type upper,
99 const struct suffix_mult *suffixes)
101 return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
104 unsigned type FAST_FUNC xatou(_range)(const char *numstr,
105 unsigned type lower,
106 unsigned type upper)
108 return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
111 unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
112 const struct suffix_mult *suffixes)
114 return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
117 unsigned type FAST_FUNC xatou()(const char *numstr)
119 return xatou(_sfx)(numstr, NULL);
122 /* Signed ones */
124 type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
125 type lower,
126 type upper,
127 const struct suffix_mult *suffixes)
129 unsigned type u = XSTR_TYPE_MAX;
130 type r;
131 const char *p = numstr;
133 /* NB: if you'll decide to disallow '+':
134 * at least renice applet needs to allow it */
135 if (p[0] == '+' || p[0] == '-') {
136 ++p;
137 if (p[0] == '-')
138 ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
141 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
143 if (*numstr == '-') {
144 r = -r;
147 if (r < lower || r > upper) {
148 bb_error_msg_and_die("number %s is not in %lld..%lld range",
149 numstr, (long long)lower, (long long)upper);
152 return r;
155 type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
157 return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
160 type FAST_FUNC xato(_range_sfx)(const char *numstr,
161 type lower,
162 type upper,
163 const struct suffix_mult *suffixes)
165 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
168 type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
170 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
173 type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
175 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
178 type FAST_FUNC xato()(const char *numstr)
180 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
183 #undef type
184 #undef xstrtou
185 #undef xstrto
186 #undef xatou
187 #undef xato
188 #undef XSTR_UTYPE_MAX
189 #undef XSTR_TYPE_MAX
190 #undef XSTR_TYPE_MIN
191 #undef XSTR_STRTOU