strtol.c revision 17141
1236769Sobrien/*- 2236769Sobrien * Copyright (c) 1990, 1993 3236769Sobrien * The Regents of the University of California. All rights reserved. 4236769Sobrien * 5236769Sobrien * Redistribution and use in source and binary forms, with or without 6236769Sobrien * modification, are permitted provided that the following conditions 7236769Sobrien * are met: 8236769Sobrien * 1. Redistributions of source code must retain the above copyright 9236769Sobrien * notice, this list of conditions and the following disclaimer. 10236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11236769Sobrien * notice, this list of conditions and the following disclaimer in the 12236769Sobrien * documentation and/or other materials provided with the distribution. 13236769Sobrien * 3. All advertising materials mentioning features or use of this software 14236769Sobrien * must display the following acknowledgement: 15236769Sobrien * This product includes software developed by the University of 16236769Sobrien * California, Berkeley and its contributors. 17236769Sobrien * 4. Neither the name of the University nor the names of its contributors 18236769Sobrien * may be used to endorse or promote products derived from this software 19236769Sobrien * without specific prior written permission. 20236769Sobrien * 21236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31236769Sobrien * SUCH DAMAGE. 32236769Sobrien */ 33236769Sobrien 34236769Sobrien#if defined(LIBC_SCCS) && !defined(lint) 35236769Sobrienstatic char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 36236769Sobrien#endif /* LIBC_SCCS and not lint */ 37236769Sobrien 38236769Sobrien#include <limits.h> 39236769Sobrien#include <ctype.h> 40236769Sobrien#include <errno.h> 41236769Sobrien#include <stdlib.h> 42236769Sobrien 43236769Sobrien 44236769Sobrien/* 45236769Sobrien * Convert a string to a long integer. 46236769Sobrien * 47236769Sobrien * Ignores `locale' stuff. Assumes that the upper and lower case 48236769Sobrien * alphabets and digits are each contiguous. 49236769Sobrien */ 50236769Sobrienlong 51236769Sobrienstrtol(nptr, endptr, base) 52236769Sobrien const char *nptr; 53236769Sobrien char **endptr; 54236769Sobrien register int base; 55236769Sobrien{ 56236769Sobrien register const char *s = nptr; 57236769Sobrien register unsigned long acc; 58236769Sobrien register unsigned char c; 59236769Sobrien register unsigned long cutoff; 60236769Sobrien register int neg = 0, any, cutlim; 61236769Sobrien 62236769Sobrien /* 63236769Sobrien * Skip white space and pick up leading +/- sign if any. 64236769Sobrien * If base is 0, allow 0x for hex and 0 for octal, else 65236769Sobrien * assume decimal; if base is already 16, allow 0x. 66236769Sobrien */ 67236769Sobrien do { 68236769Sobrien c = *s++; 69236769Sobrien } while (isspace(c)); 70236769Sobrien if (c == '-') { 71236769Sobrien neg = 1; 72236769Sobrien c = *s++; 73236769Sobrien } else if (c == '+') 74236769Sobrien c = *s++; 75236769Sobrien if ((base == 0 || base == 16) && 76236769Sobrien c == '0' && (*s == 'x' || *s == 'X')) { 77236769Sobrien c = s[1]; 78236769Sobrien s += 2; 79236769Sobrien base = 16; 80236769Sobrien } 81236769Sobrien if (base == 0) 82236769Sobrien base = c == '0' ? 8 : 10; 83236769Sobrien 84236769Sobrien /* 85236769Sobrien * Compute the cutoff value between legal numbers and illegal 86236769Sobrien * numbers. That is the largest legal value, divided by the 87236769Sobrien * base. An input number that is greater than this value, if 88236769Sobrien * followed by a legal input character, is too big. One that 89236769Sobrien * is equal to this value may be valid or not; the limit 90236769Sobrien * between valid and invalid numbers is then based on the last 91236769Sobrien * digit. For instance, if the range for longs is 92236769Sobrien * [-2147483648..2147483647] and the input base is 10, 93236769Sobrien * cutoff will be set to 214748364 and cutlim to either 94236769Sobrien * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 95236769Sobrien * a value > 214748364, or equal but the next digit is > 7 (or 8), 96236769Sobrien * the number is too big, and we will return a range error. 97236769Sobrien * 98236769Sobrien * Set any if any `digits' consumed; make it negative to indicate 99236769Sobrien * overflow. 100236769Sobrien */ 101236769Sobrien cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 102236769Sobrien cutlim = cutoff % (unsigned long)base; 103236769Sobrien cutoff /= (unsigned long)base; 104236769Sobrien for (acc = 0, any = 0;; c = *s++) { 105236769Sobrien if (!isascii(c)) 106236769Sobrien break; 107236769Sobrien if (isdigit(c)) 108236769Sobrien c -= '0'; 109236769Sobrien else if (isalpha(c)) 110236769Sobrien c -= isupper(c) ? 'A' - 10 : 'a' - 10; 111236769Sobrien else 112236769Sobrien break; 113236769Sobrien if (c >= base) 114236769Sobrien break; 115236769Sobrien if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 116236769Sobrien any = -1; 117236769Sobrien else { 118236769Sobrien any = 1; 119236769Sobrien acc *= base; 120236769Sobrien acc += c; 121236769Sobrien } 122236769Sobrien } 123236769Sobrien if (any < 0) { 124236769Sobrien acc = neg ? LONG_MIN : LONG_MAX; 125236769Sobrien errno = ERANGE; 126236769Sobrien } else if (neg) 127236769Sobrien acc = -acc; 128236769Sobrien if (endptr != 0) 129236769Sobrien *endptr = (char *)(any ? s - 1 : nptr); 130236769Sobrien return (acc); 131236769Sobrien} 132236769Sobrien