xref: /minix3/common/lib/libc/stdlib/_strtoi.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 1990, 1993
5*0a6a1f1dSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
16*0a6a1f1dSLionel Sambuc  *    may be used to endorse or promote products derived from this software
17*0a6a1f1dSLionel Sambuc  *    without specific prior written permission.
18*0a6a1f1dSLionel Sambuc  *
19*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  *
31*0a6a1f1dSLionel Sambuc  * Original version ID:
32*0a6a1f1dSLionel Sambuc  * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
33*0a6a1f1dSLionel Sambuc  *
34*0a6a1f1dSLionel Sambuc  * Created by Kamil Rytarowski, based on ID:
35*0a6a1f1dSLionel Sambuc  * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joerg Exp
36*0a6a1f1dSLionel Sambuc  */
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc /*
39*0a6a1f1dSLionel Sambuc  * function template for strtoi and strtou
40*0a6a1f1dSLionel Sambuc  *
41*0a6a1f1dSLionel Sambuc  * parameters:
42*0a6a1f1dSLionel Sambuc  *	_FUNCNAME    : function name
43*0a6a1f1dSLionel Sambuc  *      __TYPE       : return and range limits type
44*0a6a1f1dSLionel Sambuc  *      __WRAPPED    : wrapped function, strtoimax or strtoumax
45*0a6a1f1dSLionel Sambuc  */
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc #define __WRAPPED_L_(x) x ## _l
48*0a6a1f1dSLionel Sambuc #define __WRAPPED_L__(x) __WRAPPED_L_(x)
49*0a6a1f1dSLionel Sambuc #define __WRAPPED_L __WRAPPED_L__(__WRAPPED)
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || \
52*0a6a1f1dSLionel Sambuc     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
53*0a6a1f1dSLionel Sambuc __TYPE
_FUNCNAME(const char * __restrict nptr,char ** __restrict endptr,int base,__TYPE lo,__TYPE hi,int * rstatus)54*0a6a1f1dSLionel Sambuc _FUNCNAME(const char * __restrict nptr, char ** __restrict endptr, int base,
55*0a6a1f1dSLionel Sambuc           __TYPE lo, __TYPE hi, int * rstatus)
56*0a6a1f1dSLionel Sambuc #else
57*0a6a1f1dSLionel Sambuc #include <locale.h>
58*0a6a1f1dSLionel Sambuc #include "setlocale_local.h"
59*0a6a1f1dSLionel Sambuc #define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
60*0a6a1f1dSLionel Sambuc #define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc static __TYPE
63*0a6a1f1dSLionel Sambuc INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restrict nptr,
64*0a6a1f1dSLionel Sambuc     char ** __restrict endptr, int base,
65*0a6a1f1dSLionel Sambuc     __TYPE lo, __TYPE hi, int * rstatus, locale_t loc)
66*0a6a1f1dSLionel Sambuc #endif
67*0a6a1f1dSLionel Sambuc {
68*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
69*0a6a1f1dSLionel Sambuc 	int serrno;
70*0a6a1f1dSLionel Sambuc #endif
71*0a6a1f1dSLionel Sambuc 	__TYPE im;
72*0a6a1f1dSLionel Sambuc 	char *ep;
73*0a6a1f1dSLionel Sambuc 	int rep;
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc 	_DIAGASSERT(hi >= lo);
76*0a6a1f1dSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc 	_DIAGASSERT(nptr != NULL);
78*0a6a1f1dSLionel Sambuc 	/* endptr may be NULL */
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 	if (endptr == NULL)
81*0a6a1f1dSLionel Sambuc 		endptr = &ep;
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	if (rstatus == NULL)
84*0a6a1f1dSLionel Sambuc 		rstatus = &rep;
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
87*0a6a1f1dSLionel Sambuc 	serrno = errno;
88*0a6a1f1dSLionel Sambuc 	errno = 0;
89*0a6a1f1dSLionel Sambuc #endif
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || \
92*0a6a1f1dSLionel Sambuc     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
93*0a6a1f1dSLionel Sambuc 	im = __WRAPPED(nptr, endptr, base);
94*0a6a1f1dSLionel Sambuc #else
95*0a6a1f1dSLionel Sambuc 	im = __WRAPPED_L(nptr, endptr, base, loc);
96*0a6a1f1dSLionel Sambuc #endif
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
99*0a6a1f1dSLionel Sambuc 	*rstatus = errno;
100*0a6a1f1dSLionel Sambuc 	errno = serrno;
101*0a6a1f1dSLionel Sambuc #endif
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc 	if (*rstatus == 0) {
104*0a6a1f1dSLionel Sambuc 		/* No digits were found */
105*0a6a1f1dSLionel Sambuc 		if (nptr == *endptr)
106*0a6a1f1dSLionel Sambuc 			*rstatus = ECANCELED;
107*0a6a1f1dSLionel Sambuc 		/* There are further characters after number */
108*0a6a1f1dSLionel Sambuc 		else if (**endptr != '\0')
109*0a6a1f1dSLionel Sambuc 			*rstatus = ENOTSUP;
110*0a6a1f1dSLionel Sambuc 	}
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc 	if (im < lo) {
113*0a6a1f1dSLionel Sambuc 		if (*rstatus == 0)
114*0a6a1f1dSLionel Sambuc 			*rstatus = ERANGE;
115*0a6a1f1dSLionel Sambuc 		return lo;
116*0a6a1f1dSLionel Sambuc 	}
117*0a6a1f1dSLionel Sambuc 	if (im > hi) {
118*0a6a1f1dSLionel Sambuc 		if (*rstatus == 0)
119*0a6a1f1dSLionel Sambuc 			*rstatus = ERANGE;
120*0a6a1f1dSLionel Sambuc 		return hi;
121*0a6a1f1dSLionel Sambuc 	}
122*0a6a1f1dSLionel Sambuc 
123*0a6a1f1dSLionel Sambuc 	return im;
124*0a6a1f1dSLionel Sambuc }
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE) && \
127*0a6a1f1dSLionel Sambuc     !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
128*0a6a1f1dSLionel Sambuc __TYPE
_FUNCNAME(const char * __restrict nptr,char ** __restrict endptr,int base,__TYPE lo,__TYPE hi,int * rstatus)129*0a6a1f1dSLionel Sambuc _FUNCNAME(const char * __restrict nptr, char ** __restrict endptr, int base,
130*0a6a1f1dSLionel Sambuc     __TYPE lo, __TYPE hi, int * rstatus)
131*0a6a1f1dSLionel Sambuc {
132*0a6a1f1dSLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, lo, hi,
133*0a6a1f1dSLionel Sambuc 	    rstatus, _current_locale());
134*0a6a1f1dSLionel Sambuc }
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc __TYPE
137*0a6a1f1dSLionel Sambuc INT_FUNCNAME(, _FUNCNAME, _l)(const char * __restrict nptr,
138*0a6a1f1dSLionel Sambuc     char ** __restrict endptr, int base,
139*0a6a1f1dSLionel Sambuc     __TYPE lo, __TYPE hi, int * rstatus, locale_t loc)
140*0a6a1f1dSLionel Sambuc {
141*0a6a1f1dSLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, lo, hi,
142*0a6a1f1dSLionel Sambuc 	    rstatus, loc);
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc #endif
145