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. Speed isn't critical here
25 * since we're parsing commandline args. So make sure we get the
26 * actual isspace function rather than a lnumstrer macro implementaion. */
27 if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
30 /* Since this is a lib function, we're not allowed to reset errno to 0.
31 * Doing so could break an app that is deferring checking of errno.
32 * So, save the old value so that we can restore it if successful. */
35 r = XSTR_STRTOU(numstr, &e, base);
36 /* Do the initial validity check. Note: The standards do not
37 * guarantee that errno is set if no digits were found. So we
38 * must test for this explicitly. */
39 if (errno || numstr == e)
40 goto inval; /* error / no digits / illegal trailing chars */
42 errno = old_errno; /* Ok. So restore errno. */
44 /* Do optional suffix parsing. Allow 'empty' suffix tables.
45 * Note that we also allow nul suffixes with associated multipliers,
46 * to allow for scaling of the numstr by some default multiplier. */
48 while (suffixes->mult) {
49 if (strcmp(suffixes->suffix, e) == 0) {
50 if (XSTR_UTYPE_MAX / suffixes->mult < r)
51 goto range; /* overflow! */
59 /* Note: trailing space is an error.
60 It would be easy enough to allow though if desired. */
64 /* Finally, check for range limits. */
65 if (r >= lower && r <= upper)
68 bb_error_msg_and_die("number %s is not in %llu..%llu range",
69 numstr, (unsigned long long)lower,
70 (unsigned long long)upper);
72 bb_error_msg_and_die("invalid number '%s'", numstr);
75 unsigned type xstrtou(_range)(const char *numstr, int base,
79 return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
82 unsigned type xstrtou(_sfx)(const char *numstr, int base,
83 const struct suffix_mult *suffixes)
85 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
88 unsigned type xstrtou()(const char *numstr, int base)
90 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
93 unsigned type xatou(_range_sfx)(const char *numstr,
96 const struct suffix_mult *suffixes)
98 return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
101 unsigned type xatou(_range)(const char *numstr,
105 return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
108 unsigned type xatou(_sfx)(const char *numstr,
109 const struct suffix_mult *suffixes)
111 return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
114 unsigned type xatou()(const char *numstr)
116 return xatou(_sfx)(numstr, NULL);
121 type xstrto(_range_sfx)(const char *numstr, int base,
124 const struct suffix_mult *suffixes)
126 unsigned type u = XSTR_TYPE_MAX;
128 const char *p = numstr;
132 ++u; /* two's complement */
135 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
137 if (*numstr == '-') {
141 if (r < lower || r > upper) {
142 bb_error_msg_and_die("number %s is not in %lld..%lld range",
143 numstr, (long long)lower, (long long)upper);
149 type xstrto(_range)(const char *numstr, int base, type lower, type upper)
151 return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
154 type xato(_range_sfx)(const char *numstr,
157 const struct suffix_mult *suffixes)
159 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
162 type xato(_range)(const char *numstr, type lower, type upper)
164 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
167 type xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
169 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
172 type xato()(const char *numstr)
174 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
182 #undef XSTR_UTYPE_MAX