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