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