1 1.1 christos /* Convert string representation of a number into an integer value. 2 1.1 christos 3 1.1 christos Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2005, 2006 4 1.1 christos Free Software Foundation, Inc. 5 1.1 christos 6 1.1 christos NOTE: The canonical source of this file is maintained with the GNU C 7 1.1 christos Library. Bugs can be reported to bug-glibc (at) gnu.org. 8 1.1 christos 9 1.1 christos This program is free software; you can redistribute it and/or modify it 10 1.1 christos under the terms of the GNU General Public License as published by the 11 1.1 christos Free Software Foundation; either version 2, or (at your option) any 12 1.1 christos later version. 13 1.1 christos 14 1.1 christos This program is distributed in the hope that it will be useful, 15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 1.1 christos GNU General Public License for more details. 18 1.1 christos 19 1.1 christos You should have received a copy of the GNU General Public License 20 1.1 christos along with this program; if not, write to the Free Software Foundation, 21 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 1.1 christos 23 1.1 christos #ifdef _LIBC 24 1.1 christos # define USE_NUMBER_GROUPING 25 1.1 christos #else 26 1.1 christos # include <config.h> 27 1.1 christos #endif 28 1.1 christos 29 1.1 christos #include <ctype.h> 30 1.1 christos #include <errno.h> 31 1.1 christos #ifndef __set_errno 32 1.1 christos # define __set_errno(Val) errno = (Val) 33 1.1 christos #endif 34 1.1 christos 35 1.1 christos #include <limits.h> 36 1.1 christos #include <stddef.h> 37 1.1 christos #include <stdlib.h> 38 1.1 christos #include <string.h> 39 1.1 christos 40 1.1 christos #ifdef USE_NUMBER_GROUPING 41 1.1 christos # include "../locale/localeinfo.h" 42 1.1 christos #endif 43 1.1 christos 44 1.1 christos /* Nonzero if we are defining `strtoul' or `strtoull', operating on 45 1.1 christos unsigned integers. */ 46 1.1 christos #ifndef UNSIGNED 47 1.1 christos # define UNSIGNED 0 48 1.1 christos # define INT LONG int 49 1.1 christos #else 50 1.1 christos # define INT unsigned LONG int 51 1.1 christos #endif 52 1.1 christos 53 1.1 christos /* Determine the name. */ 54 1.1 christos #ifdef USE_IN_EXTENDED_LOCALE_MODEL 55 1.1 christos # if UNSIGNED 56 1.1 christos # ifdef USE_WIDE_CHAR 57 1.1 christos # ifdef QUAD 58 1.1 christos # define strtol __wcstoull_l 59 1.1 christos # else 60 1.1 christos # define strtol __wcstoul_l 61 1.1 christos # endif 62 1.1 christos # else 63 1.1 christos # ifdef QUAD 64 1.1 christos # define strtol __strtoull_l 65 1.1 christos # else 66 1.1 christos # define strtol __strtoul_l 67 1.1 christos # endif 68 1.1 christos # endif 69 1.1 christos # else 70 1.1 christos # ifdef USE_WIDE_CHAR 71 1.1 christos # ifdef QUAD 72 1.1 christos # define strtol __wcstoll_l 73 1.1 christos # else 74 1.1 christos # define strtol __wcstol_l 75 1.1 christos # endif 76 1.1 christos # else 77 1.1 christos # ifdef QUAD 78 1.1 christos # define strtol __strtoll_l 79 1.1 christos # else 80 1.1 christos # define strtol __strtol_l 81 1.1 christos # endif 82 1.1 christos # endif 83 1.1 christos # endif 84 1.1 christos #else 85 1.1 christos # if UNSIGNED 86 1.1 christos # ifdef USE_WIDE_CHAR 87 1.1 christos # ifdef QUAD 88 1.1 christos # define strtol wcstoull 89 1.1 christos # else 90 1.1 christos # define strtol wcstoul 91 1.1 christos # endif 92 1.1 christos # else 93 1.1 christos # ifdef QUAD 94 1.1 christos # define strtol strtoull 95 1.1 christos # else 96 1.1 christos # define strtol strtoul 97 1.1 christos # endif 98 1.1 christos # endif 99 1.1 christos # else 100 1.1 christos # ifdef USE_WIDE_CHAR 101 1.1 christos # ifdef QUAD 102 1.1 christos # define strtol wcstoll 103 1.1 christos # else 104 1.1 christos # define strtol wcstol 105 1.1 christos # endif 106 1.1 christos # else 107 1.1 christos # ifdef QUAD 108 1.1 christos # define strtol strtoll 109 1.1 christos # endif 110 1.1 christos # endif 111 1.1 christos # endif 112 1.1 christos #endif 113 1.1 christos 114 1.1 christos /* If QUAD is defined, we are defining `strtoll' or `strtoull', 115 1.1 christos operating on `long long int's. */ 116 1.1 christos #ifdef QUAD 117 1.1 christos # define LONG long long 118 1.1 christos # define STRTOL_LONG_MIN LONG_LONG_MIN 119 1.1 christos # define STRTOL_LONG_MAX LONG_LONG_MAX 120 1.1 christos # define STRTOL_ULONG_MAX ULONG_LONG_MAX 121 1.1 christos 122 1.1 christos /* The extra casts in the following macros work around compiler bugs, 123 1.1 christos e.g., in Cray C 5.0.3.0. */ 124 1.1 christos 125 1.1 christos /* True if negative values of the signed integer type T use two's 126 1.1 christos complement, ones' complement, or signed magnitude representation, 127 1.1 christos respectively. Much GNU code assumes two's complement, but some 128 1.1 christos people like to be portable to all possible C hosts. */ 129 1.1 christos # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1) 130 1.1 christos # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0) 131 1.1 christos # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1) 132 1.1 christos 133 1.1 christos /* True if the arithmetic type T is signed. */ 134 1.1 christos # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 135 1.1 christos 136 1.1 christos /* The maximum and minimum values for the integer type T. These 137 1.1 christos macros have undefined behavior if T is signed and has padding bits. 138 1.1 christos If this is a problem for you, please let us know how to fix it for 139 1.1 christos your host. */ 140 1.1 christos # define TYPE_MINIMUM(t) \ 141 1.1 christos ((t) (! TYPE_SIGNED (t) \ 142 1.1 christos ? (t) 0 \ 143 1.1 christos : TYPE_SIGNED_MAGNITUDE (t) \ 144 1.1 christos ? ~ (t) 0 \ 145 1.1 christos : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))) 146 1.1 christos # define TYPE_MAXIMUM(t) \ 147 1.1 christos ((t) (! TYPE_SIGNED (t) \ 148 1.1 christos ? (t) -1 \ 149 1.1 christos : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))) 150 1.1 christos 151 1.1 christos # ifndef ULONG_LONG_MAX 152 1.1 christos # define ULONG_LONG_MAX TYPE_MAXIMUM (unsigned long long) 153 1.1 christos # endif 154 1.1 christos # ifndef LONG_LONG_MAX 155 1.1 christos # define LONG_LONG_MAX TYPE_MAXIMUM (long long int) 156 1.1 christos # endif 157 1.1 christos # ifndef LONG_LONG_MIN 158 1.1 christos # define LONG_LONG_MIN TYPE_MINIMUM (long long int) 159 1.1 christos # endif 160 1.1 christos 161 1.1 christos # if __GNUC__ == 2 && __GNUC_MINOR__ < 7 162 1.1 christos /* Work around gcc bug with using this constant. */ 163 1.1 christos static const unsigned long long int maxquad = ULONG_LONG_MAX; 164 1.1 christos # undef STRTOL_ULONG_MAX 165 1.1 christos # define STRTOL_ULONG_MAX maxquad 166 1.1 christos # endif 167 1.1 christos #else 168 1.1 christos # define LONG long 169 1.1 christos # define STRTOL_LONG_MIN LONG_MIN 170 1.1 christos # define STRTOL_LONG_MAX LONG_MAX 171 1.1 christos # define STRTOL_ULONG_MAX ULONG_MAX 172 1.1 christos #endif 173 1.1 christos 174 1.1 christos 175 1.1 christos /* We use this code also for the extended locale handling where the 176 1.1 christos function gets as an additional argument the locale which has to be 177 1.1 christos used. To access the values we have to redefine the _NL_CURRENT 178 1.1 christos macro. */ 179 1.1 christos #ifdef USE_IN_EXTENDED_LOCALE_MODEL 180 1.1 christos # undef _NL_CURRENT 181 1.1 christos # define _NL_CURRENT(category, item) \ 182 1.1 christos (current->values[_NL_ITEM_INDEX (item)].string) 183 1.1 christos # define LOCALE_PARAM , loc 184 1.1 christos # define LOCALE_PARAM_PROTO , __locale_t loc 185 1.1 christos #else 186 1.1 christos # define LOCALE_PARAM 187 1.1 christos # define LOCALE_PARAM_PROTO 188 1.1 christos #endif 189 1.1 christos 190 1.1 christos #if defined _LIBC || defined HAVE_WCHAR_H 191 1.1 christos # include <wchar.h> 192 1.1 christos #endif 193 1.1 christos 194 1.1 christos #ifdef USE_WIDE_CHAR 195 1.1 christos # include <wctype.h> 196 1.1 christos # define L_(Ch) L##Ch 197 1.1 christos # define UCHAR_TYPE wint_t 198 1.1 christos # define STRING_TYPE wchar_t 199 1.1 christos # ifdef USE_IN_EXTENDED_LOCALE_MODEL 200 1.1 christos # define ISSPACE(Ch) __iswspace_l ((Ch), loc) 201 1.1 christos # define ISALPHA(Ch) __iswalpha_l ((Ch), loc) 202 1.1 christos # define TOUPPER(Ch) __towupper_l ((Ch), loc) 203 1.1 christos # else 204 1.1 christos # define ISSPACE(Ch) iswspace (Ch) 205 1.1 christos # define ISALPHA(Ch) iswalpha (Ch) 206 1.1 christos # define TOUPPER(Ch) towupper (Ch) 207 1.1 christos # endif 208 1.1 christos #else 209 1.1 christos # define L_(Ch) Ch 210 1.1 christos # define UCHAR_TYPE unsigned char 211 1.1 christos # define STRING_TYPE char 212 1.1 christos # ifdef USE_IN_EXTENDED_LOCALE_MODEL 213 1.1 christos # define ISSPACE(Ch) __isspace_l ((Ch), loc) 214 1.1 christos # define ISALPHA(Ch) __isalpha_l ((Ch), loc) 215 1.1 christos # define TOUPPER(Ch) __toupper_l ((Ch), loc) 216 1.1 christos # else 217 1.1 christos # define ISSPACE(Ch) isspace (Ch) 218 1.1 christos # define ISALPHA(Ch) isalpha (Ch) 219 1.1 christos # define TOUPPER(Ch) toupper (Ch) 220 1.1 christos # endif 221 1.1 christos #endif 222 1.1 christos 223 1.1 christos #define INTERNAL(X) INTERNAL1(X) 224 1.1 christos #define INTERNAL1(X) __##X##_internal 225 1.1 christos #define WEAKNAME(X) WEAKNAME1(X) 226 1.1 christos 227 1.1 christos #ifdef USE_NUMBER_GROUPING 228 1.1 christos /* This file defines a function to check for correct grouping. */ 229 1.1 christos # include "grouping.h" 230 1.1 christos #endif 231 1.1 christos 232 1.1 christos 233 1.1 christos 234 1.1 christos /* Convert NPTR to an `unsigned long int' or `long int' in base BASE. 235 1.1 christos If BASE is 0 the base is determined by the presence of a leading 236 1.1 christos zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal. 237 1.1 christos If BASE is < 2 or > 36, it is reset to 10. 238 1.1 christos If ENDPTR is not NULL, a pointer to the character after the last 239 1.1 christos one converted is stored in *ENDPTR. */ 240 1.1 christos 241 1.1 christos INT 242 1.1 christos INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr, 243 1.1 christos int base, int group LOCALE_PARAM_PROTO) 244 1.1 christos { 245 1.1 christos int negative; 246 1.1 christos register unsigned LONG int cutoff; 247 1.1 christos register unsigned int cutlim; 248 1.1 christos register unsigned LONG int i; 249 1.1 christos register const STRING_TYPE *s; 250 1.1 christos register UCHAR_TYPE c; 251 1.1 christos const STRING_TYPE *save, *end; 252 1.1 christos int overflow; 253 1.1 christos 254 1.1 christos #ifdef USE_NUMBER_GROUPING 255 1.1 christos # ifdef USE_IN_EXTENDED_LOCALE_MODEL 256 1.1 christos struct locale_data *current = loc->__locales[LC_NUMERIC]; 257 1.1 christos # endif 258 1.1 christos /* The thousands character of the current locale. */ 259 1.1 christos wchar_t thousands = L'\0'; 260 1.1 christos /* The numeric grouping specification of the current locale, 261 1.1 christos in the format described in <locale.h>. */ 262 1.1 christos const char *grouping; 263 1.1 christos 264 1.1 christos if (group) 265 1.1 christos { 266 1.1 christos grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); 267 1.1 christos if (*grouping <= 0 || *grouping == CHAR_MAX) 268 1.1 christos grouping = NULL; 269 1.1 christos else 270 1.1 christos { 271 1.1 christos /* Figure out the thousands separator character. */ 272 1.1 christos # if defined _LIBC || defined _HAVE_BTOWC 273 1.1 christos thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)); 274 1.1 christos if (thousands == WEOF) 275 1.1 christos thousands = L'\0'; 276 1.1 christos # endif 277 1.1 christos if (thousands == L'\0') 278 1.1 christos grouping = NULL; 279 1.1 christos } 280 1.1 christos } 281 1.1 christos else 282 1.1 christos grouping = NULL; 283 1.1 christos #endif 284 1.1 christos 285 1.1 christos if (base < 0 || base == 1 || base > 36) 286 1.1 christos { 287 1.1 christos __set_errno (EINVAL); 288 1.1 christos return 0; 289 1.1 christos } 290 1.1 christos 291 1.1 christos save = s = nptr; 292 1.1 christos 293 1.1 christos /* Skip white space. */ 294 1.1 christos while (ISSPACE (*s)) 295 1.1 christos ++s; 296 1.1 christos if (*s == L_('\0')) 297 1.1 christos goto noconv; 298 1.1 christos 299 1.1 christos /* Check for a sign. */ 300 1.1 christos if (*s == L_('-')) 301 1.1 christos { 302 1.1 christos negative = 1; 303 1.1 christos ++s; 304 1.1 christos } 305 1.1 christos else if (*s == L_('+')) 306 1.1 christos { 307 1.1 christos negative = 0; 308 1.1 christos ++s; 309 1.1 christos } 310 1.1 christos else 311 1.1 christos negative = 0; 312 1.1 christos 313 1.1 christos /* Recognize number prefix and if BASE is zero, figure it out ourselves. */ 314 1.1 christos if (*s == L_('0')) 315 1.1 christos { 316 1.1 christos if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X')) 317 1.1 christos { 318 1.1 christos s += 2; 319 1.1 christos base = 16; 320 1.1 christos } 321 1.1 christos else if (base == 0) 322 1.1 christos base = 8; 323 1.1 christos } 324 1.1 christos else if (base == 0) 325 1.1 christos base = 10; 326 1.1 christos 327 1.1 christos /* Save the pointer so we can check later if anything happened. */ 328 1.1 christos save = s; 329 1.1 christos 330 1.1 christos #ifdef USE_NUMBER_GROUPING 331 1.1 christos if (group) 332 1.1 christos { 333 1.1 christos /* Find the end of the digit string and check its grouping. */ 334 1.1 christos end = s; 335 1.1 christos for (c = *end; c != L_('\0'); c = *++end) 336 1.1 christos if ((wchar_t) c != thousands 337 1.1 christos && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9')) 338 1.1 christos && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base)) 339 1.1 christos break; 340 1.1 christos if (*s == thousands) 341 1.1 christos end = s; 342 1.1 christos else 343 1.1 christos end = correctly_grouped_prefix (s, end, thousands, grouping); 344 1.1 christos } 345 1.1 christos else 346 1.1 christos #endif 347 1.1 christos end = NULL; 348 1.1 christos 349 1.1 christos cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base; 350 1.1 christos cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base; 351 1.1 christos 352 1.1 christos overflow = 0; 353 1.1 christos i = 0; 354 1.1 christos for (c = *s; c != L_('\0'); c = *++s) 355 1.1 christos { 356 1.1 christos if (s == end) 357 1.1 christos break; 358 1.1 christos if (c >= L_('0') && c <= L_('9')) 359 1.1 christos c -= L_('0'); 360 1.1 christos else if (ISALPHA (c)) 361 1.1 christos c = TOUPPER (c) - L_('A') + 10; 362 1.1 christos else 363 1.1 christos break; 364 1.1 christos if ((int) c >= base) 365 1.1 christos break; 366 1.1 christos /* Check for overflow. */ 367 1.1 christos if (i > cutoff || (i == cutoff && c > cutlim)) 368 1.1 christos overflow = 1; 369 1.1 christos else 370 1.1 christos { 371 1.1 christos i *= (unsigned LONG int) base; 372 1.1 christos i += c; 373 1.1 christos } 374 1.1 christos } 375 1.1 christos 376 1.1 christos /* Check if anything actually happened. */ 377 1.1 christos if (s == save) 378 1.1 christos goto noconv; 379 1.1 christos 380 1.1 christos /* Store in ENDPTR the address of one character 381 1.1 christos past the last character we converted. */ 382 1.1 christos if (endptr != NULL) 383 1.1 christos *endptr = (STRING_TYPE *) s; 384 1.1 christos 385 1.1 christos #if !UNSIGNED 386 1.1 christos /* Check for a value that is within the range of 387 1.1 christos `unsigned LONG int', but outside the range of `LONG int'. */ 388 1.1 christos if (overflow == 0 389 1.1 christos && i > (negative 390 1.1 christos ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1 391 1.1 christos : (unsigned LONG int) STRTOL_LONG_MAX)) 392 1.1 christos overflow = 1; 393 1.1 christos #endif 394 1.1 christos 395 1.1 christos if (overflow) 396 1.1 christos { 397 1.1 christos __set_errno (ERANGE); 398 1.1 christos #if UNSIGNED 399 1.1 christos return STRTOL_ULONG_MAX; 400 1.1 christos #else 401 1.1 christos return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX; 402 1.1 christos #endif 403 1.1 christos } 404 1.1 christos 405 1.1 christos /* Return the result of the appropriate sign. */ 406 1.1 christos return negative ? -i : i; 407 1.1 christos 408 1.1 christos noconv: 409 1.1 christos /* We must handle a special case here: the base is 0 or 16 and the 410 1.1 christos first two characters are '0' and 'x', but the rest are no 411 1.1 christos hexadecimal digits. This is no error case. We return 0 and 412 1.1 christos ENDPTR points to the `x`. */ 413 1.1 christos if (endptr != NULL) 414 1.1 christos { 415 1.1 christos if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X') 416 1.1 christos && save[-2] == L_('0')) 417 1.1 christos *endptr = (STRING_TYPE *) &save[-1]; 418 1.1 christos else 419 1.1 christos /* There was no number to convert. */ 420 1.1 christos *endptr = (STRING_TYPE *) nptr; 421 1.1 christos } 422 1.1 christos 423 1.1 christos return 0L; 424 1.1 christos } 425 1.1 christos 426 1.1 christos /* External user entry point. */ 428 1.1 christos 429 1.1 christos 430 1.1 christos INT 431 1.1 christos #ifdef weak_function 432 1.1 christos weak_function 433 1.1 christos #endif 434 1.1 christos strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr, 435 1.1 christos int base LOCALE_PARAM_PROTO) 436 1.1 christos { 437 1.1 christos return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM); 438 } 439