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