Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 1 | /* |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 2 | * |
| 3 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 4 | */ |
| 5 | /* |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 6 | You 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 19 | unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 20 | 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 Vlasenko | 2cd4a74 | 2007-12-11 13:15:11 +0000 | [diff] [blame] | 28 | /* Disallow '-' and any leading whitespace. Make sure we get the |
| 29 | * actual isspace function rather than a macro implementaion. */ |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 30 | if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr)) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 31 | 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 Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 42 | if (errno || numstr == e) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 43 | 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 Vlasenko | 1bab1de | 2007-07-27 15:30:39 +0000 | [diff] [blame] | 51 | while (suffixes->mult) { |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 52 | if (strcmp(suffixes->suffix, e) == 0) { |
| 53 | if (XSTR_UTYPE_MAX / suffixes->mult < r) |
| 54 | goto range; /* overflow! */ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 55 | r *= suffixes->mult; |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 56 | goto chk_range; |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 57 | } |
| 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 Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 66 | chk_range: |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 67 | /* 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 78 | unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 79 | unsigned type lower, |
| 80 | unsigned type upper) |
| 81 | { |
| 82 | return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL); |
| 83 | } |
| 84 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 85 | unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 86 | const struct suffix_mult *suffixes) |
| 87 | { |
| 88 | return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes); |
| 89 | } |
| 90 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 91 | unsigned type FAST_FUNC xstrtou()(const char *numstr, int base) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 92 | { |
| 93 | return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL); |
| 94 | } |
| 95 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 96 | unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 97 | 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 104 | unsigned type FAST_FUNC xatou(_range)(const char *numstr, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 105 | unsigned type lower, |
| 106 | unsigned type upper) |
| 107 | { |
| 108 | return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL); |
| 109 | } |
| 110 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 111 | unsigned type FAST_FUNC xatou(_sfx)(const char *numstr, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 112 | const struct suffix_mult *suffixes) |
| 113 | { |
| 114 | return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes); |
| 115 | } |
| 116 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 117 | unsigned type FAST_FUNC xatou()(const char *numstr) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 118 | { |
| 119 | return xatou(_sfx)(numstr, NULL); |
| 120 | } |
| 121 | |
| 122 | /* Signed ones */ |
| 123 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 124 | type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 125 | 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 Vlasenko | 2cd4a74 | 2007-12-11 13:15:11 +0000 | [diff] [blame] | 133 | /* NB: if you'll decide to disallow '+': |
| 134 | * at least renice applet needs to allow it */ |
| 135 | if (p[0] == '+' || p[0] == '-') { |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 136 | ++p; |
Denis Vlasenko | 2cd4a74 | 2007-12-11 13:15:11 +0000 | [diff] [blame] | 137 | if (p[0] == '-') |
| 138 | ++u; /* = <type>_MIN (01111... + 1 == 10000...) */ |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 139 | } |
| 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 155 | type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 156 | { |
| 157 | return xstrto(_range_sfx)(numstr, base, lower, upper, NULL); |
| 158 | } |
| 159 | |
Denys Vlasenko | bf2af9a | 2009-05-25 04:15:37 +0200 | [diff] [blame] | 160 | type 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 165 | type FAST_FUNC xato(_range_sfx)(const char *numstr, |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 166 | 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 173 | type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 174 | { |
| 175 | return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL); |
| 176 | } |
| 177 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 178 | type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 179 | { |
| 180 | return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes); |
| 181 | } |
| 182 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 183 | type FAST_FUNC xato()(const char *numstr) |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 184 | { |
| 185 | return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL); |
| 186 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 187 | |
| 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 |