12a6b7db3Sskrll /*-
22a6b7db3Sskrll  * Copyright (c) 1990 The Regents of the University of California.
32a6b7db3Sskrll  * All rights reserved.
42a6b7db3Sskrll  *
52a6b7db3Sskrll  * Redistribution and use in source and binary forms, with or without
62a6b7db3Sskrll  * modification, are permitted provided that the following conditions
72a6b7db3Sskrll  * are met:
82a6b7db3Sskrll  * 1. Redistributions of source code must retain the above copyright
92a6b7db3Sskrll  *    notice, this list of conditions and the following disclaimer.
102a6b7db3Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
112a6b7db3Sskrll  *    notice, this list of conditions and the following disclaimer in the
122a6b7db3Sskrll  *    documentation and/or other materials provided with the distribution.
132a6b7db3Sskrll  * 3. [rescinded 22 July 1999]
142a6b7db3Sskrll  * 4. Neither the name of the University nor the names of its contributors
152a6b7db3Sskrll  *    may be used to endorse or promote products derived from this software
162a6b7db3Sskrll  *    without specific prior written permission.
172a6b7db3Sskrll  *
182a6b7db3Sskrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192a6b7db3Sskrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202a6b7db3Sskrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212a6b7db3Sskrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222a6b7db3Sskrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232a6b7db3Sskrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242a6b7db3Sskrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252a6b7db3Sskrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262a6b7db3Sskrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272a6b7db3Sskrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282a6b7db3Sskrll  * SUCH DAMAGE.
292a6b7db3Sskrll  */
302a6b7db3Sskrll 
312a6b7db3Sskrll /*
322a6b7db3Sskrll 
33*05caefcfSchristos @deftypefn Supplemental {long int} strtol (const char *@var{string}, @
34*05caefcfSchristos   char **@var{endptr}, int @var{base})
35*05caefcfSchristos @deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, @
36*05caefcfSchristos   char **@var{endptr}, int @var{base})
372a6b7db3Sskrll 
382a6b7db3Sskrll The @code{strtol} function converts the string in @var{string} to a
392a6b7db3Sskrll long integer value according to the given @var{base}, which must be
402a6b7db3Sskrll between 2 and 36 inclusive, or be the special value 0.  If @var{base}
412a6b7db3Sskrll is 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
422a6b7db3Sskrll to indicate bases 8 and 16, respectively, else default to base 10.
432a6b7db3Sskrll When the base is 16 (either explicitly or implicitly), a prefix of
442a6b7db3Sskrll @code{0x} is allowed.  The handling of @var{endptr} is as that of
452a6b7db3Sskrll @code{strtod} above.  The @code{strtoul} function is the same, except
462a6b7db3Sskrll that the converted value is unsigned.
472a6b7db3Sskrll 
482a6b7db3Sskrll @end deftypefn
492a6b7db3Sskrll 
502a6b7db3Sskrll */
512a6b7db3Sskrll 
522a6b7db3Sskrll #ifdef HAVE_CONFIG_H
532a6b7db3Sskrll #include "config.h"
542a6b7db3Sskrll #endif
552a6b7db3Sskrll #ifdef HAVE_LIMITS_H
562a6b7db3Sskrll #include <limits.h>
572a6b7db3Sskrll #endif
582a6b7db3Sskrll #ifdef HAVE_SYS_PARAM_H
592a6b7db3Sskrll #include <sys/param.h>
602a6b7db3Sskrll #endif
612a6b7db3Sskrll #include <errno.h>
622a6b7db3Sskrll #ifdef NEED_DECLARATION_ERRNO
632a6b7db3Sskrll extern int errno;
642a6b7db3Sskrll #endif
652a6b7db3Sskrll #include "safe-ctype.h"
662a6b7db3Sskrll 
672a6b7db3Sskrll /* FIXME: It'd be nice to configure around these, but the include files are too
682a6b7db3Sskrll    painful.  These macros should at least be more portable than hardwired hex
692a6b7db3Sskrll    constants. */
702a6b7db3Sskrll 
712a6b7db3Sskrll #ifndef ULONG_MAX
722a6b7db3Sskrll #define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
732a6b7db3Sskrll #endif
742a6b7db3Sskrll 
752a6b7db3Sskrll #ifndef LONG_MAX
762a6b7db3Sskrll #define	LONG_MAX	((long)(ULONG_MAX >> 1))	/* 0x7FFFFFFF */
772a6b7db3Sskrll #endif
782a6b7db3Sskrll 
792a6b7db3Sskrll #ifndef LONG_MIN
802a6b7db3Sskrll #define	LONG_MIN	((long)(~LONG_MAX))		/* 0x80000000 */
812a6b7db3Sskrll #endif
822a6b7db3Sskrll 
832a6b7db3Sskrll /*
842a6b7db3Sskrll  * Convert a string to a long integer.
852a6b7db3Sskrll  *
862a6b7db3Sskrll  * Ignores `locale' stuff.  Assumes that the upper and lower case
872a6b7db3Sskrll  * alphabets and digits are each contiguous.
882a6b7db3Sskrll  */
892a6b7db3Sskrll long
strtol(const char * nptr,char ** endptr,register int base)902a6b7db3Sskrll strtol(const char *nptr, char **endptr, register int base)
912a6b7db3Sskrll {
922a6b7db3Sskrll 	register const char *s = nptr;
932a6b7db3Sskrll 	register unsigned long acc;
942a6b7db3Sskrll 	register int c;
952a6b7db3Sskrll 	register unsigned long cutoff;
962a6b7db3Sskrll 	register int neg = 0, any, cutlim;
972a6b7db3Sskrll 
982a6b7db3Sskrll 	/*
992a6b7db3Sskrll 	 * Skip white space and pick up leading +/- sign if any.
1002a6b7db3Sskrll 	 * If base is 0, allow 0x for hex and 0 for octal, else
1012a6b7db3Sskrll 	 * assume decimal; if base is already 16, allow 0x.
1022a6b7db3Sskrll 	 */
1032a6b7db3Sskrll 	do {
1042a6b7db3Sskrll 		c = *s++;
1052a6b7db3Sskrll 	} while (ISSPACE(c));
1062a6b7db3Sskrll 	if (c == '-') {
1072a6b7db3Sskrll 		neg = 1;
1082a6b7db3Sskrll 		c = *s++;
1092a6b7db3Sskrll 	} else if (c == '+')
1102a6b7db3Sskrll 		c = *s++;
1112a6b7db3Sskrll 	if ((base == 0 || base == 16) &&
1122a6b7db3Sskrll 	    c == '0' && (*s == 'x' || *s == 'X')) {
1132a6b7db3Sskrll 		c = s[1];
1142a6b7db3Sskrll 		s += 2;
1152a6b7db3Sskrll 		base = 16;
1162a6b7db3Sskrll 	}
1172a6b7db3Sskrll 	if (base == 0)
1182a6b7db3Sskrll 		base = c == '0' ? 8 : 10;
1192a6b7db3Sskrll 
1202a6b7db3Sskrll 	/*
1212a6b7db3Sskrll 	 * Compute the cutoff value between legal numbers and illegal
1222a6b7db3Sskrll 	 * numbers.  That is the largest legal value, divided by the
1232a6b7db3Sskrll 	 * base.  An input number that is greater than this value, if
1242a6b7db3Sskrll 	 * followed by a legal input character, is too big.  One that
1252a6b7db3Sskrll 	 * is equal to this value may be valid or not; the limit
1262a6b7db3Sskrll 	 * between valid and invalid numbers is then based on the last
1272a6b7db3Sskrll 	 * digit.  For instance, if the range for longs is
1282a6b7db3Sskrll 	 * [-2147483648..2147483647] and the input base is 10,
1292a6b7db3Sskrll 	 * cutoff will be set to 214748364 and cutlim to either
1302a6b7db3Sskrll 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
1312a6b7db3Sskrll 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
1322a6b7db3Sskrll 	 * the number is too big, and we will return a range error.
1332a6b7db3Sskrll 	 *
1342a6b7db3Sskrll 	 * Set any if any `digits' consumed; make it negative to indicate
1352a6b7db3Sskrll 	 * overflow.
1362a6b7db3Sskrll 	 */
1372a6b7db3Sskrll 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
1382a6b7db3Sskrll 	cutlim = cutoff % (unsigned long)base;
1392a6b7db3Sskrll 	cutoff /= (unsigned long)base;
1402a6b7db3Sskrll 	for (acc = 0, any = 0;; c = *s++) {
1412a6b7db3Sskrll 		if (ISDIGIT(c))
1422a6b7db3Sskrll 			c -= '0';
1432a6b7db3Sskrll 		else if (ISALPHA(c))
1442a6b7db3Sskrll 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
1452a6b7db3Sskrll 		else
1462a6b7db3Sskrll 			break;
1472a6b7db3Sskrll 		if (c >= base)
1482a6b7db3Sskrll 			break;
1492a6b7db3Sskrll 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1502a6b7db3Sskrll 			any = -1;
1512a6b7db3Sskrll 		else {
1522a6b7db3Sskrll 			any = 1;
1532a6b7db3Sskrll 			acc *= base;
1542a6b7db3Sskrll 			acc += c;
1552a6b7db3Sskrll 		}
1562a6b7db3Sskrll 	}
1572a6b7db3Sskrll 	if (any < 0) {
1582a6b7db3Sskrll 		acc = neg ? LONG_MIN : LONG_MAX;
1592a6b7db3Sskrll 		errno = ERANGE;
1602a6b7db3Sskrll 	} else if (neg)
1612a6b7db3Sskrll 		acc = -acc;
1622a6b7db3Sskrll 	if (endptr != 0)
1632a6b7db3Sskrll 		*endptr = (char *) (any ? s - 1 : nptr);
1642a6b7db3Sskrll 	return (acc);
1652a6b7db3Sskrll }
166