blob: 339a7d35fdcd845ba6524fdefc1ad0d568ac6288 [file] [log] [blame]
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00001/*
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00002 *
3 * Licensed under GPLv2, see file LICENSE in this tarball for details.
4 */
5/*
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00006You need to define the following (example):
7
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
17*/
18
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000019unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000020 unsigned type lower,
21 unsigned type upper,
22 const struct suffix_mult *suffixes)
23{
24 unsigned type r;
25 int old_errno;
26 char *e;
27
Denis Vlasenko2cd4a742007-12-11 13:15:11 +000028 /* Disallow '-' and any leading whitespace. Make sure we get the
29 * actual isspace function rather than a macro implementaion. */
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000030 if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000031 goto inval;
32
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. */
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000042 if (errno || numstr == e)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000043 goto inval; /* error / no digits / illegal trailing chars */
44
45 errno = old_errno; /* Ok. So restore errno. */
46
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) {
Denis Vlasenko1bab1de2007-07-27 15:30:39 +000051 while (suffixes->mult) {
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000052 if (strcmp(suffixes->suffix, e) == 0) {
53 if (XSTR_UTYPE_MAX / suffixes->mult < r)
54 goto range; /* overflow! */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000055 r *= suffixes->mult;
Denis Vlasenko53a0e972007-06-16 00:30:52 +000056 goto chk_range;
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000057 }
58 ++suffixes;
59 }
60 }
61
62 /* Note: trailing space is an error.
63 It would be easy enough to allow though if desired. */
64 if (*e)
65 goto inval;
Denis Vlasenko53a0e972007-06-16 00:30:52 +000066 chk_range:
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000067 /* 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);
76}
77
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000078unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000079 unsigned type lower,
80 unsigned type upper)
81{
82 return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
83}
84
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000085unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000086 const struct suffix_mult *suffixes)
87{
88 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
89}
90
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000091unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000092{
93 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
94}
95
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000096unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000097 unsigned type lower,
98 unsigned type upper,
99 const struct suffix_mult *suffixes)
100{
101 return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
102}
103
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000104unsigned type FAST_FUNC xatou(_range)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000105 unsigned type lower,
106 unsigned type upper)
107{
108 return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
109}
110
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000111unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000112 const struct suffix_mult *suffixes)
113{
114 return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
115}
116
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000117unsigned type FAST_FUNC xatou()(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000118{
119 return xatou(_sfx)(numstr, NULL);
120}
121
122/* Signed ones */
123
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000124type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000125 type lower,
126 type upper,
127 const struct suffix_mult *suffixes)
128{
129 unsigned type u = XSTR_TYPE_MAX;
130 type r;
131 const char *p = numstr;
132
Denis Vlasenko2cd4a742007-12-11 13:15:11 +0000133 /* NB: if you'll decide to disallow '+':
134 * at least renice applet needs to allow it */
135 if (p[0] == '+' || p[0] == '-') {
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000136 ++p;
Denis Vlasenko2cd4a742007-12-11 13:15:11 +0000137 if (p[0] == '-')
138 ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000139 }
140
141 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
142
143 if (*numstr == '-') {
144 r = -r;
145 }
146
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);
150 }
151
152 return r;
153}
154
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000155type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000156{
157 return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
158}
159
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200160type FAST_FUNC xstrto()(const char *numstr, int base)
161{
162 return xstrto(_range_sfx)(numstr, base, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
163}
164
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000165type FAST_FUNC xato(_range_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000166 type lower,
167 type upper,
168 const struct suffix_mult *suffixes)
169{
170 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
171}
172
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000173type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000174{
175 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
176}
177
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000178type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000179{
180 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
181}
182
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000183type FAST_FUNC xato()(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000184{
185 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
186}
Denis Vlasenkod686a042006-11-27 14:43:21 +0000187
188#undef type
189#undef xstrtou
190#undef xstrto
191#undef xatou
192#undef xato
193#undef XSTR_UTYPE_MAX
194#undef XSTR_TYPE_MAX
195#undef XSTR_TYPE_MIN
196#undef XSTR_STRTOU