Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/bb_strtonum.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/libbb/bb_strtonum.c
r1765 r2725 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 6 * 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 8 */ 9 9 … … 11 11 12 12 /* On exit: errno = 0 only if there was non-empty, '\0' terminated value 13 * errno = EINVAL if value was not '\0' terminated, but other vise ok13 * errno = EINVAL if value was not '\0' terminated, but otherwise ok 14 14 * Return value is still valid, caller should just check whether end[0] 15 15 * is a valid terminating char for particular case. OTOH, if caller … … 19 19 * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok ) 20 20 * return value is all-ones in this case. 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); 21 31 */ 22 32 … … 30 40 { 31 41 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 42 39 43 /* errno is already set to ERANGE by strtoXXX if value overflowed */ … … 49 53 50 54 51 unsigned long long bb_strtoull(const char *arg, char **endp, int base)55 unsigned long long FAST_FUNC bb_strtoull(const char *arg, char **endp, int base) 52 56 { 53 57 unsigned long long v; … … 64 68 } 65 69 66 long long bb_strtoll(const char *arg, char **endp, int base)70 long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base) 67 71 { 68 72 unsigned long long v; 69 73 char *endptr; 70 74 71 if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); 75 /* Check for the weird "feature": 76 * a "-" string is apparently a valid "number" for strto[u]l[l]! 77 * It returns zero and errno is 0! :( */ 78 char first = (arg[0] != '-' ? arg[0] : arg[1]); 79 if (!isalnum(first)) return ret_ERANGE(); 80 72 81 errno = 0; 73 82 v = strtoll(arg, &endptr, base); … … 76 85 77 86 #if ULONG_MAX != ULLONG_MAX 78 unsigned long bb_strtoul(const char *arg, char **endp, int base)87 unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base) 79 88 { 80 89 unsigned long v; … … 87 96 } 88 97 89 long bb_strtol(const char *arg, char **endp, int base)98 long FAST_FUNC bb_strtol(const char *arg, char **endp, int base) 90 99 { 91 100 long v; 92 101 char *endptr; 93 102 94 if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); 103 char first = (arg[0] != '-' ? arg[0] : arg[1]); 104 if (!isalnum(first)) return ret_ERANGE(); 105 95 106 errno = 0; 96 107 v = strtol(arg, &endptr, base); … … 100 111 101 112 #if UINT_MAX != ULONG_MAX 102 unsigned bb_strtou(const char *arg, char **endp, int base)113 unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base) 103 114 { 104 115 unsigned long v; … … 112 123 } 113 124 114 int bb_strtoi(const char *arg, char **endp, int base)125 int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base) 115 126 { 116 127 long v; 117 128 char *endptr; 118 129 119 if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); 130 char first = (arg[0] != '-' ? arg[0] : arg[1]); 131 if (!isalnum(first)) return ret_ERANGE(); 132 120 133 errno = 0; 121 134 v = strtol(arg, &endptr, base); … … 125 138 } 126 139 #endif 127 128 /* Floating point */129 130 #if 0131 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
Note:
See TracChangeset
for help on using the changeset viewer.