xref: /illumos-gate/usr/src/common/util/strtol.c (revision 7c478bd9)
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 2.15	*/
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 <sys/types.h>
40 #include <stdlib.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 long
57 strtol(const char *str, char **nptr, int base)
58 {
59 	long val;
60 	int c;
61 	int xx, neg = 0;
62 	long	multmin;
63 	long	limit;
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 	/* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */
102 	if (neg)
103 		limit = LONG_MIN;
104 	else
105 		limit = -LONG_MAX;
106 	multmin = limit / (long)base;
107 	val = -DIGIT(c);
108 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
109 		/* accumulate neg avoids surprises near MAXLONG */
110 		if (val < multmin)
111 			goto overflow;
112 		val *= base;
113 		if (val < limit + xx)
114 			goto overflow;
115 		val -= xx;
116 		c = *++ustr;
117 	}
118 	if (ptr != (const char **)0)
119 		*ptr = (char *)ustr;
120 	return (neg ? val : -val);
121 
122 overflow:
123 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
124 		;
125 	if (ptr != (const char **)0)
126 		*ptr = (char *)ustr;
127 	errno = ERANGE;
128 	return (neg ? LONG_MIN : LONG_MAX);
129 }
130