xref: /netbsd-src/lib/libc/locale/_wcstol.h (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: _wcstol.h,v 1.1 2003/03/11 09:21:23 tshiozak Exp $ */
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * Original version ID:
36  * @(#)strtol.c	8.1 (Berkeley) 6/4/93
37  * NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp
38  * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp
39  */
40 
41 /*
42  * function template for wcstol, wcstoll and wcstoimax.
43  *
44  * parameters:
45  *	_FUNCNAME : function name
46  *      __INT     : return type
47  *      __INT_MIN : lower limit of the return type
48  *      __INT_MAX : upper limit of the return type
49  */
50 
51 __INT
52 _FUNCNAME(nptr, endptr, base)
53 	const wchar_t *nptr;
54 	wchar_t **endptr;
55 	int base;
56 {
57 	const wchar_t *s;
58 	__INT acc, cutoff;
59 	wint_t wc;
60 	int i;
61 	int neg, any, cutlim;
62 
63 	_DIAGASSERT(nptr != NULL);
64 	/* endptr may be NULL */
65 
66 #ifdef __GNUC__
67 	(void)&acc; (void)&cutoff;
68 #endif
69 
70 	/* check base value */
71 	if (base && (base < 2 || base > 36)) {
72 		errno = EINVAL;
73 		return 0;
74 	}
75 
76 	/*
77 	 * Skip white space and pick up leading +/- sign if any.
78 	 * If base is 0, allow 0x for hex and 0 for octal, else
79 	 * assume decimal; if base is already 16, allow 0x.
80 	 */
81 	s = nptr;
82 	do {
83 		wc = (wchar_t) *s++;
84 	} while (iswspace(wc));
85 	if (wc == L'-') {
86 		neg = 1;
87 		wc = *s++;
88 	} else {
89 		neg = 0;
90 		if (wc == L'+')
91 			wc = *s++;
92 	}
93 	if ((base == 0 || base == 16) &&
94 	    wc == L'0' && (*s == L'x' || *s == L'X')) {
95 		wc = s[1];
96 		s += 2;
97 		base = 16;
98 	}
99 	if (base == 0)
100 		base = wc == L'0' ? 8 : 10;
101 
102 	/*
103 	 * See strtol for comments as to the logic used.
104 	 */
105 	cutoff = neg ? __INT_MIN : __INT_MAX;
106 	cutlim = (int)(cutoff % base);
107 	cutoff /= base;
108 	if (neg) {
109 		if (cutlim > 0) {
110 			cutlim -= base;
111 			cutoff += 1;
112 		}
113 		cutlim = -cutlim;
114 	}
115 	for (acc = 0, any = 0;; wc = (wchar_t) *s++) {
116 		i = __wctoint(wc);
117 		if (i == -1)
118 			break;
119 		if (i >= base)
120 			break;
121 		if (any < 0)
122 			continue;
123 		if (neg) {
124 			if (acc < cutoff || (acc == cutoff && i > cutlim)) {
125 				any = -1;
126 				acc = __INT_MIN;
127 				errno = ERANGE;
128 			} else {
129 				any = 1;
130 				acc *= base;
131 				acc -= i;
132 			}
133 		} else {
134 			if (acc > cutoff || (acc == cutoff && i > cutlim)) {
135 				any = -1;
136 				acc = __INT_MAX;
137 				errno = ERANGE;
138 			} else {
139 				any = 1;
140 				acc *= base;
141 				acc += i;
142 			}
143 		}
144 	}
145 	if (endptr != 0)
146 		/* LINTED interface specification */
147 		*endptr = (wchar_t *)(any ? s - 1 : nptr);
148 	return (acc);
149 }
150