1*0a6a1f1dSLionel Sambuc /* $NetBSD: _strtoi.h,v 1.1 2015/01/22 02:15:59 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 __TYPE
_FUNCNAME(const char * __restrict nptr,char ** __restrict endptr,int base,__TYPE lo,__TYPE hi,int * rstatus)48*0a6a1f1dSLionel Sambuc _FUNCNAME(const char * __restrict nptr, char ** __restrict endptr, int base,
49*0a6a1f1dSLionel Sambuc __TYPE lo, __TYPE hi, int * rstatus)
50*0a6a1f1dSLionel Sambuc {
51*0a6a1f1dSLionel Sambuc int serrno;
52*0a6a1f1dSLionel Sambuc __TYPE im;
53*0a6a1f1dSLionel Sambuc char *ep;
54*0a6a1f1dSLionel Sambuc int rep;
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc /* endptr may be NULL */
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc if (endptr == NULL)
59*0a6a1f1dSLionel Sambuc endptr = &ep;
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc if (rstatus == NULL)
62*0a6a1f1dSLionel Sambuc rstatus = &rep;
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc serrno = errno;
65*0a6a1f1dSLionel Sambuc errno = 0;
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc im = __WRAPPED(nptr, endptr, base);
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc *rstatus = errno;
70*0a6a1f1dSLionel Sambuc errno = serrno;
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc if (*rstatus == 0) {
73*0a6a1f1dSLionel Sambuc /* No digits were found */
74*0a6a1f1dSLionel Sambuc if (nptr == *endptr)
75*0a6a1f1dSLionel Sambuc *rstatus = ECANCELED;
76*0a6a1f1dSLionel Sambuc /* There are further characters after number */
77*0a6a1f1dSLionel Sambuc else if (**endptr != '\0')
78*0a6a1f1dSLionel Sambuc *rstatus = ENOTSUP;
79*0a6a1f1dSLionel Sambuc }
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc if (im < lo) {
82*0a6a1f1dSLionel Sambuc if (*rstatus == 0)
83*0a6a1f1dSLionel Sambuc *rstatus = ERANGE;
84*0a6a1f1dSLionel Sambuc return lo;
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc if (im > hi) {
87*0a6a1f1dSLionel Sambuc if (*rstatus == 0)
88*0a6a1f1dSLionel Sambuc *rstatus = ERANGE;
89*0a6a1f1dSLionel Sambuc return hi;
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc return im;
93*0a6a1f1dSLionel Sambuc }
94