Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
| 11 | /* On exit: errno = 0 only if there was non-empty, '\0' terminated value |
Denis Vlasenko | 49a5eba | 2008-07-23 08:41:08 +0000 | [diff] [blame] | 12 | * errno = EINVAL if value was not '\0' terminated, but otherwise ok |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 13 | * Return value is still valid, caller should just check whether end[0] |
| 14 | * is a valid terminating char for particular case. OTOH, if caller |
| 15 | * requires '\0' terminated input, [s]he can just check errno == 0. |
| 16 | * errno = ERANGE if value had alphanumeric terminating char ("1234abcg"). |
| 17 | * errno = ERANGE if value is out of range, missing, etc. |
| 18 | * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok ) |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 19 | * return value is all-ones in this case. |
Denis Vlasenko | f19817d | 2008-07-18 18:17:10 +0000 | [diff] [blame] | 20 | * |
| 21 | * Test code: |
| 22 | * char *endptr; |
| 23 | * const char *minus = "-"; |
| 24 | * errno = 0; |
| 25 | * bb_strtoi(minus, &endptr, 0); // must set ERANGE |
| 26 | * printf("minus:%p endptr:%p errno:%d EINVAL:%d\n", minus, endptr, errno, EINVAL); |
| 27 | * errno = 0; |
| 28 | * bb_strtoi("-0-", &endptr, 0); // must set EINVAL and point to second '-' |
| 29 | * printf("endptr[0]:%c errno:%d EINVAL:%d\n", endptr[0], errno, EINVAL); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | static unsigned long long ret_ERANGE(void) |
| 33 | { |
| 34 | errno = ERANGE; /* this ain't as small as it looks (on glibc) */ |
| 35 | return ULLONG_MAX; |
| 36 | } |
| 37 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 38 | static unsigned long long handle_errors(unsigned long long v, char **endp) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 39 | { |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 40 | char next_ch = **endp; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 42 | /* errno is already set to ERANGE by strtoXXX if value overflowed */ |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 43 | if (next_ch) { |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 44 | /* "1234abcg" or out-of-range? */ |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 45 | if (isalnum(next_ch) || errno) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 46 | return ret_ERANGE(); |
| 47 | /* good number, just suspicious terminator */ |
| 48 | errno = EINVAL; |
| 49 | } |
| 50 | return v; |
| 51 | } |
| 52 | |
| 53 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 54 | unsigned long long FAST_FUNC bb_strtoull(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 56 | unsigned long long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 57 | char *endptr; |
| 58 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 59 | if (!endp) endp = &endptr; |
| 60 | *endp = (char*) arg; |
| 61 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 62 | /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */ |
| 63 | /* I don't think that this is right. Preventing this... */ |
| 64 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 65 | |
| 66 | /* not 100% correct for lib func, but convenient for the caller */ |
| 67 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 68 | v = strtoull(arg, endp, base); |
| 69 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 72 | long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 73 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 74 | unsigned long long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 75 | char *endptr; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 76 | char first; |
| 77 | |
| 78 | if (!endp) endp = &endptr; |
| 79 | *endp = (char*) arg; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | f19817d | 2008-07-18 18:17:10 +0000 | [diff] [blame] | 81 | /* Check for the weird "feature": |
| 82 | * a "-" string is apparently a valid "number" for strto[u]l[l]! |
| 83 | * It returns zero and errno is 0! :( */ |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 84 | first = (arg[0] != '-' ? arg[0] : arg[1]); |
Denis Vlasenko | f19817d | 2008-07-18 18:17:10 +0000 | [diff] [blame] | 85 | if (!isalnum(first)) return ret_ERANGE(); |
| 86 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 87 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 88 | v = strtoll(arg, endp, base); |
| 89 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #if ULONG_MAX != ULLONG_MAX |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 93 | unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 94 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 95 | unsigned long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 96 | char *endptr; |
| 97 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 98 | if (!endp) endp = &endptr; |
| 99 | *endp = (char*) arg; |
| 100 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 101 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 102 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 103 | v = strtoul(arg, endp, base); |
| 104 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 107 | long FAST_FUNC bb_strtol(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 109 | long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 110 | char *endptr; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 111 | char first; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 112 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 113 | if (!endp) endp = &endptr; |
| 114 | *endp = (char*) arg; |
| 115 | |
| 116 | first = (arg[0] != '-' ? arg[0] : arg[1]); |
Denis Vlasenko | f19817d | 2008-07-18 18:17:10 +0000 | [diff] [blame] | 117 | if (!isalnum(first)) return ret_ERANGE(); |
| 118 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 119 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 120 | v = strtol(arg, endp, base); |
| 121 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 122 | } |
| 123 | #endif |
| 124 | |
| 125 | #if UINT_MAX != ULONG_MAX |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 126 | unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 127 | { |
| 128 | unsigned long v; |
| 129 | char *endptr; |
| 130 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 131 | if (!endp) endp = &endptr; |
| 132 | *endp = (char*) arg; |
| 133 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 134 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 135 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 136 | v = strtoul(arg, endp, base); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 137 | if (v > UINT_MAX) return ret_ERANGE(); |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 138 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 141 | int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base) |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 142 | { |
| 143 | long v; |
| 144 | char *endptr; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 145 | char first; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 146 | |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 147 | if (!endp) endp = &endptr; |
| 148 | *endp = (char*) arg; |
| 149 | |
| 150 | first = (arg[0] != '-' ? arg[0] : arg[1]); |
Denis Vlasenko | f19817d | 2008-07-18 18:17:10 +0000 | [diff] [blame] | 151 | if (!isalnum(first)) return ret_ERANGE(); |
| 152 | |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 153 | errno = 0; |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 154 | v = strtol(arg, endp, base); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 155 | if (v > INT_MAX) return ret_ERANGE(); |
| 156 | if (v < INT_MIN) return ret_ERANGE(); |
Denys Vlasenko | ba65872 | 2011-12-22 13:00:32 +0100 | [diff] [blame] | 157 | return handle_errors(v, endp); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 158 | } |
| 159 | #endif |