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