3 * Licensed under GPLv2, see file LICENSE in this tarball for details.
6 You need to define the following (example):
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,
22 const struct suffix_mult *suffixes)
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))
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. */
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. */
51 while (suffixes->mult) {
52 if (strcmp(suffixes->suffix, e) == 0) {
53 if (XSTR_UTYPE_MAX / suffixes->mult < r)
54 goto range; /* overflow! */
62 /* Note: trailing space is an error.
63 It would be easy enough to allow though if desired. */
67 /* Finally, check for range limits. */
68 if (r >= lower && r <= upper)
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);
75 bb_error_msg_and_die("invalid number '%s'", numstr);
78 unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
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,
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,
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);
124 type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
127 const struct suffix_mult *suffixes)
129 unsigned type u = XSTR_TYPE_MAX;
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] == '-') {
138 ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
141 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
143 if (*numstr == '-') {
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);
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 xstrto()(const char *numstr, int base)
162 return xstrto(_range_sfx)(numstr, base, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
165 type FAST_FUNC xato(_range_sfx)(const char *numstr,
168 const struct suffix_mult *suffixes)
170 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
173 type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
175 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
178 type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
180 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
183 type FAST_FUNC xato()(const char *numstr)
185 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
193 #undef XSTR_UTYPE_MAX