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