xref: /netbsd-src/common/lib/libc/stdlib/_strtoi.h (revision 22f9aeea7eae77b982013be530e926f315d4d423)
1*22f9aeeaSkre /*	$NetBSD: _strtoi.h,v 1.5 2024/07/24 09:11:27 kre Exp $	*/
2368192c4Schristos 
3368192c4Schristos /*-
4368192c4Schristos  * Copyright (c) 1990, 1993
5368192c4Schristos  *	The Regents of the University of California.  All rights reserved.
6368192c4Schristos  *
7368192c4Schristos  * Redistribution and use in source and binary forms, with or without
8368192c4Schristos  * modification, are permitted provided that the following conditions
9368192c4Schristos  * are met:
10368192c4Schristos  * 1. Redistributions of source code must retain the above copyright
11368192c4Schristos  *    notice, this list of conditions and the following disclaimer.
12368192c4Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13368192c4Schristos  *    notice, this list of conditions and the following disclaimer in the
14368192c4Schristos  *    documentation and/or other materials provided with the distribution.
15368192c4Schristos  * 3. Neither the name of the University nor the names of its contributors
16368192c4Schristos  *    may be used to endorse or promote products derived from this software
17368192c4Schristos  *    without specific prior written permission.
18368192c4Schristos  *
19368192c4Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20368192c4Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21368192c4Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22368192c4Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23368192c4Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24368192c4Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25368192c4Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26368192c4Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27368192c4Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28368192c4Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29368192c4Schristos  * SUCH DAMAGE.
30368192c4Schristos  *
31368192c4Schristos  * Original version ID:
32368192c4Schristos  * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
33368192c4Schristos  *
34368192c4Schristos  * Created by Kamil Rytarowski, based on ID:
35368192c4Schristos  * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joerg Exp
36368192c4Schristos  */
37368192c4Schristos 
38368192c4Schristos /*
39368192c4Schristos  * function template for strtoi and strtou
40368192c4Schristos  *
41368192c4Schristos  * parameters:
42368192c4Schristos  *	_FUNCNAME    : function name
43368192c4Schristos  *      __TYPE       : return and range limits type
44368192c4Schristos  *      __WRAPPED    : wrapped function, strtoimax or strtoumax
45368192c4Schristos  */
46368192c4Schristos 
47368192c4Schristos #define __WRAPPED_L_(x) x ## _l
48368192c4Schristos #define __WRAPPED_L__(x) __WRAPPED_L_(x)
49368192c4Schristos #define __WRAPPED_L __WRAPPED_L__(__WRAPPED)
50368192c4Schristos 
51368192c4Schristos #if defined(_KERNEL) || defined(_STANDALONE) || \
52368192c4Schristos     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
53368192c4Schristos __TYPE
5484a48e08Schristos _FUNCNAME(const char * __restrict nptr, char ** __restrict endptr, int base,
55368192c4Schristos           __TYPE lo, __TYPE hi, int * rstatus)
56368192c4Schristos #else
57368192c4Schristos #include <locale.h>
58368192c4Schristos #include "setlocale_local.h"
59368192c4Schristos #define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
60368192c4Schristos #define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
61368192c4Schristos 
62368192c4Schristos static __TYPE
6384a48e08Schristos INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restrict nptr,
64368192c4Schristos     char ** __restrict endptr, int base,
65368192c4Schristos     __TYPE lo, __TYPE hi, int * rstatus, locale_t loc)
66368192c4Schristos #endif
67368192c4Schristos {
68368192c4Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
69368192c4Schristos 	int serrno;
70368192c4Schristos #endif
71368192c4Schristos 	__TYPE im;
72*22f9aeeaSkre 	char *ep;
73368192c4Schristos 	int rep;
74368192c4Schristos 
75368192c4Schristos 	_DIAGASSERT(hi >= lo);
76368192c4Schristos 
7784a48e08Schristos 	_DIAGASSERT(nptr != NULL);
78368192c4Schristos 	/* endptr may be NULL */
79368192c4Schristos 
80*22f9aeeaSkre 	if (endptr == NULL)
81*22f9aeeaSkre 		endptr = &ep;
82368192c4Schristos 
83368192c4Schristos 	if (rstatus == NULL)
84368192c4Schristos 		rstatus = &rep;
85368192c4Schristos 
86*22f9aeeaSkre 	*rstatus = 0;		/* assume there will be no errors */
87*22f9aeeaSkre 
88*22f9aeeaSkre 	if (base != 0 && (base < 2 || base > 36)) {
89*22f9aeeaSkre #if !defined(_KERNEL) && !defined(_STANDALONE)
90*22f9aeeaSkre 		*rstatus = EINVAL;
91*22f9aeeaSkre 		if (endptr != NULL)
92*22f9aeeaSkre 			/* LINTED interface specification */
93*22f9aeeaSkre 			*endptr = __UNCONST(nptr);
94*22f9aeeaSkre 		return 0;
95*22f9aeeaSkre #else
96*22f9aeeaSkre 		panic("%s: invalid base %d", __func__, base);
97*22f9aeeaSkre #endif
98*22f9aeeaSkre 	}
99*22f9aeeaSkre 
100368192c4Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
101368192c4Schristos 	serrno = errno;
102368192c4Schristos 	errno = 0;
103368192c4Schristos #endif
104368192c4Schristos 
105368192c4Schristos #if defined(_KERNEL) || defined(_STANDALONE) || \
106368192c4Schristos     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
107*22f9aeeaSkre 	im = __WRAPPED(nptr, endptr, base);
108368192c4Schristos #else
109*22f9aeeaSkre 	im = __WRAPPED_L(nptr, endptr, base, loc);
110368192c4Schristos #endif
111368192c4Schristos 
112368192c4Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
113*22f9aeeaSkre 	/* EINVAL here can only mean "nothing converted" */
114*22f9aeeaSkre 	if (errno != EINVAL)
115368192c4Schristos 		*rstatus = errno;
116368192c4Schristos 	errno = serrno;
117368192c4Schristos #endif
118368192c4Schristos 
119368192c4Schristos 	/* No digits were found */
120*22f9aeeaSkre 	if (*rstatus == 0 && nptr == *endptr)
121368192c4Schristos 		*rstatus = ECANCELED;
122368192c4Schristos 
123368192c4Schristos 	if (im < lo) {
124368192c4Schristos 		if (*rstatus == 0)
125368192c4Schristos 			*rstatus = ERANGE;
126368192c4Schristos 		return lo;
127368192c4Schristos 	}
128f1987b28Schristos 
129368192c4Schristos 	if (im > hi) {
130368192c4Schristos 		if (*rstatus == 0)
131368192c4Schristos 			*rstatus = ERANGE;
132368192c4Schristos 		return hi;
133368192c4Schristos 	}
134368192c4Schristos 
135f1987b28Schristos 	/* There are further characters after number */
136*22f9aeeaSkre 	if (*rstatus == 0 && **endptr != '\0')
137f1987b28Schristos 		*rstatus = ENOTSUP;
138f1987b28Schristos 
139368192c4Schristos 	return im;
140368192c4Schristos }
141368192c4Schristos 
142368192c4Schristos #if !defined(_KERNEL) && !defined(_STANDALONE) && \
143368192c4Schristos     !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
144368192c4Schristos __TYPE
14584a48e08Schristos _FUNCNAME(const char * __restrict nptr, char ** __restrict endptr, int base,
146368192c4Schristos     __TYPE lo, __TYPE hi, int * rstatus)
147368192c4Schristos {
14884a48e08Schristos 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, lo, hi,
149368192c4Schristos 	    rstatus, _current_locale());
150368192c4Schristos }
151368192c4Schristos 
152368192c4Schristos __TYPE
15384a48e08Schristos INT_FUNCNAME(, _FUNCNAME, _l)(const char * __restrict nptr,
154368192c4Schristos     char ** __restrict endptr, int base,
155368192c4Schristos     __TYPE lo, __TYPE hi, int * rstatus, locale_t loc)
156368192c4Schristos {
15784a48e08Schristos 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, lo, hi,
158368192c4Schristos 	    rstatus, loc);
159368192c4Schristos }
160368192c4Schristos #endif
161