3 * Licensed under GPLv2, see file LICENSE in this source tree.
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. */
29 if (*numstr
== '-' || *numstr
== '+' || isspace(*numstr
))
32 /* Since this is a lib function, we're not allowed to reset errno to 0.
33 * Doing so could break an app that is deferring checking of errno.
34 * So, save the old value so that we can restore it if successful. */
37 r
= XSTR_STRTOU(numstr
, &e
, base
);
38 /* Do the initial validity check. Note: The standards do not
39 * guarantee that errno is set if no digits were found. So we
40 * must test for this explicitly. */
41 if (errno
|| numstr
== e
)
42 goto inval
; /* error / no digits / illegal trailing chars */
44 errno
= old_errno
; /* Ok. So restore errno. */
46 /* Do optional suffix parsing. Allow 'empty' suffix tables.
47 * Note that we also allow nul suffixes with associated multipliers,
48 * to allow for scaling of the numstr by some default multiplier. */
50 while (suffixes
->mult
) {
51 if (strcmp(suffixes
->suffix
, e
) == 0) {
52 if (XSTR_UTYPE_MAX
/ suffixes
->mult
< r
)
53 goto range
; /* overflow! */
61 /* Note: trailing space is an error.
62 * It would be easy enough to allow though if desired. */
66 /* Finally, check for range limits. */
67 if (r
>= lower
&& r
<= upper
)
70 bb_error_msg_and_die("number %s is not in %llu..%llu range",
71 numstr
, (unsigned long long)lower
,
72 (unsigned long long)upper
);
74 bb_error_msg_and_die("invalid number '%s'", numstr
);
77 unsigned type FAST_FUNC
xstrtou(_range
)(const char *numstr
, int base
,
81 return xstrtou(_range_sfx
)(numstr
, base
, lower
, upper
, NULL
);
84 unsigned type FAST_FUNC
xstrtou(_sfx
)(const char *numstr
, int base
,
85 const struct suffix_mult
*suffixes
)
87 return xstrtou(_range_sfx
)(numstr
, base
, 0, XSTR_UTYPE_MAX
, suffixes
);
90 unsigned type FAST_FUNC
xstrtou()(const char *numstr
, int base
)
92 return xstrtou(_range_sfx
)(numstr
, base
, 0, XSTR_UTYPE_MAX
, NULL
);
95 unsigned type FAST_FUNC
xatou(_range_sfx
)(const char *numstr
,
98 const struct suffix_mult
*suffixes
)
100 return xstrtou(_range_sfx
)(numstr
, 10, lower
, upper
, suffixes
);
103 unsigned type FAST_FUNC
xatou(_range
)(const char *numstr
,
107 return xstrtou(_range_sfx
)(numstr
, 10, lower
, upper
, NULL
);
110 unsigned type FAST_FUNC
xatou(_sfx
)(const char *numstr
,
111 const struct suffix_mult
*suffixes
)
113 return xstrtou(_range_sfx
)(numstr
, 10, 0, XSTR_UTYPE_MAX
, suffixes
);
116 unsigned type FAST_FUNC
xatou()(const char *numstr
)
118 return xatou(_sfx
)(numstr
, NULL
);
123 type FAST_FUNC
xstrto(_range_sfx
)(const char *numstr
, int base
,
126 const struct suffix_mult
*suffixes
)
128 unsigned type u
= XSTR_TYPE_MAX
;
130 const char *p
= numstr
;
132 /* NB: if you'll decide to disallow '+':
133 * at least renice applet needs to allow it */
134 if (p
[0] == '+' || p
[0] == '-') {
137 ++u
; /* = <type>_MIN (01111... + 1 == 10000...) */
140 r
= xstrtou(_range_sfx
)(p
, base
, 0, u
, suffixes
);
142 if (*numstr
== '-') {
146 if (r
< lower
|| r
> upper
) {
147 bb_error_msg_and_die("number %s is not in %lld..%lld range",
148 numstr
, (long long)lower
, (long long)upper
);
154 type FAST_FUNC
xstrto(_range
)(const char *numstr
, int base
, type lower
, type upper
)
156 return xstrto(_range_sfx
)(numstr
, base
, lower
, upper
, NULL
);
159 type FAST_FUNC
xstrto()(const char *numstr
, int base
)
161 return xstrto(_range_sfx
)(numstr
, base
, XSTR_TYPE_MIN
, XSTR_TYPE_MAX
, NULL
);
164 type FAST_FUNC
xato(_range_sfx
)(const char *numstr
,
167 const struct suffix_mult
*suffixes
)
169 return xstrto(_range_sfx
)(numstr
, 10, lower
, upper
, suffixes
);
172 type FAST_FUNC
xato(_range
)(const char *numstr
, type lower
, type upper
)
174 return xstrto(_range_sfx
)(numstr
, 10, lower
, upper
, NULL
);
177 type FAST_FUNC
xato(_sfx
)(const char *numstr
, const struct suffix_mult
*suffixes
)
179 return xstrto(_range_sfx
)(numstr
, 10, XSTR_TYPE_MIN
, XSTR_TYPE_MAX
, suffixes
);
182 type FAST_FUNC
xato()(const char *numstr
)
184 return xstrto(_range_sfx
)(numstr
, 10, XSTR_TYPE_MIN
, XSTR_TYPE_MAX
, NULL
);
192 #undef XSTR_UTYPE_MAX