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