1*42115Sbostic /*- 2*42115Sbostic * Copyright (c) 1990 The Regents of the University of California. 335278Sbostic * All rights reserved. 435278Sbostic * 5*42115Sbostic * %sccs.include.redist.c% 635278Sbostic */ 735278Sbostic 835278Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*42115Sbostic static char sccsid[] = "@(#)strtol.c 5.2 (Berkeley) 05/16/90"; 1035278Sbostic #endif /* LIBC_SCCS and not lint */ 1135278Sbostic 12*42115Sbostic #include <limits.h> 1335278Sbostic #include <ctype.h> 14*42115Sbostic #include <errno.h> 1535278Sbostic 16*42115Sbostic /* 17*42115Sbostic * Convert a string to a long integer. 18*42115Sbostic * 19*42115Sbostic * Ignores `locale' stuff. Assumes that the upper and lower case 20*42115Sbostic * alphabets and digits are each contiguous. 21*42115Sbostic */ 2235278Sbostic long 23*42115Sbostic strtol(nptr, endptr, base) 24*42115Sbostic char *nptr, **endptr; 2535278Sbostic register int base; 2635278Sbostic { 27*42115Sbostic register char *s = nptr; 28*42115Sbostic register unsigned long acc; 2935278Sbostic register int c; 30*42115Sbostic register unsigned long cutoff; 31*42115Sbostic register int neg = 0, any, cutlim; 3235278Sbostic 33*42115Sbostic /* 34*42115Sbostic * Skip white space and pick up leading +/- sign if any. 35*42115Sbostic * If base is 0, allow 0x for hex and 0 for octal, else 36*42115Sbostic * assume decimal; if base is already 16, allow 0x. 37*42115Sbostic */ 38*42115Sbostic do { 39*42115Sbostic c = *s++; 40*42115Sbostic } while (isspace(c)); 41*42115Sbostic if (c == '-') { 42*42115Sbostic neg = 1; 43*42115Sbostic c = *s++; 44*42115Sbostic } else if (c == '+') 45*42115Sbostic c = *s++; 46*42115Sbostic if ((base == 0 || base == 16) && 47*42115Sbostic c == '0' && (*s == 'x' || *s == 'X')) { 48*42115Sbostic c = s[1]; 49*42115Sbostic s += 2; 50*42115Sbostic base = 16; 5135278Sbostic } 52*42115Sbostic if (base == 0) 53*42115Sbostic base = c == '0' ? 8 : 10; 5435278Sbostic 5535278Sbostic /* 56*42115Sbostic * Compute the cutoff value between legal numbers and illegal 57*42115Sbostic * numbers. That is the largest legal value, divided by the 58*42115Sbostic * base. An input number that is greater than this value, if 59*42115Sbostic * followed by a legal input character, is too big. One that 60*42115Sbostic * is equal to this value may be valid or not; the limit 61*42115Sbostic * between valid and invalid numbers is then based on the last 62*42115Sbostic * digit. For instance, if the range for longs is 63*42115Sbostic * [-2147483648..2147483647] and the input base is 10, 64*42115Sbostic * cutoff will be set to 214748364 and cutlim to either 65*42115Sbostic * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 66*42115Sbostic * a value > 214748364, or equal but the next digit is > 7 (or 8), 67*42115Sbostic * the number is too big, and we will return a range error. 68*42115Sbostic * 69*42115Sbostic * Set any if any `digits' consumed; make it negative to indicate 70*42115Sbostic * overflow. 7135278Sbostic */ 72*42115Sbostic cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 73*42115Sbostic cutlim = cutoff % (unsigned long)base; 74*42115Sbostic cutoff /= (unsigned long)base; 75*42115Sbostic for (acc = 0, any = 0;; c = *s++) { 7635278Sbostic if (isdigit(c)) 7735278Sbostic c -= '0'; 78*42115Sbostic else if (isalpha(c)) 79*42115Sbostic c -= isupper(c) ? 'A' - 10 : 'a' - 10; 80*42115Sbostic else 81*42115Sbostic break; 82*42115Sbostic if (c >= base) 83*42115Sbostic break; 84*42115Sbostic if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 85*42115Sbostic any = -1; 8635278Sbostic else { 87*42115Sbostic any = 1; 88*42115Sbostic acc *= base; 89*42115Sbostic acc += c; 9035278Sbostic } 9135278Sbostic } 92*42115Sbostic if (any < 0) { 93*42115Sbostic acc = neg ? LONG_MIN : LONG_MAX; 94*42115Sbostic errno = ERANGE; 95*42115Sbostic } else if (neg) 96*42115Sbostic acc = -acc; 97*42115Sbostic if (endptr != 0) 98*42115Sbostic *endptr = any ? s - 1 : nptr; 99*42115Sbostic return (acc); 10035278Sbostic } 101