2 You need to define the following (example):
5 #define xstrtou(rest) xstrtoul##rest
6 #define xstrto(rest) xstrtol##rest
7 #define xatou(rest) xatoul##rest
8 #define xato(rest) xatol##rest
9 #define XSTR_UTYPE_MAX ULONG_MAX
10 #define XSTR_TYPE_MAX LONG_MAX
11 #define XSTR_TYPE_MIN LONG_MIN
12 #define XSTR_STRTOU strtoul
15 unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
18 const struct suffix_mult *suffixes)
24 /* Disallow '-' and any leading whitespace. Make sure we get the
25 * actual isspace function rather than a macro implementaion. */
26 if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
29 /* Since this is a lib function, we're not allowed to reset errno to 0.
30 * Doing so could break an app that is deferring checking of errno.
31 * So, save the old value so that we can restore it if successful. */
34 r = XSTR_STRTOU(numstr, &e, base);
35 /* Do the initial validity check. Note: The standards do not
36 * guarantee that errno is set if no digits were found. So we
37 * must test for this explicitly. */
38 if (errno || numstr == e)
39 goto inval; /* error / no digits / illegal trailing chars */
41 errno = old_errno; /* Ok. So restore errno. */
43 /* Do optional suffix parsing. Allow 'empty' suffix tables.
44 * Note that we also allow nul suffixes with associated multipliers,
45 * to allow for scaling of the numstr by some default multiplier. */
47 while (suffixes->mult) {
48 if (strcmp(suffixes->suffix, e) == 0) {
49 if (XSTR_UTYPE_MAX / suffixes->mult < r)
50 goto range; /* overflow! */
58 /* Note: trailing space is an error.
59 It would be easy enough to allow though if desired. */
63 /* Finally, check for range limits. */
64 if (r >= lower && r <= upper)
67 bb_error_msg_and_die("number %s is not in %llu..%llu range",
68 numstr, (unsigned long long)lower,
69 (unsigned long long)upper);
71 bb_error_msg_and_die("invalid number '%s'", numstr);
74 unsigned type xstrtou(_range)(const char *numstr, int base,
78 return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
81 unsigned type xstrtou(_sfx)(const char *numstr, int base,
82 const struct suffix_mult *suffixes)
84 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
87 unsigned type xstrtou()(const char *numstr, int base)
89 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
92 unsigned type xatou(_range_sfx)(const char *numstr,
95 const struct suffix_mult *suffixes)
97 return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
100 unsigned type xatou(_range)(const char *numstr,
104 return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
107 unsigned type xatou(_sfx)(const char *numstr,
108 const struct suffix_mult *suffixes)
110 return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
113 unsigned type xatou()(const char *numstr)
115 return xatou(_sfx)(numstr, NULL);
120 type xstrto(_range_sfx)(const char *numstr, int base,
123 const struct suffix_mult *suffixes)
125 unsigned type u = XSTR_TYPE_MAX;
127 const char *p = numstr;
129 /* NB: if you'll decide to disallow '+':
130 * at least renice applet needs to allow it */
131 if (p[0] == '+' || p[0] == '-') {
134 ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
137 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
139 if (*numstr == '-') {
143 if (r < lower || r > upper) {
144 bb_error_msg_and_die("number %s is not in %lld..%lld range",
145 numstr, (long long)lower, (long long)upper);
151 type xstrto(_range)(const char *numstr, int base, type lower, type upper)
153 return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
156 type xato(_range_sfx)(const char *numstr,
159 const struct suffix_mult *suffixes)
161 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
164 type xato(_range)(const char *numstr, type lower, type upper)
166 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
169 type xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
171 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
174 type xato()(const char *numstr)
176 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
184 #undef XSTR_UTYPE_MAX