xref: /onnv-gate/usr/src/lib/libc/port/i18n/wcstol.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1986 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #ifndef	_WCS_LONGLONG
33*6812Sraf #pragma weak _wcstol = wcstol
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
36*6812Sraf #include "lint.h"
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <wchar.h>
400Sstevel@tonic-gate #define	DIGIT(x)	(iswdigit(x) ? (x) - L'0' : \
410Sstevel@tonic-gate 			iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
420Sstevel@tonic-gate #define	MBASE	(L'z' - L'a' + 1 + 10)
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #ifdef	_WCS_LONGLONG
450Sstevel@tonic-gate #define	_WLONG_T	long long
460Sstevel@tonic-gate #define	_WLONG_MAX	LLONG_MAX
470Sstevel@tonic-gate #define	_WLONG_MIN	LLONG_MIN
480Sstevel@tonic-gate #else  /* _WCS_LONGLONG */
490Sstevel@tonic-gate #define	_WLONG_T	long
500Sstevel@tonic-gate #define	_WLONG_MAX	LONG_MAX
510Sstevel@tonic-gate #define	_WLONG_MIN	LONG_MIN
520Sstevel@tonic-gate #endif /* _WCS_LONGLONG */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef	_WCS_LONGLONG
550Sstevel@tonic-gate long long
wcstoll(const wchar_t * _RESTRICT_KYWD str,wchar_t ** _RESTRICT_KYWD ptr,int base)560Sstevel@tonic-gate wcstoll(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
570Sstevel@tonic-gate     int base)
580Sstevel@tonic-gate #else  /* _WCS_LONGLONG */
590Sstevel@tonic-gate long
60*6812Sraf wcstol(const wchar_t *str, wchar_t **ptr, int base)
610Sstevel@tonic-gate #endif /* _WCS_LONGLONG */
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	_WLONG_T	val;
640Sstevel@tonic-gate 	wchar_t	c;
650Sstevel@tonic-gate 	int	xx, neg = 0;
660Sstevel@tonic-gate 	_WLONG_T	multmin, limit;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (ptr != NULL)
690Sstevel@tonic-gate 		*ptr = (wchar_t *)str; /* in case no number is formed */
700Sstevel@tonic-gate 	if (base < 0 || base > MBASE) {
710Sstevel@tonic-gate 		errno = EINVAL;
720Sstevel@tonic-gate 		return (0); /* base is invalid -- should be a fatal error */
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!iswalnum(c = *str)) {
760Sstevel@tonic-gate 		while (iswspace(c)) {
770Sstevel@tonic-gate 			c = *++str;
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 		switch (c) {
800Sstevel@tonic-gate 		case L'-':
810Sstevel@tonic-gate 			neg++;
820Sstevel@tonic-gate 			/*FALLTHRU*/
830Sstevel@tonic-gate 		case L'+':
840Sstevel@tonic-gate 			c = *++str;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	if (base == 0) {
880Sstevel@tonic-gate 		if (c != L'0')
890Sstevel@tonic-gate 			base = 10;
900Sstevel@tonic-gate 		else if (str[1] == L'x' || str[1] == L'X')
910Sstevel@tonic-gate 			base = 16;
920Sstevel@tonic-gate 		else
930Sstevel@tonic-gate 			base = 8;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	/*
960Sstevel@tonic-gate 	 * for any base > 10, the digits incrementally following
970Sstevel@tonic-gate 	 *	9 are assumed to be "abc...z" or "ABC...Z"
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
1000Sstevel@tonic-gate 		errno = EINVAL;
1010Sstevel@tonic-gate 		return (0); /* no number formed */
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
1050Sstevel@tonic-gate 	    (str[1] == L'x' || str[1] == L'X')) {
1060Sstevel@tonic-gate 		c = *(str += 2); /* skip over leading "0x" or "0X" */
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (neg) {
1100Sstevel@tonic-gate 		limit = _WLONG_MIN;
1110Sstevel@tonic-gate 	} else {
1120Sstevel@tonic-gate 		limit = -_WLONG_MAX;
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	multmin = limit / base;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	val = -DIGIT(c);
1170Sstevel@tonic-gate 	for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
1180Sstevel@tonic-gate 		/* accumulate neg avoids surprises near MAXLONG */
1190Sstevel@tonic-gate 		if (val < multmin)
1200Sstevel@tonic-gate 			goto overflow;
1210Sstevel@tonic-gate 		val *= base;
1220Sstevel@tonic-gate 		if (val < limit + xx)
1230Sstevel@tonic-gate 			goto overflow;
1240Sstevel@tonic-gate 		val -= xx;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	if (ptr != NULL)
1270Sstevel@tonic-gate 		*ptr = (wchar_t *)str;
1280Sstevel@tonic-gate 	return (neg ? val : -val);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate overflow:
1310Sstevel@tonic-gate 	while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
1320Sstevel@tonic-gate 		;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (ptr != NULL)
1350Sstevel@tonic-gate 		*ptr = (wchar_t *)str;
1360Sstevel@tonic-gate 	errno = ERANGE;
1370Sstevel@tonic-gate 	return (neg ? _WLONG_MIN : _WLONG_MAX);
1380Sstevel@tonic-gate }
139