]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 1990, 1993 | |
e9ce8d39 A |
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. | |
e9ce8d39 A |
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 | ||
9385eb3d | 30 | #include <sys/cdefs.h> |
1f2f436a | 31 | __FBSDID("$FreeBSD: src/lib/libc/locale/wcstoul.c,v 1.2 2007/01/09 00:28:01 imp Exp $"); |
e9ce8d39 | 32 | |
e9ce8d39 | 33 | #include <ctype.h> |
9385eb3d A |
34 | #include <errno.h> |
35 | #include <limits.h> | |
36 | #include <wchar.h> | |
37 | #include <wctype.h> | |
e9ce8d39 A |
38 | |
39 | /* | |
9385eb3d | 40 | * Convert a wide character string to an unsigned long integer. |
e9ce8d39 | 41 | */ |
9385eb3d A |
42 | unsigned long |
43 | wcstoul(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base) | |
e9ce8d39 | 44 | { |
9385eb3d A |
45 | const wchar_t *s; |
46 | unsigned long acc; | |
47 | wchar_t c; | |
48 | unsigned long cutoff; | |
49 | int neg, any, cutlim; | |
e9ce8d39 A |
50 | |
51 | /* | |
9385eb3d | 52 | * See strtol for comments as to the logic used. |
e9ce8d39 A |
53 | */ |
54 | s = nptr; | |
55 | do { | |
56 | c = *s++; | |
9385eb3d A |
57 | } while (iswspace(c)); |
58 | if (c == L'-') { | |
e9ce8d39 A |
59 | neg = 1; |
60 | c = *s++; | |
5b2abdfb | 61 | } else { |
e9ce8d39 | 62 | neg = 0; |
9385eb3d | 63 | if (c == L'+') |
e9ce8d39 A |
64 | c = *s++; |
65 | } | |
66 | if ((base == 0 || base == 16) && | |
9385eb3d | 67 | c == L'0' && (*s == L'x' || *s == L'X')) { |
e9ce8d39 A |
68 | c = s[1]; |
69 | s += 2; | |
70 | base = 16; | |
71 | } | |
72 | if (base == 0) | |
9385eb3d A |
73 | base = c == L'0' ? 8 : 10; |
74 | acc = any = 0; | |
75 | if (base < 2 || base > 36) | |
76 | goto noconv; | |
77 | ||
78 | cutoff = ULONG_MAX / base; | |
79 | cutlim = ULONG_MAX % base; | |
80 | for ( ; ; c = *s++) { | |
81 | #ifdef notyet | |
82 | if (iswdigit(c)) | |
83 | c = digittoint(c); | |
84 | else | |
85 | #endif | |
86 | if (c >= L'0' && c <= L'9') | |
87 | c -= L'0'; | |
88 | else if (c >= L'A' && c <= L'Z') | |
89 | c -= L'A' - 10; | |
90 | else if (c >= L'a' && c <= L'z') | |
91 | c -= L'a' - 10; | |
e9ce8d39 A |
92 | else |
93 | break; | |
94 | if (c >= base) | |
95 | break; | |
5b2abdfb | 96 | if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) |
e9ce8d39 A |
97 | any = -1; |
98 | else { | |
99 | any = 1; | |
9385eb3d | 100 | acc *= base; |
e9ce8d39 A |
101 | acc += c; |
102 | } | |
103 | } | |
104 | if (any < 0) { | |
9385eb3d | 105 | acc = ULONG_MAX; |
e9ce8d39 | 106 | errno = ERANGE; |
9385eb3d A |
107 | } else if (!any) { |
108 | noconv: | |
109 | errno = EINVAL; | |
e9ce8d39 A |
110 | } else if (neg) |
111 | acc = -acc; | |
9385eb3d A |
112 | if (endptr != NULL) |
113 | *endptr = (wchar_t *)(any ? s - 1 : nptr); | |
e9ce8d39 A |
114 | return (acc); |
115 | } |