xref: /titanic_41/usr/src/lib/libc/port/i18n/wcstol.c (revision 40cb5e5daa7b80bb70fcf8dadfb20f9281566331)
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 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*	Copyright (c) 1986 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 
34 #ifndef	_WCS_LONGLONG
35 #pragma weak wcstol = _wcstol
36 #endif
37 
38 #include "synonyms.h"
39 #include <limits.h>
40 #include <errno.h>
41 #include <wchar.h>
42 #define	DIGIT(x)	(iswdigit(x) ? (x) - L'0' : \
43 			iswlower(x) ? (x) + 10 - L'a' : (x) + 10 - L'A')
44 #define	MBASE	(L'z' - L'a' + 1 + 10)
45 
46 #ifdef	_WCS_LONGLONG
47 #define	_WLONG_T	long long
48 #define	_WLONG_MAX	LLONG_MAX
49 #define	_WLONG_MIN	LLONG_MIN
50 #else  /* _WCS_LONGLONG */
51 #define	_WLONG_T	long
52 #define	_WLONG_MAX	LONG_MAX
53 #define	_WLONG_MIN	LONG_MIN
54 #endif /* _WCS_LONGLONG */
55 
56 #ifdef	_WCS_LONGLONG
57 long long
58 wcstoll(const wchar_t *_RESTRICT_KYWD str, wchar_t **_RESTRICT_KYWD ptr,
59     int base)
60 #else  /* _WCS_LONGLONG */
61 long
62 _wcstol(const wchar_t *str, wchar_t **ptr, int base)
63 #endif /* _WCS_LONGLONG */
64 {
65 	_WLONG_T	val;
66 	wchar_t	c;
67 	int	xx, neg = 0;
68 	_WLONG_T	multmin, limit;
69 
70 	if (ptr != NULL)
71 		*ptr = (wchar_t *)str; /* in case no number is formed */
72 	if (base < 0 || base > MBASE) {
73 		errno = EINVAL;
74 		return (0); /* base is invalid -- should be a fatal error */
75 	}
76 
77 	if (!iswalnum(c = *str)) {
78 		while (iswspace(c)) {
79 			c = *++str;
80 		}
81 		switch (c) {
82 		case L'-':
83 			neg++;
84 			/*FALLTHRU*/
85 		case L'+':
86 			c = *++str;
87 		}
88 	}
89 	if (base == 0) {
90 		if (c != L'0')
91 			base = 10;
92 		else if (str[1] == L'x' || str[1] == L'X')
93 			base = 16;
94 		else
95 			base = 8;
96 	}
97 	/*
98 	 * for any base > 10, the digits incrementally following
99 	 *	9 are assumed to be "abc...z" or "ABC...Z"
100 	 */
101 	if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
102 		errno = EINVAL;
103 		return (0); /* no number formed */
104 	}
105 
106 	if (base == 16 && c == L'0' && iswxdigit(str[2]) &&
107 	    (str[1] == L'x' || str[1] == L'X')) {
108 		c = *(str += 2); /* skip over leading "0x" or "0X" */
109 	}
110 
111 	if (neg) {
112 		limit = _WLONG_MIN;
113 	} else {
114 		limit = -_WLONG_MAX;
115 	}
116 	multmin = limit / base;
117 
118 	val = -DIGIT(c);
119 	for (; iswalnum(c = *++str) && (xx = DIGIT(c)) < base; ) {
120 		/* accumulate neg avoids surprises near MAXLONG */
121 		if (val < multmin)
122 			goto overflow;
123 		val *= base;
124 		if (val < limit + xx)
125 			goto overflow;
126 		val -= xx;
127 	}
128 	if (ptr != NULL)
129 		*ptr = (wchar_t *)str;
130 	return (neg ? val : -val);
131 
132 overflow:
133 	while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
134 		;
135 
136 	if (ptr != NULL)
137 		*ptr = (wchar_t *)str;
138 	errno = ERANGE;
139 	return (neg ? _WLONG_MIN : _WLONG_MAX);
140 }
141