1*6ca2c52aSchristos /*
2*6ca2c52aSchristos  * Copyright (c) 1990 Regents of the University of California.
3*6ca2c52aSchristos  * All rights reserved.
4*6ca2c52aSchristos  *
5*6ca2c52aSchristos  * Redistribution and use in source and binary forms, with or without
6*6ca2c52aSchristos  * modification, are permitted provided that the following conditions
7*6ca2c52aSchristos  * are met:
8*6ca2c52aSchristos  * 1. Redistributions of source code must retain the above copyright
9*6ca2c52aSchristos  *    notice, this list of conditions and the following disclaimer.
10*6ca2c52aSchristos  * 2. Redistributions in binary form must reproduce the above copyright
11*6ca2c52aSchristos  *    notice, this list of conditions and the following disclaimer in the
12*6ca2c52aSchristos  *    documentation and/or other materials provided with the distribution.
13*6ca2c52aSchristos  * 3. [rescinded 22 July 1999]
14*6ca2c52aSchristos  * 4. Neither the name of the University nor the names of its contributors
15*6ca2c52aSchristos  *    may be used to endorse or promote products derived from this software
16*6ca2c52aSchristos  *    without specific prior written permission.
17*6ca2c52aSchristos  *
18*6ca2c52aSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*6ca2c52aSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*6ca2c52aSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*6ca2c52aSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*6ca2c52aSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*6ca2c52aSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*6ca2c52aSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*6ca2c52aSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*6ca2c52aSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*6ca2c52aSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*6ca2c52aSchristos  * SUCH DAMAGE.
29*6ca2c52aSchristos  */
30*6ca2c52aSchristos 
31*6ca2c52aSchristos #ifdef HAVE_CONFIG_H
32*6ca2c52aSchristos #include "config.h"
33*6ca2c52aSchristos #endif
34*6ca2c52aSchristos #ifdef HAVE_LIMITS_H
35*6ca2c52aSchristos #include <limits.h>
36*6ca2c52aSchristos #endif
37*6ca2c52aSchristos #ifdef HAVE_SYS_PARAM_H
38*6ca2c52aSchristos #include <sys/param.h>
39*6ca2c52aSchristos #endif
40*6ca2c52aSchristos #include <errno.h>
41*6ca2c52aSchristos #ifdef NEED_DECLARATION_ERRNO
42*6ca2c52aSchristos extern int errno;
43*6ca2c52aSchristos #endif
44*6ca2c52aSchristos #if 0
45*6ca2c52aSchristos #include <stdlib.h>
46*6ca2c52aSchristos #endif
47*6ca2c52aSchristos #include "ansidecl.h"
48*6ca2c52aSchristos #include "safe-ctype.h"
49*6ca2c52aSchristos 
50*6ca2c52aSchristos #ifndef ULONG_MAX
51*6ca2c52aSchristos #define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
52*6ca2c52aSchristos #endif
53*6ca2c52aSchristos 
54*6ca2c52aSchristos /*
55*6ca2c52aSchristos  * Convert a string to an unsigned long integer.
56*6ca2c52aSchristos  *
57*6ca2c52aSchristos  * Ignores `locale' stuff.  Assumes that the upper and lower case
58*6ca2c52aSchristos  * alphabets and digits are each contiguous.
59*6ca2c52aSchristos  */
60*6ca2c52aSchristos unsigned long
strtoul(const char * nptr,char ** endptr,register int base)61*6ca2c52aSchristos strtoul(const char *nptr, char **endptr, register int base)
62*6ca2c52aSchristos {
63*6ca2c52aSchristos 	register const char *s = nptr;
64*6ca2c52aSchristos 	register unsigned long acc;
65*6ca2c52aSchristos 	register int c;
66*6ca2c52aSchristos 	register unsigned long cutoff;
67*6ca2c52aSchristos 	register int neg = 0, any, cutlim;
68*6ca2c52aSchristos 
69*6ca2c52aSchristos 	/*
70*6ca2c52aSchristos 	 * See strtol for comments as to the logic used.
71*6ca2c52aSchristos 	 */
72*6ca2c52aSchristos 	do {
73*6ca2c52aSchristos 		c = *s++;
74*6ca2c52aSchristos 	} while (ISSPACE(c));
75*6ca2c52aSchristos 	if (c == '-') {
76*6ca2c52aSchristos 		neg = 1;
77*6ca2c52aSchristos 		c = *s++;
78*6ca2c52aSchristos 	} else if (c == '+')
79*6ca2c52aSchristos 		c = *s++;
80*6ca2c52aSchristos 	if ((base == 0 || base == 16) &&
81*6ca2c52aSchristos 	    c == '0' && (*s == 'x' || *s == 'X')) {
82*6ca2c52aSchristos 		c = s[1];
83*6ca2c52aSchristos 		s += 2;
84*6ca2c52aSchristos 		base = 16;
85*6ca2c52aSchristos 	}
86*6ca2c52aSchristos 	if (base == 0)
87*6ca2c52aSchristos 		base = c == '0' ? 8 : 10;
88*6ca2c52aSchristos 	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
89*6ca2c52aSchristos 	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
90*6ca2c52aSchristos 	for (acc = 0, any = 0;; c = *s++) {
91*6ca2c52aSchristos 		if (ISDIGIT(c))
92*6ca2c52aSchristos 			c -= '0';
93*6ca2c52aSchristos 		else if (ISALPHA(c))
94*6ca2c52aSchristos 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
95*6ca2c52aSchristos 		else
96*6ca2c52aSchristos 			break;
97*6ca2c52aSchristos 		if (c >= base)
98*6ca2c52aSchristos 			break;
99*6ca2c52aSchristos 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
100*6ca2c52aSchristos 			any = -1;
101*6ca2c52aSchristos 		else {
102*6ca2c52aSchristos 			any = 1;
103*6ca2c52aSchristos 			acc *= base;
104*6ca2c52aSchristos 			acc += c;
105*6ca2c52aSchristos 		}
106*6ca2c52aSchristos 	}
107*6ca2c52aSchristos 	if (any < 0) {
108*6ca2c52aSchristos 		acc = ULONG_MAX;
109*6ca2c52aSchristos 		errno = ERANGE;
110*6ca2c52aSchristos 	} else if (neg)
111*6ca2c52aSchristos 		acc = -acc;
112*6ca2c52aSchristos 	if (endptr != 0)
113*6ca2c52aSchristos 		*endptr = (char *) (any ? s - 1 : nptr);
114*6ca2c52aSchristos 	return (acc);
115*6ca2c52aSchristos }
116