6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libc / port / i18n / wcstol.c
blob162feeddb2b8f5bb58083bb9d4c23b6abcc57d2c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1986 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #ifndef _WCS_LONGLONG
33 #pragma weak _wcstol = wcstol
34 #endif
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)
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 */
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 */
63 _WLONG_T val;
64 wchar_t c;
65 int xx, neg = 0;
66 _WLONG_T multmin, limit;
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 */
75 if (!iswalnum(c = *str)) {
76 while (iswspace(c)) {
77 c = *++str;
79 switch (c) {
80 case L'-':
81 neg++;
82 /*FALLTHRU*/
83 case L'+':
84 c = *++str;
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;
96 * for any base > 10, the digits incrementally following
97 * 9 are assumed to be "abc...z" or "ABC...Z"
99 if (!iswalnum(c) || (xx = DIGIT(c)) >= base) {
100 errno = EINVAL;
101 return (0); /* no number formed */
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" */
109 if (neg) {
110 limit = _WLONG_MIN;
111 } else {
112 limit = -_WLONG_MAX;
114 multmin = limit / base;
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;
126 if (ptr != NULL)
127 *ptr = (wchar_t *)str;
128 return (neg ? val : -val);
130 overflow:
131 while (iswalnum(c = *++str) && (xx = DIGIT(c)) < base)
134 if (ptr != NULL)
135 *ptr = (wchar_t *)str;
136 errno = ERANGE;
137 return (neg ? _WLONG_MIN : _WLONG_MAX);