xref: /illumos-gate/usr/src/common/util/strtol.c (revision 1fa2a66491e7d8ae0be84e7da4da8e812480c710)
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 2011 Nexenta Systems, Inc. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*	Copyright (c) 1988 AT&T	*/
32 /*	  All Rights Reserved	*/
33 
34 #if	defined(_KERNEL)
35 #include <sys/null.h>
36 #endif	/* _KERNEL */
37 
38 #if	defined(_KERNEL) && !defined(_BOOT)
39 #include <sys/errno.h>
40 #else	/* _KERNEL && !_BOOT */
41 #if	!defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
42 #include "lint.h"
43 #endif	/* !_BOOT && !_KMDB && !_STANDALONE */
44 #if	defined(_STANDALONE)
45 #include <sys/cdefs.h>
46 #include <stand.h>
47 #include <limits.h>
48 #else
49 #include <errno.h>
50 #include <ctype.h>
51 #include <limits.h>
52 #include <stdlib.h>
53 #endif	/* _STANDALONE */
54 #endif	/* _KERNEL && !_BOOT */
55 #include "strtolctype.h"
56 #include <sys/types.h>
57 
58 #if	defined(_KERNEL) && !defined(_BOOT)
59 int
60 ddi_strtol(const char *str, char **nptr, int base, long *result)
61 #else	/* _KERNEL && !_BOOT */
62 long
63 strtol(const char *str, char **nptr, int base)
64 #endif	/* _KERNEL && !_BOOT */
65 {
66 	long val;
67 	int c;
68 	int xx;
69 	int neg = 0;
70 	long multmin;
71 	long limit;
72 	const char **ptr = (const char **)nptr;
73 	const unsigned char *ustr = (const unsigned char *)str;
74 
75 	if (ptr != NULL)
76 		*ptr = (char *)ustr; /* in case no number is formed */
77 	if (base < 0 || base > MBASE || base == 1) {
78 		/* base is invalid -- should be a fatal error */
79 #if	defined(_KERNEL) && !defined(_BOOT)
80 		return (EINVAL);
81 #else	/* _KERNEL && !_BOOT */
82 		errno = EINVAL;
83 		return (0);
84 #endif	/* _KERNEL && !_BOOT */
85 	}
86 	if (!isalnum(c = *ustr)) {
87 		while (isspace(c))
88 			c = *++ustr;
89 		switch (c) {
90 		case '-':
91 			neg++;
92 			/* FALLTHROUGH */
93 		case '+':
94 			c = *++ustr;
95 		}
96 	}
97 	if (base == 0) {
98 		if (c != '0')
99 			base = 10;
100 		else if (ustr[1] == 'x' || ustr[1] == 'X')
101 			base = 16;
102 		else
103 			base = 8;
104 	}
105 	/*
106 	 * for any base > 10, the digits incrementally following
107 	 *	9 are assumed to be "abc...z" or "ABC...Z"
108 	 */
109 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
110 		/* no number formed */
111 #if	defined(_KERNEL) && !defined(_BOOT)
112 		return (EINVAL);
113 #else	/* _KERNEL && !_BOOT */
114 		errno = EINVAL;
115 		return (0);
116 #endif	/* _KERNEL && !_BOOT */
117 	}
118 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
119 	    isxdigit(ustr[2]))
120 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
121 
122 	/* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */
123 	if (neg)
124 		limit = LONG_MIN;
125 	else
126 		limit = -LONG_MAX;
127 	multmin = limit / (long)base;
128 	val = -DIGIT(c);
129 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
130 		/* accumulate neg avoids surprises near LONG_MAX */
131 		if (val < multmin)
132 			goto overflow;
133 		val *= base;
134 		if (val < limit + xx)
135 			goto overflow;
136 		val -= xx;
137 		c = *++ustr;
138 	}
139 	if (ptr != NULL)
140 		*ptr = (char *)ustr;
141 #if	defined(_KERNEL) && !defined(_BOOT)
142 	*result = neg ? val : -val;
143 	return (0);
144 #else	/* _KERNEL && !_BOOT */
145 	return (neg ? val : -val);
146 #endif	/* _KERNEL && !_BOOT */
147 
148 overflow:
149 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
150 		;
151 	if (ptr != NULL)
152 		*ptr = (char *)ustr;
153 #if	defined(_KERNEL) && !defined(_BOOT)
154 	return (ERANGE);
155 #else	/* _KERNEL && !_BOOT */
156 	errno = ERANGE;
157 	return (neg ? LONG_MIN : LONG_MAX);
158 #endif	/* _KERNEL && !_BOOT */
159 }
160