142115Sbostic /*- 242115Sbostic * Copyright (c) 1990 The Regents of the University of California. 335278Sbostic * All rights reserved. 435278Sbostic * 542115Sbostic * %sccs.include.redist.c% 635278Sbostic */ 735278Sbostic 835278Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*42179Sbostic static char sccsid[] = "@(#)strtol.c 5.3 (Berkeley) 05/17/90"; 1035278Sbostic #endif /* LIBC_SCCS and not lint */ 1135278Sbostic 1242115Sbostic #include <limits.h> 1335278Sbostic #include <ctype.h> 1442115Sbostic #include <errno.h> 15*42179Sbostic #include <stdlib.h> 1635278Sbostic 17*42179Sbostic 1842115Sbostic /* 1942115Sbostic * Convert a string to a long integer. 2042115Sbostic * 2142115Sbostic * Ignores `locale' stuff. Assumes that the upper and lower case 2242115Sbostic * alphabets and digits are each contiguous. 2342115Sbostic */ 2435278Sbostic long 2542115Sbostic strtol(nptr, endptr, base) 2642115Sbostic char *nptr, **endptr; 2735278Sbostic register int base; 2835278Sbostic { 2942115Sbostic register char *s = nptr; 3042115Sbostic register unsigned long acc; 3135278Sbostic register int c; 3242115Sbostic register unsigned long cutoff; 3342115Sbostic register int neg = 0, any, cutlim; 3435278Sbostic 3542115Sbostic /* 3642115Sbostic * Skip white space and pick up leading +/- sign if any. 3742115Sbostic * If base is 0, allow 0x for hex and 0 for octal, else 3842115Sbostic * assume decimal; if base is already 16, allow 0x. 3942115Sbostic */ 4042115Sbostic do { 4142115Sbostic c = *s++; 4242115Sbostic } while (isspace(c)); 4342115Sbostic if (c == '-') { 4442115Sbostic neg = 1; 4542115Sbostic c = *s++; 4642115Sbostic } else if (c == '+') 4742115Sbostic c = *s++; 4842115Sbostic if ((base == 0 || base == 16) && 4942115Sbostic c == '0' && (*s == 'x' || *s == 'X')) { 5042115Sbostic c = s[1]; 5142115Sbostic s += 2; 5242115Sbostic base = 16; 5335278Sbostic } 5442115Sbostic if (base == 0) 5542115Sbostic base = c == '0' ? 8 : 10; 5635278Sbostic 5735278Sbostic /* 5842115Sbostic * Compute the cutoff value between legal numbers and illegal 5942115Sbostic * numbers. That is the largest legal value, divided by the 6042115Sbostic * base. An input number that is greater than this value, if 6142115Sbostic * followed by a legal input character, is too big. One that 6242115Sbostic * is equal to this value may be valid or not; the limit 6342115Sbostic * between valid and invalid numbers is then based on the last 6442115Sbostic * digit. For instance, if the range for longs is 6542115Sbostic * [-2147483648..2147483647] and the input base is 10, 6642115Sbostic * cutoff will be set to 214748364 and cutlim to either 6742115Sbostic * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 6842115Sbostic * a value > 214748364, or equal but the next digit is > 7 (or 8), 6942115Sbostic * the number is too big, and we will return a range error. 7042115Sbostic * 7142115Sbostic * Set any if any `digits' consumed; make it negative to indicate 7242115Sbostic * overflow. 7335278Sbostic */ 7442115Sbostic cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 7542115Sbostic cutlim = cutoff % (unsigned long)base; 7642115Sbostic cutoff /= (unsigned long)base; 7742115Sbostic for (acc = 0, any = 0;; c = *s++) { 7835278Sbostic if (isdigit(c)) 7935278Sbostic c -= '0'; 8042115Sbostic else if (isalpha(c)) 8142115Sbostic c -= isupper(c) ? 'A' - 10 : 'a' - 10; 8242115Sbostic else 8342115Sbostic break; 8442115Sbostic if (c >= base) 8542115Sbostic break; 8642115Sbostic if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 8742115Sbostic any = -1; 8835278Sbostic else { 8942115Sbostic any = 1; 9042115Sbostic acc *= base; 9142115Sbostic acc += c; 9235278Sbostic } 9335278Sbostic } 9442115Sbostic if (any < 0) { 9542115Sbostic acc = neg ? LONG_MIN : LONG_MAX; 9642115Sbostic errno = ERANGE; 9742115Sbostic } else if (neg) 9842115Sbostic acc = -acc; 9942115Sbostic if (endptr != 0) 10042115Sbostic *endptr = any ? s - 1 : nptr; 10142115Sbostic return (acc); 10235278Sbostic } 103