1*9d9553bdSguenther /* $OpenBSD: _wcstol.h,v 1.3 2015/10/01 02:32:07 guenther Exp $ */
238311d31Sespie /* $NetBSD: _wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp $ */
338311d31Sespie
438311d31Sespie /*-
538311d31Sespie * Copyright (c) 1990, 1993
638311d31Sespie * The Regents of the University of California. All rights reserved.
738311d31Sespie *
838311d31Sespie * Redistribution and use in source and binary forms, with or without
938311d31Sespie * modification, are permitted provided that the following conditions
1038311d31Sespie * are met:
1138311d31Sespie * 1. Redistributions of source code must retain the above copyright
1238311d31Sespie * notice, this list of conditions and the following disclaimer.
1338311d31Sespie * 2. Redistributions in binary form must reproduce the above copyright
1438311d31Sespie * notice, this list of conditions and the following disclaimer in the
1538311d31Sespie * documentation and/or other materials provided with the distribution.
1638311d31Sespie * 3. Neither the name of the University nor the names of its contributors
1738311d31Sespie * may be used to endorse or promote products derived from this software
1838311d31Sespie * without specific prior written permission.
1938311d31Sespie *
2038311d31Sespie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2138311d31Sespie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2238311d31Sespie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2338311d31Sespie * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2438311d31Sespie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2538311d31Sespie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2638311d31Sespie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2738311d31Sespie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2838311d31Sespie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2938311d31Sespie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3038311d31Sespie * SUCH DAMAGE.
3138311d31Sespie *
3238311d31Sespie * Original version ID:
3338311d31Sespie * @(#)strtol.c 8.1 (Berkeley) 6/4/93
3438311d31Sespie * NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp
3538311d31Sespie * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp
3638311d31Sespie */
3738311d31Sespie
3838311d31Sespie /*
3938311d31Sespie * function template for wcstol, wcstoll and wcstoimax.
4038311d31Sespie *
4138311d31Sespie * parameters:
4238311d31Sespie * FUNCNAME : function name
4338311d31Sespie * int_type : return type
4438311d31Sespie * MIN_VALUE : lower limit of the return type
4538311d31Sespie * MAX_VALUE : upper limit of the return type
4638311d31Sespie */
4738311d31Sespie
4838311d31Sespie int_type
FUNCNAME(const wchar_t * nptr,wchar_t ** endptr,int base)4938311d31Sespie FUNCNAME(const wchar_t *nptr, wchar_t **endptr, int base)
5038311d31Sespie {
5138311d31Sespie const wchar_t *s;
5238311d31Sespie int_type acc, cutoff;
5338311d31Sespie wint_t wc;
5438311d31Sespie int i;
5538311d31Sespie int neg, any, cutlim;
5638311d31Sespie
5738311d31Sespie /* check base value */
5838311d31Sespie if (base && (base < 2 || base > 36)) {
5938311d31Sespie errno = EINVAL;
6038311d31Sespie return 0;
6138311d31Sespie }
6238311d31Sespie
6338311d31Sespie /*
6438311d31Sespie * Skip white space and pick up leading +/- sign if any.
6538311d31Sespie * If base is 0, allow 0x for hex and 0 for octal, else
6638311d31Sespie * assume decimal; if base is already 16, allow 0x.
6738311d31Sespie */
6838311d31Sespie s = nptr;
6938311d31Sespie do {
7038311d31Sespie wc = (wchar_t) *s++;
7138311d31Sespie } while (iswspace(wc));
7238311d31Sespie if (wc == L'-') {
7338311d31Sespie neg = 1;
7438311d31Sespie wc = *s++;
7538311d31Sespie } else {
7638311d31Sespie neg = 0;
7738311d31Sespie if (wc == L'+')
7838311d31Sespie wc = *s++;
7938311d31Sespie }
8038311d31Sespie if ((base == 0 || base == 16) &&
8138311d31Sespie wc == L'0' && (*s == L'x' || *s == L'X')) {
8238311d31Sespie wc = s[1];
8338311d31Sespie s += 2;
8438311d31Sespie base = 16;
8538311d31Sespie }
8638311d31Sespie if (base == 0)
8738311d31Sespie base = wc == L'0' ? 8 : 10;
8838311d31Sespie
8938311d31Sespie /*
9038311d31Sespie * See strtol for comments as to the logic used.
9138311d31Sespie */
9238311d31Sespie cutoff = neg ? MIN_VALUE : MAX_VALUE;
9338311d31Sespie cutlim = (int)(cutoff % base);
9438311d31Sespie cutoff /= base;
9538311d31Sespie if (neg) {
9638311d31Sespie if (cutlim > 0) {
9738311d31Sespie cutlim -= base;
9838311d31Sespie cutoff += 1;
9938311d31Sespie }
10038311d31Sespie cutlim = -cutlim;
10138311d31Sespie }
10238311d31Sespie for (acc = 0, any = 0;; wc = (wchar_t) *s++) {
10338311d31Sespie i = wctoint(wc);
10438311d31Sespie if (i == -1)
10538311d31Sespie break;
10638311d31Sespie if (i >= base)
10738311d31Sespie break;
10838311d31Sespie if (any < 0)
10938311d31Sespie continue;
11038311d31Sespie if (neg) {
11138311d31Sespie if (acc < cutoff || (acc == cutoff && i > cutlim)) {
11238311d31Sespie any = -1;
11338311d31Sespie acc = MIN_VALUE;
11438311d31Sespie errno = ERANGE;
11538311d31Sespie } else {
11638311d31Sespie any = 1;
11738311d31Sespie acc *= base;
11838311d31Sespie acc -= i;
11938311d31Sespie }
12038311d31Sespie } else {
12138311d31Sespie if (acc > cutoff || (acc == cutoff && i > cutlim)) {
12238311d31Sespie any = -1;
12338311d31Sespie acc = MAX_VALUE;
12438311d31Sespie errno = ERANGE;
12538311d31Sespie } else {
12638311d31Sespie any = 1;
12738311d31Sespie acc *= base;
12838311d31Sespie acc += i;
12938311d31Sespie }
13038311d31Sespie }
13138311d31Sespie }
13238311d31Sespie if (endptr != 0)
13338311d31Sespie *endptr = (wchar_t *)(any ? s - 1 : nptr);
13438311d31Sespie return (acc);
13538311d31Sespie }
13638a75b98Sguenther DEF_STRONG(FUNCNAME);
137