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