strtol.c revision 83024
150276Speter/*- 250276Speter * Copyright (c) 1990, 1993 3166124Srafan * The Regents of the University of California. All rights reserved. 450276Speter * 550276Speter * Redistribution and use in source and binary forms, with or without 650276Speter * modification, are permitted provided that the following conditions 750276Speter * are met: 850276Speter * 1. Redistributions of source code must retain the above copyright 950276Speter * notice, this list of conditions and the following disclaimer. 1050276Speter * 2. Redistributions in binary form must reproduce the above copyright 1150276Speter * notice, this list of conditions and the following disclaimer in the 1250276Speter * documentation and/or other materials provided with the distribution. 1350276Speter * 3. All advertising materials mentioning features or use of this software 1450276Speter * must display the following acknowledgement: 1550276Speter * This product includes software developed by the University of 1650276Speter * California, Berkeley and its contributors. 1750276Speter * 4. Neither the name of the University nor the names of its contributors 1850276Speter * may be used to endorse or promote products derived from this software 1950276Speter * without specific prior written permission. 2050276Speter * 2150276Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2250276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2350276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2450276Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2550276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2650276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2750276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2850276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2950276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3050276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3150276Speter * SUCH DAMAGE. 32174993Srafan * 3350276Speter * $FreeBSD: head/lib/libc/stdlib/strtol.c 83024 2001-09-04 21:28:01Z ache $ 3450276Speter */ 3550276Speter 3650276Speter#if defined(LIBC_SCCS) && !defined(lint) 3750276Speterstatic char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 3850276Speter#endif /* LIBC_SCCS and not lint */ 3950276Speter 4050276Speter#include <limits.h> 4150276Speter#include <ctype.h> 4250276Speter#include <errno.h> 4350276Speter#include <stdlib.h> 4450276Speter 4550276Speter 4650276Speter/* 4750276Speter * Convert a string to a long integer. 4850276Speter * 4950276Speter * Assumes that the upper and lower case 5050276Speter * alphabets and digits are each contiguous. 5150276Speter */ 5250276Speterlong 5350276Speterstrtol(nptr, endptr, base) 5450276Speter const char *nptr; 5550276Speter char **endptr; 5650276Speter register int base; 5750276Speter{ 5862449Speter register const char *s; 5962449Speter register unsigned long acc; 6062449Speter register unsigned char c; 61166124Srafan register unsigned long cutoff; 62166124Srafan register int neg, any, cutlim; 6350276Speter 64166124Srafan /* 65166124Srafan * Skip white space and pick up leading +/- sign if any. 66166124Srafan * If base is 0, allow 0x for hex and 0 for octal, else 67166124Srafan * assume decimal; if base is already 16, allow 0x. 68166124Srafan */ 69166124Srafan s = nptr; 70166124Srafan do { 71166124Srafan c = *s++; 7250276Speter } while (isspace(c)); 73166124Srafan if (c == '-') { 74166124Srafan neg = 1; 7550276Speter c = *s++; 7650276Speter } else { 7750276Speter neg = 0; 78166124Srafan if (c == '+') 7950276Speter c = *s++; 80166124Srafan } 8150276Speter if ((base == 0 || base == 16) && 8250276Speter c == '0' && (*s == 'x' || *s == 'X')) { 83166124Srafan c = s[1]; 84174993Srafan s += 2; 85166124Srafan base = 16; 86166124Srafan } 8750276Speter if (base == 0) 8850276Speter base = c == '0' ? 8 : 10; 89166124Srafan acc = any = 0; 90166124Srafan if (base < 2 || base > 36) 9150276Speter goto noconv; 92166124Srafan 93166124Srafan /* 94166124Srafan * Compute the cutoff value between legal numbers and illegal 95166124Srafan * numbers. That is the largest legal value, divided by the 96166124Srafan * base. An input number that is greater than this value, if 9762449Speter * followed by a legal input character, is too big. One that 98166124Srafan * is equal to this value may be valid or not; the limit 99166124Srafan * between valid and invalid numbers is then based on the last 100166124Srafan * digit. For instance, if the range for longs is 101166124Srafan * [-2147483648..2147483647] and the input base is 10, 102166124Srafan * cutoff will be set to 214748364 and cutlim to either 10350276Speter * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 104166124Srafan * a value > 214748364, or equal but the next digit is > 7 (or 8), 105166124Srafan * the number is too big, and we will return a range error. 106166124Srafan * 107166124Srafan * Set 'any' if any `digits' consumed; make it negative to indicate 108166124Srafan * overflow. 109166124Srafan */ 110166124Srafan cutoff = neg ? -(LONG_MIN + LONG_MAX) + (unsigned long)LONG_MAX 111166124Srafan : LONG_MAX; 112166124Srafan cutlim = cutoff % base; 113166124Srafan cutoff /= base; 114166124Srafan for ( ; ; c = *s++) { 115166124Srafan if (!isascii(c)) 116166124Srafan break; 117166124Srafan if (isdigit(c)) 118166124Srafan c -= '0'; 11950276Speter else if (isalpha(c)) 120166124Srafan c -= isupper(c) ? 'A' - 10 : 'a' - 10; 121166124Srafan else 122166124Srafan break; 123166124Srafan if (c >= base) 124166124Srafan break; 125166124Srafan if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 126166124Srafan any = -1; 127166124Srafan else { 128166124Srafan any = 1; 129166124Srafan acc *= base; 130166124Srafan acc += c; 131166124Srafan } 132166124Srafan } 133166124Srafan if (any < 0) { 134166124Srafan acc = neg ? LONG_MIN : LONG_MAX; 135166124Srafan errno = ERANGE; 136166124Srafan } else if (!any) { 137166124Srafannoconv: 138166124Srafan errno = EINVAL; 13950276Speter } else if (neg) 140166124Srafan acc = -acc; 14150276Speter if (endptr != NULL) 142 *endptr = (char *)(any ? s - 1 : nptr); 143 return (acc); 144} 145