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