xref: /minix3/common/lib/libc/stdlib/_strtol.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: _strtol.h,v 1.7 2013/05/17 12:55:56 joerg Exp $ */
2b6cbf720SGianluca Guida 
3b6cbf720SGianluca Guida /*-
4b6cbf720SGianluca Guida  * Copyright (c) 1990, 1993
5b6cbf720SGianluca Guida  *	The Regents of the University of California.  All rights reserved.
6b6cbf720SGianluca Guida  *
7b6cbf720SGianluca Guida  * Redistribution and use in source and binary forms, with or without
8b6cbf720SGianluca Guida  * modification, are permitted provided that the following conditions
9b6cbf720SGianluca Guida  * are met:
10b6cbf720SGianluca Guida  * 1. Redistributions of source code must retain the above copyright
11b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer.
12b6cbf720SGianluca Guida  * 2. Redistributions in binary form must reproduce the above copyright
13b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer in the
14b6cbf720SGianluca Guida  *    documentation and/or other materials provided with the distribution.
15b6cbf720SGianluca Guida  * 3. Neither the name of the University nor the names of its contributors
16b6cbf720SGianluca Guida  *    may be used to endorse or promote products derived from this software
17b6cbf720SGianluca Guida  *    without specific prior written permission.
18b6cbf720SGianluca Guida  *
19b6cbf720SGianluca Guida  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20b6cbf720SGianluca Guida  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21b6cbf720SGianluca Guida  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b6cbf720SGianluca Guida  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23b6cbf720SGianluca Guida  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b6cbf720SGianluca Guida  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25b6cbf720SGianluca Guida  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26b6cbf720SGianluca Guida  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27b6cbf720SGianluca Guida  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28b6cbf720SGianluca Guida  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29b6cbf720SGianluca Guida  * SUCH DAMAGE.
30b6cbf720SGianluca Guida  *
31b6cbf720SGianluca Guida  * Original version ID:
32b6cbf720SGianluca Guida  * NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp
33b6cbf720SGianluca Guida  */
34b6cbf720SGianluca Guida 
35b6cbf720SGianluca Guida /*
36b6cbf720SGianluca Guida  * function template for strtol, strtoll and strtoimax.
37b6cbf720SGianluca Guida  *
38b6cbf720SGianluca Guida  * parameters:
39b6cbf720SGianluca Guida  *	_FUNCNAME : function name
40b6cbf720SGianluca Guida  *      __INT     : return type
41b6cbf720SGianluca Guida  *      __INT_MIN : lower limit of the return type
42b6cbf720SGianluca Guida  *      __INT_MAX : upper limit of the return type
43b6cbf720SGianluca Guida  */
44*84d9c625SLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
45b6cbf720SGianluca Guida __INT
_FUNCNAME(const char * nptr,char ** endptr,int base)46b6cbf720SGianluca Guida _FUNCNAME(const char *nptr, char **endptr, int base)
47*84d9c625SLionel Sambuc #else
48*84d9c625SLionel Sambuc #include <locale.h>
49*84d9c625SLionel Sambuc #include "setlocale_local.h"
50*84d9c625SLionel Sambuc #define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
51*84d9c625SLionel Sambuc #define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
52*84d9c625SLionel Sambuc 
53*84d9c625SLionel Sambuc static __INT
54*84d9c625SLionel Sambuc INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr,
55*84d9c625SLionel Sambuc 				   int base, locale_t loc)
56*84d9c625SLionel Sambuc #endif
57b6cbf720SGianluca Guida {
58b6cbf720SGianluca Guida 	const char *s;
59b6cbf720SGianluca Guida 	__INT acc, cutoff;
60b6cbf720SGianluca Guida 	unsigned char c;
61b6cbf720SGianluca Guida 	int i, neg, any, cutlim;
62b6cbf720SGianluca Guida 
63b6cbf720SGianluca Guida 	_DIAGASSERT(nptr != NULL);
64b6cbf720SGianluca Guida 	/* endptr may be NULL */
65b6cbf720SGianluca Guida 
66b6cbf720SGianluca Guida 	/* check base value */
67b6cbf720SGianluca Guida 	if (base && (base < 2 || base > 36)) {
68b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
69b6cbf720SGianluca Guida 		errno = EINVAL;
70b6cbf720SGianluca Guida 		if (endptr != NULL)
71b6cbf720SGianluca Guida 			/* LINTED interface specification */
72b6cbf720SGianluca Guida 			*endptr = __UNCONST(nptr);
73b6cbf720SGianluca Guida 		return 0;
74b6cbf720SGianluca Guida #else
75b6cbf720SGianluca Guida 		panic("%s: invalid base %d", __func__, base);
76b6cbf720SGianluca Guida #endif
77b6cbf720SGianluca Guida 	}
78b6cbf720SGianluca Guida 
79b6cbf720SGianluca Guida 	/*
80b6cbf720SGianluca Guida 	 * Skip white space and pick up leading +/- sign if any.
81b6cbf720SGianluca Guida 	 * If base is 0, allow 0x for hex and 0 for octal, else
82b6cbf720SGianluca Guida 	 * assume decimal; if base is already 16, allow 0x.
83b6cbf720SGianluca Guida 	 */
84b6cbf720SGianluca Guida 	s = nptr;
85*84d9c625SLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || \
86*84d9c625SLionel Sambuc     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
87b6cbf720SGianluca Guida 	do {
88b6cbf720SGianluca Guida 		c = *s++;
89b6cbf720SGianluca Guida 	} while (isspace(c));
90*84d9c625SLionel Sambuc #else
91*84d9c625SLionel Sambuc 	do {
92*84d9c625SLionel Sambuc 		c = *s++;
93*84d9c625SLionel Sambuc 	} while (isspace_l(c, loc));
94*84d9c625SLionel Sambuc #endif
95b6cbf720SGianluca Guida 	if (c == '-') {
96b6cbf720SGianluca Guida 		neg = 1;
97b6cbf720SGianluca Guida 		c = *s++;
98b6cbf720SGianluca Guida 	} else {
99b6cbf720SGianluca Guida 		neg = 0;
100b6cbf720SGianluca Guida 		if (c == '+')
101b6cbf720SGianluca Guida 			c = *s++;
102b6cbf720SGianluca Guida 	}
103b6cbf720SGianluca Guida 	if ((base == 0 || base == 16) &&
104b6cbf720SGianluca Guida 	    c == '0' && (*s == 'x' || *s == 'X')) {
105b6cbf720SGianluca Guida 		c = s[1];
106b6cbf720SGianluca Guida 		s += 2;
107b6cbf720SGianluca Guida 		base = 16;
108b6cbf720SGianluca Guida 	}
109b6cbf720SGianluca Guida 	if (base == 0)
110b6cbf720SGianluca Guida 		base = (c == '0' ? 8 : 10);
111b6cbf720SGianluca Guida 
112b6cbf720SGianluca Guida 	/*
113b6cbf720SGianluca Guida 	 * Compute the cutoff value between legal numbers and illegal
114b6cbf720SGianluca Guida 	 * numbers.  That is the largest legal value, divided by the
115b6cbf720SGianluca Guida 	 * base.  An input number that is greater than this value, if
116b6cbf720SGianluca Guida 	 * followed by a legal input character, is too big.  One that
117b6cbf720SGianluca Guida 	 * is equal to this value may be valid or not; the limit
118b6cbf720SGianluca Guida 	 * between valid and invalid numbers is then based on the last
119b6cbf720SGianluca Guida 	 * digit.  For instance, if the range for longs is
120b6cbf720SGianluca Guida 	 * [-2147483648..2147483647] and the input base is 10,
121b6cbf720SGianluca Guida 	 * cutoff will be set to 214748364 and cutlim to either
122b6cbf720SGianluca Guida 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
123b6cbf720SGianluca Guida 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
124b6cbf720SGianluca Guida 	 * the number is too big, and we will return a range error.
125b6cbf720SGianluca Guida 	 *
126b6cbf720SGianluca Guida 	 * Set any if any `digits' consumed; make it negative to indicate
127b6cbf720SGianluca Guida 	 * overflow.
128b6cbf720SGianluca Guida 	 */
129f14fb602SLionel Sambuc 	cutoff = (__INT)(neg ? __INT_MIN : __INT_MAX);
130b6cbf720SGianluca Guida 	cutlim = (int)(cutoff % base);
131b6cbf720SGianluca Guida 	cutoff /= base;
132b6cbf720SGianluca Guida 	if (neg) {
133b6cbf720SGianluca Guida 		if (cutlim > 0) {
134b6cbf720SGianluca Guida 			cutlim -= base;
135b6cbf720SGianluca Guida 			cutoff += 1;
136b6cbf720SGianluca Guida 		}
137b6cbf720SGianluca Guida 		cutlim = -cutlim;
138b6cbf720SGianluca Guida 	}
139b6cbf720SGianluca Guida 	for (acc = 0, any = 0;; c = *s++) {
140*84d9c625SLionel Sambuc 		if (c >= '0' && c <= '9')
141b6cbf720SGianluca Guida 			i = c - '0';
142*84d9c625SLionel Sambuc 		else if (c >= 'a' && c <= 'z')
143*84d9c625SLionel Sambuc 			i = (c - 'a') + 10;
144*84d9c625SLionel Sambuc 		else if (c >= 'A' && c <= 'Z')
145*84d9c625SLionel Sambuc 			i = (c - 'A') + 10;
146b6cbf720SGianluca Guida 		else
147b6cbf720SGianluca Guida 			break;
148b6cbf720SGianluca Guida 		if (i >= base)
149b6cbf720SGianluca Guida 			break;
150b6cbf720SGianluca Guida 		if (any < 0)
151b6cbf720SGianluca Guida 			continue;
152b6cbf720SGianluca Guida 		if (neg) {
153b6cbf720SGianluca Guida 			if (acc < cutoff || (acc == cutoff && i > cutlim)) {
154b6cbf720SGianluca Guida 				acc = __INT_MIN;
155b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
156b6cbf720SGianluca Guida 				any = -1;
157b6cbf720SGianluca Guida 				errno = ERANGE;
158b6cbf720SGianluca Guida #else
159b6cbf720SGianluca Guida 				any = 0;
160b6cbf720SGianluca Guida 				break;
161b6cbf720SGianluca Guida #endif
162b6cbf720SGianluca Guida 			} else {
163b6cbf720SGianluca Guida 				any = 1;
164b6cbf720SGianluca Guida 				acc *= base;
165b6cbf720SGianluca Guida 				acc -= i;
166b6cbf720SGianluca Guida 			}
167b6cbf720SGianluca Guida 		} else {
168b6cbf720SGianluca Guida 			if (acc > cutoff || (acc == cutoff && i > cutlim)) {
169b6cbf720SGianluca Guida 				acc = __INT_MAX;
170b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
171b6cbf720SGianluca Guida 				any = -1;
172b6cbf720SGianluca Guida 				errno = ERANGE;
173b6cbf720SGianluca Guida #else
174b6cbf720SGianluca Guida 				any = 0;
175b6cbf720SGianluca Guida 				break;
176b6cbf720SGianluca Guida #endif
177b6cbf720SGianluca Guida 			} else {
178b6cbf720SGianluca Guida 				any = 1;
179b6cbf720SGianluca Guida 				acc *= base;
180b6cbf720SGianluca Guida 				acc += i;
181b6cbf720SGianluca Guida 			}
182b6cbf720SGianluca Guida 		}
183b6cbf720SGianluca Guida 	}
184b6cbf720SGianluca Guida 	if (endptr != NULL)
185b6cbf720SGianluca Guida 		/* LINTED interface specification */
186b6cbf720SGianluca Guida 		*endptr = __UNCONST(any ? s - 1 : nptr);
187b6cbf720SGianluca Guida 	return(acc);
188b6cbf720SGianluca Guida }
189*84d9c625SLionel Sambuc 
190*84d9c625SLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE) && \
191*84d9c625SLionel Sambuc     !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
192*84d9c625SLionel Sambuc __INT
_FUNCNAME(const char * nptr,char ** endptr,int base)193*84d9c625SLionel Sambuc _FUNCNAME(const char *nptr, char **endptr, int base)
194*84d9c625SLionel Sambuc {
195*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale());
196*84d9c625SLionel Sambuc }
197*84d9c625SLionel Sambuc 
198*84d9c625SLionel Sambuc __INT
199*84d9c625SLionel Sambuc INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc)
200*84d9c625SLionel Sambuc {
201*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);
202*84d9c625SLionel Sambuc }
203*84d9c625SLionel Sambuc #endif
204