xref: /titanic_41/usr/src/common/util/strtoul.c (revision 8d483882aa3390058094b043f3d62187b5d1de03)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.8	*/
32 
33 #if !defined(_BOOT) && !defined(_KMDB)
34 #include "synonyms.h"
35 #endif /* !_BOOT && !_KMDB */
36 #include <errno.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <sys/types.h>
41 
42 #define	DIGIT(x)	\
43 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
44 
45 #define	MBASE	('z' - 'a' + 1 + 10)
46 
47 /*
48  * The following macro is a local version of isalnum() which limits
49  * alphabetic characters to the ranges a-z and A-Z; locale dependent
50  * characters will not return 1. The members of a-z and A-Z are
51  * assumed to be in ascending order and contiguous
52  */
53 #define	lisalnum(x)	\
54 	(isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
55 
56 unsigned long
57 strtoul(const char *str, char **nptr, int base)
58 {
59 	unsigned long val;
60 	int c;
61 	int xx;
62 	unsigned long	multmax;
63 	int neg = 0;
64 	const char **ptr = (const char **)nptr;
65 	const unsigned char	*ustr = (const unsigned char *)str;
66 
67 	if (ptr != (const char **)0)
68 		*ptr = (char *)ustr; /* in case no number is formed */
69 	if (base < 0 || base > MBASE || base == 1) {
70 		errno = EINVAL;
71 		return (0); /* base is invalid -- should be a fatal error */
72 	}
73 	if (!isalnum(c = *ustr)) {
74 		while (isspace(c))
75 			c = *++ustr;
76 		switch (c) {
77 		case '-':
78 			neg++;
79 			/* FALLTHROUGH */
80 		case '+':
81 			c = *++ustr;
82 		}
83 	}
84 	if (base == 0)
85 		if (c != '0')
86 			base = 10;
87 		else if (ustr[1] == 'x' || ustr[1] == 'X')
88 			base = 16;
89 		else
90 			base = 8;
91 	/*
92 	 * for any base > 10, the digits incrementally following
93 	 *	9 are assumed to be "abc...z" or "ABC...Z"
94 	 */
95 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base)
96 		return (0); /* no number formed */
97 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
98 	    isxdigit(ustr[2]))
99 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
100 
101 	multmax = ULONG_MAX / (unsigned long)base;
102 	val = DIGIT(c);
103 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
104 		if (val > multmax)
105 			goto overflow;
106 		val *= base;
107 		if (ULONG_MAX - val < xx)
108 			goto overflow;
109 		val += xx;
110 		c = *++ustr;
111 	}
112 	if (ptr != (const char **)0)
113 		*ptr = (char *)ustr;
114 	return (neg ? -val : val);
115 
116 overflow:
117 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
118 		;
119 	if (ptr != (const char **)0)
120 		*ptr = (char *)ustr;
121 	errno = ERANGE;
122 	return (ULONG_MAX);
123 }
124