Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* strtol - convert string representation of a number into a long integer value. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | Bash is distributed in the hope that it will be useful, |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 20 | |
| 21 | #include <config.h> |
| 22 | |
| 23 | #if !defined (HAVE_STRTOL) |
| 24 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 25 | #include <chartypes.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | |
| 28 | #ifndef errno |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #ifndef __set_errno |
| 33 | # define __set_errno(Val) errno = (Val) |
| 34 | #endif |
| 35 | |
| 36 | #ifdef HAVE_LIMITS_H |
| 37 | # include <limits.h> |
| 38 | #endif |
| 39 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 40 | #include <typemax.h> |
| 41 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 42 | #include <stdc.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 43 | #include <bashansi.h> |
| 44 | |
| 45 | #ifndef NULL |
| 46 | # define NULL 0 |
| 47 | #endif |
| 48 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 49 | /* Nonzero if we are defining `strtoul' or `strtoull', operating on |
| 50 | unsigned integers. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 51 | #ifndef UNSIGNED |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 52 | # define UNSIGNED 0 |
| 53 | # define INT LONG int |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 54 | #else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 55 | # define INT unsigned LONG int |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 58 | #if UNSIGNED |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 59 | # ifdef QUAD |
| 60 | # define strtol strtoull |
| 61 | # else |
| 62 | # define strtol strtoul |
| 63 | # endif |
| 64 | #else |
| 65 | # ifdef QUAD |
| 66 | # define strtol strtoll |
| 67 | # endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 70 | /* If QUAD is defined, we are defining `strtoll' or `strtoull', |
| 71 | operating on `long long ints. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 72 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 73 | #ifdef QUAD |
| 74 | # define LONG long long |
| 75 | # define STRTOL_LONG_MIN LLONG_MIN |
| 76 | # define STRTOL_LONG_MAX LLONG_MAX |
| 77 | # define STRTOL_ULONG_MAX ULLONG_MAX |
| 78 | #else /* !QUAD */ |
| 79 | # define LONG long |
| 80 | # define STRTOL_LONG_MIN LONG_MIN |
| 81 | # define STRTOL_LONG_MAX LONG_MAX |
| 82 | # define STRTOL_ULONG_MAX ULONG_MAX |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | /* Convert NPTR to an `unsigned long int' or `long int' in base BASE. |
| 86 | If BASE is 0 the base is determined by the presence of a leading |
| 87 | zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal. |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 88 | If BASE is < 2 or > 36, it is no longer reset to 10; EINVAL is returned. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 89 | If ENDPTR is not NULL, a pointer to the character after the last |
| 90 | one converted is stored in *ENDPTR. */ |
| 91 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 92 | INT |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 93 | strtol (nptr, endptr, base) |
| 94 | const char *nptr; |
| 95 | char **endptr; |
| 96 | int base; |
| 97 | { |
| 98 | int negative; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 99 | register unsigned LONG int cutoff; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 100 | register unsigned int cutlim; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 101 | register unsigned LONG int i; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 102 | register const char *s; |
| 103 | register unsigned char c; |
| 104 | const char *save, *end; |
| 105 | int overflow; |
| 106 | |
| 107 | if (base < 0 || base == 1 || base > 36) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 108 | { |
| 109 | __set_errno (EINVAL); |
| 110 | return 0; |
| 111 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 112 | |
| 113 | save = s = nptr; |
| 114 | |
| 115 | /* Skip white space. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 116 | while (ISSPACE ((unsigned char)*s)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 117 | ++s; |
| 118 | if (*s == '\0') |
| 119 | goto noconv; |
| 120 | |
| 121 | /* Check for a sign. */ |
| 122 | if (*s == '-' || *s == '+') |
| 123 | { |
| 124 | negative = (*s == '-'); |
| 125 | ++s; |
| 126 | } |
| 127 | else |
| 128 | negative = 0; |
| 129 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 130 | /* Recognize number prefix and if BASE is zero, figure it out ourselves. */ |
| 131 | if (*s == '0') |
| 132 | { |
| 133 | if ((base == 0 || base == 16) && TOUPPER ((unsigned char) s[1]) == 'X') |
| 134 | { |
| 135 | s += 2; |
| 136 | base = 16; |
| 137 | } |
| 138 | else if (base == 0) |
| 139 | base = 8; |
| 140 | } |
| 141 | else if (base == 0) |
| 142 | base = 10; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 143 | |
| 144 | /* Save the pointer so we can check later if anything happened. */ |
| 145 | save = s; |
| 146 | |
| 147 | end = NULL; |
| 148 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 149 | cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base; |
| 150 | cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 151 | |
| 152 | overflow = 0; |
| 153 | i = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 154 | c = *s; |
| 155 | if (sizeof (long int) != sizeof (LONG int)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 156 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 157 | unsigned long int j = 0; |
| 158 | unsigned long int jmax = ULONG_MAX / base; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 159 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 160 | for (;c != '\0'; c = *++s) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 161 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 162 | if (s == end) |
| 163 | break; |
| 164 | if (DIGIT (c)) |
| 165 | c -= '0'; |
| 166 | else if (ISALPHA (c)) |
| 167 | c = TOUPPER (c) - 'A' + 10; |
| 168 | else |
| 169 | break; |
| 170 | |
| 171 | if ((int) c >= base) |
| 172 | break; |
| 173 | /* Note that we never can have an overflow. */ |
| 174 | else if (j >= jmax) |
| 175 | { |
| 176 | /* We have an overflow. Now use the long representation. */ |
| 177 | i = (unsigned LONG int) j; |
| 178 | goto use_long; |
| 179 | } |
| 180 | else |
| 181 | j = j * (unsigned long int) base + c; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 182 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 183 | |
| 184 | i = (unsigned LONG int) j; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 185 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 186 | else |
| 187 | for (;c != '\0'; c = *++s) |
| 188 | { |
| 189 | if (s == end) |
| 190 | break; |
| 191 | if (DIGIT (c)) |
| 192 | c -= '0'; |
| 193 | else if (ISALPHA (c)) |
| 194 | c = TOUPPER (c) - 'A' + 10; |
| 195 | else |
| 196 | break; |
| 197 | if ((int) c >= base) |
| 198 | break; |
| 199 | /* Check for overflow. */ |
| 200 | if (i > cutoff || (i == cutoff && c > cutlim)) |
| 201 | overflow = 1; |
| 202 | else |
| 203 | { |
| 204 | use_long: |
| 205 | i *= (unsigned LONG int) base; |
| 206 | i += c; |
| 207 | } |
| 208 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 209 | |
| 210 | /* Check if anything actually happened. */ |
| 211 | if (s == save) |
| 212 | goto noconv; |
| 213 | |
| 214 | /* Store in ENDPTR the address of one character |
| 215 | past the last character we converted. */ |
| 216 | if (endptr != NULL) |
| 217 | *endptr = (char *) s; |
| 218 | |
| 219 | #if !UNSIGNED |
| 220 | /* Check for a value that is within the range of |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 221 | `unsigned LONG int', but outside the range of `LONG int'. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 222 | if (overflow == 0 |
| 223 | && i > (negative |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 224 | ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1 |
| 225 | : (unsigned LONG int) STRTOL_LONG_MAX)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 226 | overflow = 1; |
| 227 | #endif |
| 228 | |
| 229 | if (overflow) |
| 230 | { |
| 231 | __set_errno (ERANGE); |
| 232 | #if UNSIGNED |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 233 | return STRTOL_ULONG_MAX; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 234 | #else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 235 | return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 236 | #endif |
| 237 | } |
| 238 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 239 | /* Return the result of the appropriate sign. */ |
| 240 | return negative ? -i : i; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 241 | |
| 242 | noconv: |
| 243 | /* We must handle a special case here: the base is 0 or 16 and the |
| 244 | first two characters are '0' and 'x', but the rest are no |
| 245 | hexadecimal digits. This is no error case. We return 0 and |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 246 | ENDPTR points to the `x`. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 247 | if (endptr != NULL) |
| 248 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 249 | if (save - nptr >= 2 && TOUPPER ((unsigned char) save[-1]) == 'X' && save[-2] == '0') |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 250 | *endptr = (char *) &save[-1]; |
| 251 | else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 252 | /* There was no number to convert. */ |
| 253 | *endptr = (char *) nptr; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return 0L; |
| 257 | } |
| 258 | |
| 259 | #endif /* !HAVE_STRTOL */ |