4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1986 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #pragma weak _wcstol = wcstol
40 #define DIGIT(x) (iswdigit(x) ? (x) - L'0' : \
41 iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
42 #define MBASE (L'z' - L'a' + 1 + 10)
45 #define _WLONG_T long long
46 #define _WLONG_MAX LLONG_MAX
47 #define _WLONG_MIN LLONG_MIN
48 #else /* _WCS_LONGLONG */
50 #define _WLONG_MAX LONG_MAX
51 #define _WLONG_MIN LONG_MIN
52 #endif /* _WCS_LONGLONG */
56 wcstoll(const wchar_t *_RESTRICT_KYWD str
, wchar_t **_RESTRICT_KYWD ptr
,
58 #else /* _WCS_LONGLONG */
60 wcstol(const wchar_t *str
, wchar_t **ptr
, int base
)
61 #endif /* _WCS_LONGLONG */
66 _WLONG_T multmin
, limit
;
69 *ptr
= (wchar_t *)str
; /* in case no number is formed */
70 if (base
< 0 || base
> MBASE
) {
72 return (0); /* base is invalid -- should be a fatal error */
75 if (!iswalnum(c
= *str
)) {
90 else if (str
[1] == L
'x' || str
[1] == L
'X')
96 * for any base > 10, the digits incrementally following
97 * 9 are assumed to be "abc...z" or "ABC...Z"
99 if (!iswalnum(c
) || (xx
= DIGIT(c
)) >= base
) {
101 return (0); /* no number formed */
104 if (base
== 16 && c
== L
'0' && iswxdigit(str
[2]) &&
105 (str
[1] == L
'x' || str
[1] == L
'X')) {
106 c
= *(str
+= 2); /* skip over leading "0x" or "0X" */
114 multmin
= limit
/ base
;
117 for (; iswalnum(c
= *++str
) && (xx
= DIGIT(c
)) < base
; ) {
118 /* accumulate neg avoids surprises near MAXLONG */
122 if (val
< limit
+ xx
)
127 *ptr
= (wchar_t *)str
;
128 return (neg
? val
: -val
);
131 while (iswalnum(c
= *++str
) && (xx
= DIGIT(c
)) < base
)
135 *ptr
= (wchar_t *)str
;
137 return (neg
? _WLONG_MIN
: _WLONG_MAX
);