1169695Skan/*
2169695Skan * Copyright (c) 1990 Regents of the University of California.
3169695Skan * All rights reserved.
4169695Skan *
5169695Skan * Redistribution and use in source and binary forms, with or without
6169695Skan * modification, are permitted provided that the following conditions
7169695Skan * are met:
8169695Skan * 1. Redistributions of source code must retain the above copyright
9169695Skan *    notice, this list of conditions and the following disclaimer.
10169695Skan * 2. Redistributions in binary form must reproduce the above copyright
11169695Skan *    notice, this list of conditions and the following disclaimer in the
12169695Skan *    documentation and/or other materials provided with the distribution.
13169695Skan * 3. [rescinded 22 July 1999]
14169695Skan * 4. Neither the name of the University nor the names of its contributors
15169695Skan *    may be used to endorse or promote products derived from this software
16169695Skan *    without specific prior written permission.
17169695Skan *
18169695Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28169695Skan * SUCH DAMAGE.
29169695Skan */
30169695Skan
31169695Skan#ifdef HAVE_CONFIG_H
32169695Skan#include "config.h"
33169695Skan#endif
34169695Skan#ifdef HAVE_LIMITS_H
35169695Skan#include <limits.h>
36169695Skan#endif
37169695Skan#ifdef HAVE_SYS_PARAM_H
38169695Skan#include <sys/param.h>
39169695Skan#endif
40169695Skan#include <errno.h>
41169695Skan#ifdef NEED_DECLARATION_ERRNO
42169695Skanextern int errno;
43169695Skan#endif
44169695Skan#if 0
45169695Skan#include <stdlib.h>
46169695Skan#endif
47169695Skan#include "ansidecl.h"
48169695Skan#include "safe-ctype.h"
49169695Skan
50169695Skan#ifndef ULONG_MAX
51169695Skan#define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
52169695Skan#endif
53169695Skan
54169695Skan/*
55169695Skan * Convert a string to an unsigned long integer.
56169695Skan *
57169695Skan * Ignores `locale' stuff.  Assumes that the upper and lower case
58169695Skan * alphabets and digits are each contiguous.
59169695Skan */
60169695Skanunsigned long
61169695Skanstrtoul(const char *nptr, char **endptr, register int base)
62169695Skan{
63169695Skan	register const char *s = nptr;
64169695Skan	register unsigned long acc;
65169695Skan	register int c;
66169695Skan	register unsigned long cutoff;
67169695Skan	register int neg = 0, any, cutlim;
68169695Skan
69169695Skan	/*
70169695Skan	 * See strtol for comments as to the logic used.
71169695Skan	 */
72169695Skan	do {
73169695Skan		c = *s++;
74169695Skan	} while (ISSPACE(c));
75169695Skan	if (c == '-') {
76169695Skan		neg = 1;
77169695Skan		c = *s++;
78169695Skan	} else if (c == '+')
79169695Skan		c = *s++;
80169695Skan	if ((base == 0 || base == 16) &&
81169695Skan	    c == '0' && (*s == 'x' || *s == 'X')) {
82169695Skan		c = s[1];
83169695Skan		s += 2;
84169695Skan		base = 16;
85169695Skan	}
86169695Skan	if (base == 0)
87169695Skan		base = c == '0' ? 8 : 10;
88169695Skan	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
89169695Skan	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
90169695Skan	for (acc = 0, any = 0;; c = *s++) {
91169695Skan		if (ISDIGIT(c))
92169695Skan			c -= '0';
93169695Skan		else if (ISALPHA(c))
94169695Skan			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
95169695Skan		else
96169695Skan			break;
97169695Skan		if (c >= base)
98169695Skan			break;
99169695Skan		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
100169695Skan			any = -1;
101169695Skan		else {
102169695Skan			any = 1;
103169695Skan			acc *= base;
104169695Skan			acc += c;
105169695Skan		}
106169695Skan	}
107169695Skan	if (any < 0) {
108169695Skan		acc = ULONG_MAX;
109169695Skan		errno = ERANGE;
110169695Skan	} else if (neg)
111169695Skan		acc = -acc;
112169695Skan	if (endptr != 0)
113169695Skan		*endptr = (char *) (any ? s - 1 : nptr);
114169695Skan	return (acc);
115169695Skan}
116