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