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