blob: c97a4b795fe82b38d2b6836746d6634034f02105 [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
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020028 /* Disallow '-' and any leading whitespace. */
29 if (*numstr == '-' || *numstr == '+' || isspace(*numstr))
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000030 goto inval;
31
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. */
35 old_errno = errno;
36 errno = 0;
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. */
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000041 if (errno || numstr == e)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000042 goto inval; /* error / no digits / illegal trailing chars */
43
44 errno = old_errno; /* Ok. So restore errno. */
45
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. */
49 if (suffixes) {
Denis Vlasenko1bab1de2007-07-27 15:30:39 +000050 while (suffixes->mult) {
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000051 if (strcmp(suffixes->suffix, e) == 0) {
52 if (XSTR_UTYPE_MAX / suffixes->mult < r)
53 goto range; /* overflow! */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000054 r *= suffixes->mult;
Denis Vlasenko53a0e972007-06-16 00:30:52 +000055 goto chk_range;
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000056 }
57 ++suffixes;
58 }
59 }
60
61 /* Note: trailing space is an error.
62 It would be easy enough to allow though if desired. */
63 if (*e)
64 goto inval;
Denis Vlasenko53a0e972007-06-16 00:30:52 +000065 chk_range:
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000066 /* Finally, check for range limits. */
67 if (r >= lower && r <= upper)
68 return r;
69 range:
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);
73 inval:
74 bb_error_msg_and_die("invalid number '%s'", numstr);
75}
76
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000077unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000078 unsigned type lower,
79 unsigned type upper)
80{
81 return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
82}
83
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000084unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000085 const struct suffix_mult *suffixes)
86{
87 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
88}
89
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000090unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000091{
92 return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
93}
94
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000095unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000096 unsigned type lower,
97 unsigned type upper,
98 const struct suffix_mult *suffixes)
99{
100 return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
101}
102
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000103unsigned type FAST_FUNC xatou(_range)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000104 unsigned type lower,
105 unsigned type upper)
106{
107 return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
108}
109
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000110unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000111 const struct suffix_mult *suffixes)
112{
113 return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
114}
115
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000116unsigned type FAST_FUNC xatou()(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000117{
118 return xatou(_sfx)(numstr, NULL);
119}
120
121/* Signed ones */
122
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000123type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000124 type lower,
125 type upper,
126 const struct suffix_mult *suffixes)
127{
128 unsigned type u = XSTR_TYPE_MAX;
129 type r;
130 const char *p = numstr;
131
Denis Vlasenko2cd4a742007-12-11 13:15:11 +0000132 /* NB: if you'll decide to disallow '+':
133 * at least renice applet needs to allow it */
134 if (p[0] == '+' || p[0] == '-') {
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000135 ++p;
Denis Vlasenko2cd4a742007-12-11 13:15:11 +0000136 if (p[0] == '-')
137 ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000138 }
139
140 r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
141
142 if (*numstr == '-') {
143 r = -r;
144 }
145
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);
149 }
150
151 return r;
152}
153
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000154type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000155{
156 return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
157}
158
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200159type FAST_FUNC xstrto()(const char *numstr, int base)
160{
161 return xstrto(_range_sfx)(numstr, base, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
162}
163
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000164type FAST_FUNC xato(_range_sfx)(const char *numstr,
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000165 type lower,
166 type upper,
167 const struct suffix_mult *suffixes)
168{
169 return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
170}
171
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000172type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000173{
174 return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
175}
176
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000177type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000178{
179 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
180}
181
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000182type FAST_FUNC xato()(const char *numstr)
Denis Vlasenkoed836cd2006-11-25 14:44:13 +0000183{
184 return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
185}
Denis Vlasenkod686a042006-11-27 14:43:21 +0000186
187#undef type
188#undef xstrtou
189#undef xstrto
190#undef xatou
191#undef xato
192#undef XSTR_UTYPE_MAX
193#undef XSTR_TYPE_MAX
194#undef XSTR_TYPE_MIN
195#undef XSTR_STRTOU