1 /*
2 FUNCTION
3 <<strtoull>>, <<strtoull_l>>---string to unsigned long long
4
5 INDEX
6 strtoull
7
8 INDEX
9 strtoull_l
10
11 SYNOPSIS
12 #include <stdlib.h>
13 unsigned long long strtoull(const char *restrict <[s]>,
14 char **restrict <[ptr]>, int <[base]>);
15
16 #include <stdlib.h>
17 unsigned long long strtoull_l(const char *restrict <[s]>,
18 char **restrict <[ptr]>, int <[base]>,
19 locale_t <[locale]>);
20
21 unsigned long long _strtoull_r(void *<[reent]>,
22 const char *restrict <[s]>,
23 char **restrict <[ptr]>, int <[base]>);
24
25 DESCRIPTION
26 The function <<strtoull>> converts the string <<*<[s]>>> to
27 an <<unsigned long long>>. First, it breaks down the string into three parts:
28 leading whitespace, which is ignored; a subject string consisting
29 of the digits meaningful in the radix specified by <[base]>
30 (for example, <<0>> through <<7>> if the value of <[base]> is 8);
31 and a trailing portion consisting of one or more unparseable characters,
32 which always includes the terminating null character. Then, it attempts
33 to convert the subject string into an unsigned long long integer, and returns the
34 result.
35
36 If the value of <[base]> is zero, the subject string is expected to look
37 like a normal C integer constant (save that no optional sign is permitted):
38 a possible <<0x>> indicating hexadecimal radix, and a number.
39 If <[base]> is between 2 and 36, the expected form of the subject is a
40 sequence of digits (which may include letters, depending on the
41 base) representing an integer in the radix specified by <[base]>.
42 The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
43 10 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
44
45 The subject sequence is the longest initial sequence of the input
46 string that has the expected form, starting with the first
47 non-whitespace character. If the string is empty or consists entirely
48 of whitespace, or if the first non-whitespace character is not a
49 permissible digit, the subject string is empty.
50
51 If the subject string is acceptable, and the value of <[base]> is zero,
52 <<strtoull>> attempts to determine the radix from the input string. A
53 string with a leading <<0x>> is treated as a hexadecimal value; a string with
54 a leading <<0>> and no <<x>> is treated as octal; all other strings are
55 treated as decimal. If <[base]> is between 2 and 36, it is used as the
56 conversion radix, as described above. Finally, a pointer to the first
57 character past the converted subject string is stored in <[ptr]>, if
58 <[ptr]> is not <<NULL>>.
59
60 If the subject string is empty (that is, if <<*>><[s]> does not start
61 with a substring in acceptable form), no conversion
62 is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
63 not <<NULL>>).
64
65 <<strtoull_l>> is like <<strtoull>> but performs the conversion based on the
66 locale specified by the locale object locale. If <[locale]> is
67 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
68
69 The alternate function <<_strtoull_r>> is a reentrant version. The
70 extra argument <[reent]> is a pointer to a reentrancy structure.
71
72 RETURNS
73 <<strtoull>>, <<strtoull_l>> return the converted value, if any. If no
74 conversion was made, <<0>> is returned.
75
76 <<strtoull>>, <<strtoull_l>> return <<ULONG_LONG_MAX>> if the magnitude
77 of the converted value is too large, and sets <<errno>> to <<ERANGE>>.
78
79 PORTABILITY
80 <<strtoull>> is ANSI.
81 <<strtoull_l>> is a GNU extension.
82
83 <<strtoull>> requires no supporting OS subroutines.
84 */
85
86 /*
87 * Copyright (c) 1990 Regents of the University of California.
88 * All rights reserved.
89 *
90 * Redistribution and use in source and binary forms, with or without
91 * modification, are permitted provided that the following conditions
92 * are met:
93 * 1. Redistributions of source code must retain the above copyright
94 * notice, this list of conditions and the following disclaimer.
95 * 2. Redistributions in binary form must reproduce the above copyright
96 * notice, this list of conditions and the following disclaimer in the
97 * documentation and/or other materials provided with the distribution.
98 * 3. Neither the name of the University nor the names of its contributors
99 * may be used to endorse or promote products derived from this software
100 * without specific prior written permission.
101 *
102 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
103 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
104 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
105 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
106 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
107 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
108 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
109 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
110 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
111 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
112 * SUCH DAMAGE.
113 */
114
115 #define _GNU_SOURCE
116 #include <_ansi.h>
117 #include <limits.h>
118 #include <ctype.h>
119 #include <errno.h>
120 #include <stdlib.h>
121 #include "../locale/setlocale.h"
122
123 /*
124 * Convert a string to an unsigned long long integer.
125 */
126 static unsigned long long
_strtoull_l(const char * __restrict nptr,char ** __restrict endptr,int base,locale_t loc)127 _strtoull_l (const char *__restrict nptr,
128 char **__restrict endptr, int base, locale_t loc)
129 {
130 register const unsigned char *s = (const unsigned char *)nptr;
131 register unsigned long long acc;
132 register int c;
133 register unsigned long long cutoff;
134 register int neg = 0, any, cutlim;
135
136 /*
137 * See strtol for comments as to the logic used.
138 */
139 do {
140 c = *s++;
141 } while (isspace_l(c, loc));
142 if (c == '-') {
143 neg = 1;
144 c = *s++;
145 } else if (c == '+')
146 c = *s++;
147 if ((base == 0 || base == 16) &&
148 c == '0' && (*s == 'x' || *s == 'X')) {
149 c = s[1];
150 s += 2;
151 base = 16;
152 }
153 if (base == 0)
154 base = c == '0' ? 8 : 10;
155 cutoff = (unsigned long long)ULLONG_MAX / (unsigned long long)base;
156 cutlim = (unsigned long long)ULLONG_MAX % (unsigned long long)base;
157 for (acc = 0, any = 0;; c = *s++) {
158 if (c >= '0' && c <= '9')
159 c -= '0';
160 else if (c >= 'A' && c <= 'Z')
161 c -= 'A' - 10;
162 else if (c >= 'a' && c <= 'z')
163 c -= 'a' - 10;
164 else
165 break;
166 if (c >= base)
167 break;
168 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
169 any = -1;
170 else {
171 any = 1;
172 acc *= base;
173 acc += c;
174 }
175 }
176 if (any < 0) {
177 acc = ULLONG_MAX;
178 _REENT_ERRNO(rptr) = ERANGE;
179 } else if (neg)
180 acc = -acc;
181 if (endptr != 0)
182 *endptr = (char *) (any ? (char *)s - 1 : nptr);
183 return (acc);
184 }
185
186 #ifndef _REENT_ONLY
187
188 unsigned long long
strtoull_l(const char * __restrict s,char ** __restrict ptr,int base,locale_t loc)189 strtoull_l (const char *__restrict s, char **__restrict ptr, int base,
190 locale_t loc)
191 {
192 return _strtoull_l (s, ptr, base, loc);
193 }
194
195 unsigned long long
strtoull(const char * __restrict s,char ** __restrict ptr,int base)196 strtoull (const char *__restrict s,
197 char **__restrict ptr,
198 int base)
199 {
200 return _strtoull_l (s, ptr, base, __get_current_locale ());
201 }
202
203 #endif
204