xref: /netbsd/external/bsd/libbind/dist/bsd/strtoul.c (revision e6e30c83)
1*e6e30c83Schristos /*	$NetBSD: strtoul.c,v 1.1.1.2 2012/09/09 16:07:42 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
4b5677b36Schristos static const char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
5b5677b36Schristos static const char rcsid[] = "Id: strtoul.c,v 1.4 2008/02/18 03:49:08 marka Exp ";
6b5677b36Schristos #endif /* LIBC_SCCS and not lint */
7b5677b36Schristos 
8b5677b36Schristos /*
9b5677b36Schristos  * Copyright (c) 1990, 1993
10b5677b36Schristos  *    The Regents of the University of California.  All rights reserved.
11b5677b36Schristos  *
12b5677b36Schristos  * Redistribution and use in source and binary forms, with or without
13b5677b36Schristos  * modification, are permitted provided that the following conditions
14b5677b36Schristos  * are met:
15b5677b36Schristos  * 1. Redistributions of source code must retain the above copyright
16b5677b36Schristos  *    notice, this list of conditions and the following disclaimer.
17b5677b36Schristos  * 2. Redistributions in binary form must reproduce the above copyright
18b5677b36Schristos  *    notice, this list of conditions and the following disclaimer in the
19b5677b36Schristos  *    documentation and/or other materials provided with the distribution.
20b5677b36Schristos  * 3. All advertising materials mentioning features or use of this software
21b5677b36Schristos  *    must display the following acknowledgement:
22b5677b36Schristos  * 	This product includes software developed by the University of
23b5677b36Schristos  * 	California, Berkeley and its contributors.
24b5677b36Schristos  * 4. Neither the name of the University nor the names of its contributors
25b5677b36Schristos  *    may be used to endorse or promote products derived from this software
26b5677b36Schristos  *    without specific prior written permission.
27b5677b36Schristos  *
28b5677b36Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29b5677b36Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30b5677b36Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31b5677b36Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32b5677b36Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33b5677b36Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34b5677b36Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35b5677b36Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36b5677b36Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37b5677b36Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38b5677b36Schristos  * SUCH DAMAGE.
39b5677b36Schristos  */
40b5677b36Schristos 
41b5677b36Schristos #include "port_before.h"
42b5677b36Schristos 
43b5677b36Schristos #include <sys/types.h>
44b5677b36Schristos #include <sys/param.h>
45b5677b36Schristos 
46b5677b36Schristos #include <ctype.h>
47b5677b36Schristos #include <errno.h>
48b5677b36Schristos #include <limits.h>
49b5677b36Schristos #include <stdlib.h>
50b5677b36Schristos 
51b5677b36Schristos #include "port_after.h"
52b5677b36Schristos 
53b5677b36Schristos #ifndef NEED_STRTOUL
54b5677b36Schristos int __strtoul_unneeded__;
55b5677b36Schristos #else
56b5677b36Schristos 
57b5677b36Schristos /*%
58b5677b36Schristos  * Convert a string to an unsigned long integer.
59b5677b36Schristos  *
60b5677b36Schristos  * Ignores `locale' stuff.  Assumes that the upper and lower case
61b5677b36Schristos  * alphabets and digits are each contiguous.
62b5677b36Schristos  */
63b5677b36Schristos u_long
strtoul(const char * nptr,char ** endptr,int base)64b5677b36Schristos strtoul(const char *nptr, char **endptr, int base) {
65b5677b36Schristos 	const char *s = nptr;
66b5677b36Schristos 	u_long acc, cutoff;
67b5677b36Schristos 	int neg, c, any, cutlim;
68b5677b36Schristos 
69b5677b36Schristos 	neg = 0;
70b5677b36Schristos 
71b5677b36Schristos 	/*
72b5677b36Schristos 	 * See strtol for comments as to the logic used.
73b5677b36Schristos 	 */
74b5677b36Schristos 	do {
75b5677b36Schristos 		c = *(const unsigned char *)s++;
76b5677b36Schristos 	} while (isspace(c));
77b5677b36Schristos 	if (c == '-') {
78b5677b36Schristos 		neg = 1;
79b5677b36Schristos 		c = *s++;
80b5677b36Schristos 	} else if (c == '+')
81b5677b36Schristos 		c = *s++;
82b5677b36Schristos 	if ((base == 0 || base == 16) &&
83b5677b36Schristos 	    c == '0' && (*s == 'x' || *s == 'X')) {
84b5677b36Schristos 		c = s[1];
85b5677b36Schristos 		s += 2;
86b5677b36Schristos 		base = 16;
87b5677b36Schristos 	}
88b5677b36Schristos 	if (base == 0)
89b5677b36Schristos 		base = c == '0' ? 8 : 10;
90b5677b36Schristos 	cutoff = (u_long)ULONG_MAX / (u_long)base;
91b5677b36Schristos 	cutlim = (u_long)ULONG_MAX % (u_long)base;
92b5677b36Schristos 	for (acc = 0, any = 0;; c = *(const unsigned char*)s++) {
93b5677b36Schristos 		if (isdigit(c))
94b5677b36Schristos 			c -= '0';
95b5677b36Schristos 		else if (isalpha(c))
96b5677b36Schristos 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
97b5677b36Schristos 		else
98b5677b36Schristos 			break;
99b5677b36Schristos 		if (c >= base)
100b5677b36Schristos 			break;
101b5677b36Schristos 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
102b5677b36Schristos 			any = -1;
103b5677b36Schristos 		else {
104b5677b36Schristos 			any = 1;
105b5677b36Schristos 			acc *= base;
106b5677b36Schristos 			acc += c;
107b5677b36Schristos 		}
108b5677b36Schristos 	}
109b5677b36Schristos 	if (any < 0) {
110b5677b36Schristos 		acc = ULONG_MAX;
111b5677b36Schristos 		errno = ERANGE;
112b5677b36Schristos 	} else if (neg)
113b5677b36Schristos 		acc = -acc;
114b5677b36Schristos 	if (endptr != 0)
115b5677b36Schristos 		DE_CONST((any ? s - 1 : nptr), *endptr);
116b5677b36Schristos 	return (acc);
117b5677b36Schristos }
118b5677b36Schristos 
119b5677b36Schristos #endif /*NEED_STRTOUL*/
120b5677b36Schristos 
121b5677b36Schristos /*! \file */
122