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