xref: /openbsd/gnu/lib/libiberty/src/strtol.c (revision 150b7e42)
100bf4279Sespie /*-
200bf4279Sespie  * Copyright (c) 1990 The Regents of the University of California.
300bf4279Sespie  * All rights reserved.
400bf4279Sespie  *
500bf4279Sespie  * Redistribution and use in source and binary forms, with or without
600bf4279Sespie  * modification, are permitted provided that the following conditions
700bf4279Sespie  * are met:
800bf4279Sespie  * 1. Redistributions of source code must retain the above copyright
900bf4279Sespie  *    notice, this list of conditions and the following disclaimer.
1000bf4279Sespie  * 2. Redistributions in binary form must reproduce the above copyright
1100bf4279Sespie  *    notice, this list of conditions and the following disclaimer in the
1200bf4279Sespie  *    documentation and/or other materials provided with the distribution.
1300bf4279Sespie  * 3. [rescinded 22 July 1999]
1400bf4279Sespie  * 4. Neither the name of the University nor the names of its contributors
1500bf4279Sespie  *    may be used to endorse or promote products derived from this software
1600bf4279Sespie  *    without specific prior written permission.
1700bf4279Sespie  *
1800bf4279Sespie  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1900bf4279Sespie  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2000bf4279Sespie  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2100bf4279Sespie  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2200bf4279Sespie  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2300bf4279Sespie  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2400bf4279Sespie  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2500bf4279Sespie  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2600bf4279Sespie  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2700bf4279Sespie  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2800bf4279Sespie  * SUCH DAMAGE.
2900bf4279Sespie  */
3000bf4279Sespie 
3137c53322Sespie /*
3237c53322Sespie 
3337c53322Sespie @deftypefn Supplemental {long int} strtol (const char *@var{string}, char **@var{endptr}, int @var{base})
3437c53322Sespie @deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, char **@var{endptr}, int @var{base})
3537c53322Sespie 
3637c53322Sespie The @code{strtol} function converts the string in @var{string} to a
3737c53322Sespie long integer value according to the given @var{base}, which must be
3837c53322Sespie between 2 and 36 inclusive, or be the special value 0.  If @var{base}
3937c53322Sespie is 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
4037c53322Sespie to indicate bases 8 and 16, respectively, else default to base 10.
4137c53322Sespie When the base is 16 (either explicitly or implicitly), a prefix of
4237c53322Sespie @code{0x} is allowed.  The handling of @var{endptr} is as that of
4337c53322Sespie @code{strtod} above.  The @code{strtoul} function is the same, except
4437c53322Sespie that the converted value is unsigned.
4537c53322Sespie 
4637c53322Sespie @end deftypefn
4737c53322Sespie 
4837c53322Sespie */
4937c53322Sespie 
5037c53322Sespie #ifdef HAVE_CONFIG_H
5137c53322Sespie #include "config.h"
522e0724c7Sespie #endif
5337c53322Sespie #ifdef HAVE_LIMITS_H
5437c53322Sespie #include <limits.h>
5537c53322Sespie #endif
5637c53322Sespie #ifdef HAVE_SYS_PARAM_H
5737c53322Sespie #include <sys/param.h>
5837c53322Sespie #endif
5937c53322Sespie #include <errno.h>
6037c53322Sespie #ifdef NEED_DECLARATION_ERRNO
6137c53322Sespie extern int errno;
6237c53322Sespie #endif
6337c53322Sespie #include "safe-ctype.h"
6400bf4279Sespie 
6500bf4279Sespie /* FIXME: It'd be nice to configure around these, but the include files are too
6600bf4279Sespie    painful.  These macros should at least be more portable than hardwired hex
6700bf4279Sespie    constants. */
6800bf4279Sespie 
6900bf4279Sespie #ifndef ULONG_MAX
7000bf4279Sespie #define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
7100bf4279Sespie #endif
7200bf4279Sespie 
7300bf4279Sespie #ifndef LONG_MAX
7400bf4279Sespie #define	LONG_MAX	((long)(ULONG_MAX >> 1))	/* 0x7FFFFFFF */
7500bf4279Sespie #endif
7600bf4279Sespie 
7700bf4279Sespie #ifndef LONG_MIN
7800bf4279Sespie #define	LONG_MIN	((long)(~LONG_MAX))		/* 0x80000000 */
7900bf4279Sespie #endif
8000bf4279Sespie 
8100bf4279Sespie /*
8200bf4279Sespie  * Convert a string to a long integer.
8300bf4279Sespie  *
8400bf4279Sespie  * Ignores `locale' stuff.  Assumes that the upper and lower case
8500bf4279Sespie  * alphabets and digits are each contiguous.
8600bf4279Sespie  */
8700bf4279Sespie long
strtol(const char * nptr,char ** endptr,register int base)88*150b7e42Smiod strtol(const char *nptr, char **endptr, register int base)
8900bf4279Sespie {
9000bf4279Sespie 	register const char *s = nptr;
9100bf4279Sespie 	register unsigned long acc;
9200bf4279Sespie 	register int c;
9300bf4279Sespie 	register unsigned long cutoff;
9400bf4279Sespie 	register int neg = 0, any, cutlim;
9500bf4279Sespie 
9600bf4279Sespie 	/*
9700bf4279Sespie 	 * Skip white space and pick up leading +/- sign if any.
9800bf4279Sespie 	 * If base is 0, allow 0x for hex and 0 for octal, else
9900bf4279Sespie 	 * assume decimal; if base is already 16, allow 0x.
10000bf4279Sespie 	 */
10100bf4279Sespie 	do {
10200bf4279Sespie 		c = *s++;
10337c53322Sespie 	} while (ISSPACE(c));
10400bf4279Sespie 	if (c == '-') {
10500bf4279Sespie 		neg = 1;
10600bf4279Sespie 		c = *s++;
10700bf4279Sespie 	} else if (c == '+')
10800bf4279Sespie 		c = *s++;
10900bf4279Sespie 	if ((base == 0 || base == 16) &&
11000bf4279Sespie 	    c == '0' && (*s == 'x' || *s == 'X')) {
11100bf4279Sespie 		c = s[1];
11200bf4279Sespie 		s += 2;
11300bf4279Sespie 		base = 16;
11400bf4279Sespie 	}
11500bf4279Sespie 	if (base == 0)
11600bf4279Sespie 		base = c == '0' ? 8 : 10;
11700bf4279Sespie 
11800bf4279Sespie 	/*
11900bf4279Sespie 	 * Compute the cutoff value between legal numbers and illegal
12000bf4279Sespie 	 * numbers.  That is the largest legal value, divided by the
12100bf4279Sespie 	 * base.  An input number that is greater than this value, if
12200bf4279Sespie 	 * followed by a legal input character, is too big.  One that
12300bf4279Sespie 	 * is equal to this value may be valid or not; the limit
12400bf4279Sespie 	 * between valid and invalid numbers is then based on the last
12500bf4279Sespie 	 * digit.  For instance, if the range for longs is
12600bf4279Sespie 	 * [-2147483648..2147483647] and the input base is 10,
12700bf4279Sespie 	 * cutoff will be set to 214748364 and cutlim to either
12800bf4279Sespie 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
12900bf4279Sespie 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
13000bf4279Sespie 	 * the number is too big, and we will return a range error.
13100bf4279Sespie 	 *
13200bf4279Sespie 	 * Set any if any `digits' consumed; make it negative to indicate
13300bf4279Sespie 	 * overflow.
13400bf4279Sespie 	 */
13500bf4279Sespie 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
13600bf4279Sespie 	cutlim = cutoff % (unsigned long)base;
13700bf4279Sespie 	cutoff /= (unsigned long)base;
13800bf4279Sespie 	for (acc = 0, any = 0;; c = *s++) {
13937c53322Sespie 		if (ISDIGIT(c))
14000bf4279Sespie 			c -= '0';
14137c53322Sespie 		else if (ISALPHA(c))
14237c53322Sespie 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
14300bf4279Sespie 		else
14400bf4279Sespie 			break;
14500bf4279Sespie 		if (c >= base)
14600bf4279Sespie 			break;
147*150b7e42Smiod 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
14800bf4279Sespie 			any = -1;
14900bf4279Sespie 		else {
15000bf4279Sespie 			any = 1;
15100bf4279Sespie 			acc *= base;
15200bf4279Sespie 			acc += c;
15300bf4279Sespie 		}
15400bf4279Sespie 	}
15500bf4279Sespie 	if (any < 0) {
15600bf4279Sespie 		acc = neg ? LONG_MIN : LONG_MAX;
15700bf4279Sespie 		errno = ERANGE;
15800bf4279Sespie 	} else if (neg)
15900bf4279Sespie 		acc = -acc;
16000bf4279Sespie 	if (endptr != 0)
16100bf4279Sespie 		*endptr = (char *) (any ? s - 1 : nptr);
16200bf4279Sespie 	return (acc);
16300bf4279Sespie }
164