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