]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc/locale/wcstol.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc / locale / wcstol.c
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <ctype.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <wctype.h>
38
39 /*
40  * Convert a string to a long integer.
41  */
42 long
43 wcstol(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base)
44 {
45         const wchar_t *s;
46         unsigned long acc;
47         wchar_t c;
48         unsigned long cutoff;
49         int neg, any, cutlim;
50
51         /*
52          * See strtol for comments as to the logic used.
53          */
54         s = nptr;
55         do {
56                 c = *s++;
57         } while (iswspace(c));
58         if (c == '-') {
59                 neg = 1;
60                 c = *s++;
61         } else {
62                 neg = 0;
63                 if (c == L'+')
64                         c = *s++;
65         }
66         if ((base == 0 || base == 16) &&
67             c == L'0' && (*s == L'x' || *s == L'X')) {
68                 c = s[1];
69                 s += 2;
70                 base = 16;
71         }
72         if (base == 0)
73                 base = c == L'0' ? 8 : 10;
74         acc = any = 0;
75         if (base < 2 || base > 36)
76                 goto noconv;
77
78         cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX
79             : LONG_MAX;
80         cutlim = cutoff % base;
81         cutoff /= base;
82         for ( ; ; c = *s++) {
83 #ifdef notyet
84                 if (iswdigit(c))
85                         c = digittoint(c);
86                 else
87 #endif
88                 if (c >= L'0' && c <= L'9')
89                         c -= L'0';
90                 else if (c >= L'A' && c <= L'Z')
91                         c -= L'A' - 10;
92                 else if (c >= L'a' && c <= L'z')
93                         c -= L'a' - 10;
94                 else
95                         break;
96                 if (c >= base)
97                         break;
98                 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
99                         any = -1;
100                 else {
101                         any = 1;
102                         acc *= base;
103                         acc += c;
104                 }
105         }
106         if (any < 0) {
107                 acc = neg ? LONG_MIN : LONG_MAX;
108                 errno = ERANGE;
109         } else if (!any) {
110 noconv:
111                 errno = EINVAL;
112         } else if (neg)
113                 acc = -acc;
114         if (endptr != NULL)
115                 *endptr = (wchar_t *)(any ? s - 1 : nptr);
116         return (acc);
117 }