xref: /onnv-gate/usr/src/lib/libc/port/i18n/wcstol.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*	Copyright (c) 1986 AT&T	*/
30*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #ifndef	_WCS_LONGLONG
34*0Sstevel@tonic-gate #pragma weak wcstol = _wcstol
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "lint.h"
38*0Sstevel@tonic-gate #include <limits.h>
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <wchar.h>
41*0Sstevel@tonic-gate #define	DIGIT(x)	(iswdigit(x) ? (x) - L'0' : \
42*0Sstevel@tonic-gate 			iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
43*0Sstevel@tonic-gate #define	MBASE	(L'z' - L'a' + 1 + 10)
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #ifdef	_WCS_LONGLONG
46*0Sstevel@tonic-gate #define	_WLONG_T	long long
47*0Sstevel@tonic-gate #define	_WLONG_MAX	LLONG_MAX
48*0Sstevel@tonic-gate #define	_WLONG_MIN	LLONG_MIN
49*0Sstevel@tonic-gate #else  /* _WCS_LONGLONG */
50*0Sstevel@tonic-gate #define	_WLONG_T	long
51*0Sstevel@tonic-gate #define	_WLONG_MAX	LONG_MAX
52*0Sstevel@tonic-gate #define	_WLONG_MIN	LONG_MIN
53*0Sstevel@tonic-gate #endif /* _WCS_LONGLONG */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #ifdef	_WCS_LONGLONG
56*0Sstevel@tonic-gate long long
57*0Sstevel@tonic-gate wcstoll(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
58*0Sstevel@tonic-gate     int base)
59*0Sstevel@tonic-gate #else  /* _WCS_LONGLONG */
60*0Sstevel@tonic-gate long
61*0Sstevel@tonic-gate _wcstol(const wchar_t *str, wchar_t **ptr, int base)
62*0Sstevel@tonic-gate #endif /* _WCS_LONGLONG */
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	_WLONG_T	val;
65*0Sstevel@tonic-gate 	wchar_t	c;
66*0Sstevel@tonic-gate 	int	xx, neg = 0;
67*0Sstevel@tonic-gate 	_WLONG_T	multmin, limit;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (ptr != NULL)
70*0Sstevel@tonic-gate 		*ptr = (wchar_t *)str; /* in case no number is formed */
71*0Sstevel@tonic-gate 	if (base < 0 || base > MBASE) {
72*0Sstevel@tonic-gate 		errno = EINVAL;
73*0Sstevel@tonic-gate 		return (0); /* base is invalid -- should be a fatal error */
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (!iswalnum(c = *str)) {
77*0Sstevel@tonic-gate 		while (iswspace(c)) {
78*0Sstevel@tonic-gate 			c = *++str;
79*0Sstevel@tonic-gate 		}
80*0Sstevel@tonic-gate 		switch (c) {
81*0Sstevel@tonic-gate 		case L'-':
82*0Sstevel@tonic-gate 			neg++;
83*0Sstevel@tonic-gate 			/*FALLTHRU*/
84*0Sstevel@tonic-gate 		case L'+':
85*0Sstevel@tonic-gate 			c = *++str;
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	if (base == 0) {
89*0Sstevel@tonic-gate 		if (c != L'0')
90*0Sstevel@tonic-gate 			base = 10;
91*0Sstevel@tonic-gate 		else if (str[1] == L'x' || str[1] == L'X')
92*0Sstevel@tonic-gate 			base = 16;
93*0Sstevel@tonic-gate 		else
94*0Sstevel@tonic-gate 			base = 8;
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	/*
97*0Sstevel@tonic-gate 	 * for any base > 10, the digits incrementally following
98*0Sstevel@tonic-gate 	 *	9 are assumed to be "abc...z" or "ABC...Z"
99*0Sstevel@tonic-gate 	 */
100*0Sstevel@tonic-gate 	if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
101*0Sstevel@tonic-gate 		errno = EINVAL;
102*0Sstevel@tonic-gate 		return (0); /* no number formed */
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
106*0Sstevel@tonic-gate 	    (str[1] == L'x' || str[1] == L'X')) {
107*0Sstevel@tonic-gate 		c = *(str += 2); /* skip over leading "0x" or "0X" */
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if (neg) {
111*0Sstevel@tonic-gate 		limit = _WLONG_MIN;
112*0Sstevel@tonic-gate 	} else {
113*0Sstevel@tonic-gate 		limit = -_WLONG_MAX;
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	multmin = limit / base;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	val = -DIGIT(c);
118*0Sstevel@tonic-gate 	for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
119*0Sstevel@tonic-gate 		/* accumulate neg avoids surprises near MAXLONG */
120*0Sstevel@tonic-gate 		if (val < multmin)
121*0Sstevel@tonic-gate 			goto overflow;
122*0Sstevel@tonic-gate 		val *= base;
123*0Sstevel@tonic-gate 		if (val < limit + xx)
124*0Sstevel@tonic-gate 			goto overflow;
125*0Sstevel@tonic-gate 		val -= xx;
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	if (ptr != NULL)
128*0Sstevel@tonic-gate 		*ptr = (wchar_t *)str;
129*0Sstevel@tonic-gate 	return (neg ? val : -val);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate overflow:
132*0Sstevel@tonic-gate 	while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
133*0Sstevel@tonic-gate 		;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if (ptr != NULL)
136*0Sstevel@tonic-gate 		*ptr = (wchar_t *)str;
137*0Sstevel@tonic-gate 	errno = ERANGE;
138*0Sstevel@tonic-gate 	return (neg ? _WLONG_MIN : _WLONG_MAX);
139*0Sstevel@tonic-gate }
140