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 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
| 11 | |
| 12 | /* On exit: errno = 0 only if there was non-empty, '\0' terminated value |
| 13 | * errno = EINVAL if value was not '\0' terminated, but othervise ok |
| 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 | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | static unsigned long long ret_ERANGE(void) |
| 24 | { |
| 25 | errno = ERANGE; /* this ain't as small as it looks (on glibc) */ |
| 26 | return ULLONG_MAX; |
| 27 | } |
| 28 | |
| 29 | static unsigned long long handle_errors(unsigned long long v, char **endp, char *endptr) |
| 30 | { |
| 31 | if (endp) *endp = endptr; |
| 32 | |
| 33 | /* Check for the weird "feature": |
| 34 | * a "-" string is apparently a valid "number" for strto[u]l[l]! |
| 35 | * It returns zero and errno is 0! :( */ |
| 36 | if (endptr[-1] == '-') |
| 37 | return ret_ERANGE(); |
| 38 | |
| 39 | /* errno is already set to ERANGE by strtoXXX if value overflowed */ |
| 40 | if (endptr[0]) { |
| 41 | /* "1234abcg" or out-of-range? */ |
| 42 | if (isalnum(endptr[0]) || errno) |
| 43 | return ret_ERANGE(); |
| 44 | /* good number, just suspicious terminator */ |
| 45 | errno = EINVAL; |
| 46 | } |
| 47 | return v; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | unsigned long long bb_strtoull(const char *arg, char **endp, int base) |
| 52 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 53 | unsigned long long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 54 | char *endptr; |
| 55 | |
| 56 | /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */ |
| 57 | /* I don't think that this is right. Preventing this... */ |
| 58 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 59 | |
| 60 | /* not 100% correct for lib func, but convenient for the caller */ |
| 61 | errno = 0; |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 62 | v = strtoull(arg, &endptr, base); |
| 63 | return handle_errors(v, endp, endptr); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | long long bb_strtoll(const char *arg, char **endp, int base) |
| 67 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 68 | unsigned long long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 69 | char *endptr; |
| 70 | |
| 71 | if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); |
| 72 | errno = 0; |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 73 | v = strtoll(arg, &endptr, base); |
| 74 | return handle_errors(v, endp, endptr); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | #if ULONG_MAX != ULLONG_MAX |
| 78 | unsigned long bb_strtoul(const char *arg, char **endp, int base) |
| 79 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 80 | unsigned long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 81 | char *endptr; |
| 82 | |
| 83 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 84 | errno = 0; |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 85 | v = strtoul(arg, &endptr, base); |
| 86 | return handle_errors(v, endp, endptr); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | long bb_strtol(const char *arg, char **endp, int base) |
| 90 | { |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 91 | long v; |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 92 | char *endptr; |
| 93 | |
| 94 | if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); |
| 95 | errno = 0; |
Denis Vlasenko | df38188 | 2006-11-28 10:54:16 +0000 | [diff] [blame] | 96 | v = strtol(arg, &endptr, base); |
| 97 | return handle_errors(v, endp, endptr); |
Denis Vlasenko | 3ece72d | 2006-11-27 15:12:16 +0000 | [diff] [blame] | 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #if UINT_MAX != ULONG_MAX |
| 102 | unsigned bb_strtou(const char *arg, char **endp, int base) |
| 103 | { |
| 104 | unsigned long v; |
| 105 | char *endptr; |
| 106 | |
| 107 | if (!isalnum(arg[0])) return ret_ERANGE(); |
| 108 | errno = 0; |
| 109 | v = strtoul(arg, &endptr, base); |
| 110 | if (v > UINT_MAX) return ret_ERANGE(); |
| 111 | return handle_errors(v, endp, endptr); |
| 112 | } |
| 113 | |
| 114 | int bb_strtoi(const char *arg, char **endp, int base) |
| 115 | { |
| 116 | long v; |
| 117 | char *endptr; |
| 118 | |
| 119 | if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); |
| 120 | errno = 0; |
| 121 | v = strtol(arg, &endptr, base); |
| 122 | if (v > INT_MAX) return ret_ERANGE(); |
| 123 | if (v < INT_MIN) return ret_ERANGE(); |
| 124 | return handle_errors(v, endp, endptr); |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | /* Floating point */ |
| 129 | |
| 130 | #if 0 |
| 131 | |
| 132 | #include <math.h> /* just for HUGE_VAL */ |
| 133 | #define NOT_DIGIT(a) (((unsigned char)(a-'0')) > 9) |
| 134 | double bb_strtod(const char *arg, char **endp) |
| 135 | { |
| 136 | double v; |
| 137 | char *endptr; |
| 138 | |
| 139 | if (arg[0] != '-' && NOT_DIGIT(arg[0])) goto err; |
| 140 | errno = 0; |
| 141 | v = strtod(arg, &endptr); |
| 142 | if (endp) *endp = endptr; |
| 143 | if (endptr[0]) { |
| 144 | /* "1234abcg" or out-of-range? */ |
| 145 | if (isalnum(endptr[0]) || errno) { |
| 146 | err: |
| 147 | errno = ERANGE; |
| 148 | return HUGE_VAL; |
| 149 | } |
| 150 | /* good number, just suspicious terminator */ |
| 151 | errno = EINVAL; |
| 152 | } |
| 153 | return v; |
| 154 | } |
| 155 | |
| 156 | #endif |