133965Sjdp/*-
233965Sjdp * Copyright (c) 1990 The Regents of the University of California.
333965Sjdp * All rights reserved.
433965Sjdp *
533965Sjdp * Redistribution and use in source and binary forms, with or without
633965Sjdp * modification, are permitted provided that the following conditions
733965Sjdp * are met:
833965Sjdp * 1. Redistributions of source code must retain the above copyright
933965Sjdp *    notice, this list of conditions and the following disclaimer.
1033965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1133965Sjdp *    notice, this list of conditions and the following disclaimer in the
1233965Sjdp *    documentation and/or other materials provided with the distribution.
1360484Sobrien * 3. [rescinded 22 July 1999]
1433965Sjdp * 4. Neither the name of the University nor the names of its contributors
1533965Sjdp *    may be used to endorse or promote products derived from this software
1633965Sjdp *    without specific prior written permission.
1733965Sjdp *
1833965Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1933965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2033965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2133965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2233965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2333965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2433965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2533965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2633965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2733965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2833965Sjdp * SUCH DAMAGE.
2933965Sjdp */
3033965Sjdp
3189857Sobrien/*
3289857Sobrien
3389857Sobrien@deftypefn Supplemental {long int} strtol (const char *@var{string}, char **@var{endptr}, int @var{base})
3489857Sobrien@deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, char **@var{endptr}, int @var{base})
3589857Sobrien
3689857SobrienThe @code{strtol} function converts the string in @var{string} to a
3789857Sobrienlong integer value according to the given @var{base}, which must be
3889857Sobrienbetween 2 and 36 inclusive, or be the special value 0.  If @var{base}
3989857Sobrienis 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
4089857Sobriento indicate bases 8 and 16, respectively, else default to base 10.
4189857SobrienWhen the base is 16 (either explicitly or implicitly), a prefix of
4289857Sobrien@code{0x} is allowed.  The handling of @var{endptr} is as that of
4389857Sobrien@code{strtod} above.  The @code{strtoul} function is the same, except
4489857Sobrienthat the converted value is unsigned.
4589857Sobrien
4689857Sobrien@end deftypefn
4789857Sobrien
4889857Sobrien*/
4989857Sobrien
5077298Sobrien#ifdef HAVE_CONFIG_H
5177298Sobrien#include "config.h"
5277298Sobrien#endif
5377298Sobrien#ifdef HAVE_LIMITS_H
5433965Sjdp#include <limits.h>
5577298Sobrien#endif
5677298Sobrien#ifdef HAVE_SYS_PARAM_H
5777298Sobrien#include <sys/param.h>
5877298Sobrien#endif
5933965Sjdp#include <errno.h>
6077298Sobrien#ifdef NEED_DECLARATION_ERRNO
6177298Sobrienextern int errno;
6233965Sjdp#endif
6377298Sobrien#include "safe-ctype.h"
6433965Sjdp
6533965Sjdp/* FIXME: It'd be nice to configure around these, but the include files are too
6633965Sjdp   painful.  These macros should at least be more portable than hardwired hex
6733965Sjdp   constants. */
6833965Sjdp
6933965Sjdp#ifndef ULONG_MAX
7033965Sjdp#define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
7133965Sjdp#endif
7233965Sjdp
7333965Sjdp#ifndef LONG_MAX
7433965Sjdp#define	LONG_MAX	((long)(ULONG_MAX >> 1))	/* 0x7FFFFFFF */
7533965Sjdp#endif
7633965Sjdp
7733965Sjdp#ifndef LONG_MIN
7833965Sjdp#define	LONG_MIN	((long)(~LONG_MAX))		/* 0x80000000 */
7933965Sjdp#endif
8033965Sjdp
8133965Sjdp/*
8233965Sjdp * Convert a string to a long integer.
8333965Sjdp *
8433965Sjdp * Ignores `locale' stuff.  Assumes that the upper and lower case
8533965Sjdp * alphabets and digits are each contiguous.
8633965Sjdp */
8733965Sjdplong
88218822Sdimstrtol(const char *nptr, char **endptr, register int base)
8933965Sjdp{
9060484Sobrien	register const char *s = nptr;
9133965Sjdp	register unsigned long acc;
9233965Sjdp	register int c;
9333965Sjdp	register unsigned long cutoff;
9433965Sjdp	register int neg = 0, any, cutlim;
9533965Sjdp
9633965Sjdp	/*
9733965Sjdp	 * Skip white space and pick up leading +/- sign if any.
9833965Sjdp	 * If base is 0, allow 0x for hex and 0 for octal, else
9933965Sjdp	 * assume decimal; if base is already 16, allow 0x.
10033965Sjdp	 */
10133965Sjdp	do {
10233965Sjdp		c = *s++;
10377298Sobrien	} while (ISSPACE(c));
10433965Sjdp	if (c == '-') {
10533965Sjdp		neg = 1;
10633965Sjdp		c = *s++;
10733965Sjdp	} else if (c == '+')
10833965Sjdp		c = *s++;
10933965Sjdp	if ((base == 0 || base == 16) &&
11033965Sjdp	    c == '0' && (*s == 'x' || *s == 'X')) {
11133965Sjdp		c = s[1];
11233965Sjdp		s += 2;
11333965Sjdp		base = 16;
11433965Sjdp	}
11533965Sjdp	if (base == 0)
11633965Sjdp		base = c == '0' ? 8 : 10;
11733965Sjdp
11833965Sjdp	/*
11933965Sjdp	 * Compute the cutoff value between legal numbers and illegal
12033965Sjdp	 * numbers.  That is the largest legal value, divided by the
12133965Sjdp	 * base.  An input number that is greater than this value, if
12233965Sjdp	 * followed by a legal input character, is too big.  One that
12333965Sjdp	 * is equal to this value may be valid or not; the limit
12433965Sjdp	 * between valid and invalid numbers is then based on the last
12533965Sjdp	 * digit.  For instance, if the range for longs is
12633965Sjdp	 * [-2147483648..2147483647] and the input base is 10,
12733965Sjdp	 * cutoff will be set to 214748364 and cutlim to either
12833965Sjdp	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
12933965Sjdp	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
13033965Sjdp	 * the number is too big, and we will return a range error.
13133965Sjdp	 *
13233965Sjdp	 * Set any if any `digits' consumed; make it negative to indicate
13333965Sjdp	 * overflow.
13433965Sjdp	 */
13533965Sjdp	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
13633965Sjdp	cutlim = cutoff % (unsigned long)base;
13733965Sjdp	cutoff /= (unsigned long)base;
13833965Sjdp	for (acc = 0, any = 0;; c = *s++) {
13977298Sobrien		if (ISDIGIT(c))
14033965Sjdp			c -= '0';
14177298Sobrien		else if (ISALPHA(c))
14277298Sobrien			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
14333965Sjdp		else
14433965Sjdp			break;
14533965Sjdp		if (c >= base)
14633965Sjdp			break;
147218822Sdim		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
14833965Sjdp			any = -1;
14933965Sjdp		else {
15033965Sjdp			any = 1;
15133965Sjdp			acc *= base;
15233965Sjdp			acc += c;
15333965Sjdp		}
15433965Sjdp	}
15533965Sjdp	if (any < 0) {
15633965Sjdp		acc = neg ? LONG_MIN : LONG_MAX;
15733965Sjdp		errno = ERANGE;
15833965Sjdp	} else if (neg)
15933965Sjdp		acc = -acc;
16033965Sjdp	if (endptr != 0)
16133965Sjdp		*endptr = (char *) (any ? s - 1 : nptr);
16233965Sjdp	return (acc);
16333965Sjdp}
164