xref: /illumos-gate/usr/src/common/util/strtol.c (revision 8c067cfd3aea0c49a166f9fb38114b56a9160ac6)
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 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #if !defined(_BOOT) && !defined(_KMDB)
33 #include "lint.h"
34 #endif /* !_BOOT && !_KMDB */
35 #include <errno.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include <stdlib.h>
40 
41 #define	DIGIT(x)	\
42 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
43 
44 #define	MBASE	('z' - 'a' + 1 + 10)
45 
46 /*
47  * The following macro is a local version of isalnum() which limits
48  * alphabetic characters to the ranges a-z and A-Z; locale dependent
49  * characters will not return 1. The members of a-z and A-Z are
50  * assumed to be in ascending order and contiguous
51  */
52 #define	lisalnum(x)	\
53 	(isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
54 
55 long
56 strtol(const char *str, char **nptr, int base)
57 {
58 	long val;
59 	int c;
60 	int xx, neg = 0;
61 	long	multmin;
62 	long	limit;
63 	const char **ptr = (const char **)nptr;
64 	const unsigned char	*ustr = (const unsigned char *)str;
65 
66 	if (ptr != (const char **)0)
67 		*ptr = (char *)ustr; /* in case no number is formed */
68 	if (base < 0 || base > MBASE || base == 1) {
69 		errno = EINVAL;
70 		return (0); /* base is invalid -- should be a fatal error */
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 		return (0); /* no number formed */
96 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
97 	    isxdigit(ustr[2]))
98 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
99 
100 	/* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */
101 	if (neg)
102 		limit = LONG_MIN;
103 	else
104 		limit = -LONG_MAX;
105 	multmin = limit / (long)base;
106 	val = -DIGIT(c);
107 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
108 		/* accumulate neg avoids surprises near MAXLONG */
109 		if (val < multmin)
110 			goto overflow;
111 		val *= base;
112 		if (val < limit + xx)
113 			goto overflow;
114 		val -= xx;
115 		c = *++ustr;
116 	}
117 	if (ptr != (const char **)0)
118 		*ptr = (char *)ustr;
119 	return (neg ? val : -val);
120 
121 overflow:
122 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
123 		;
124 	if (ptr != (const char **)0)
125 		*ptr = (char *)ustr;
126 	errno = ERANGE;
127 	return (neg ? LONG_MIN : LONG_MAX);
128 }
129