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