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