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