Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / strtoull.c
blob943de6b83797a15e604802e21412deecb9f50a5b
1 /*
2 FUNCTION
3 <<strtoull>>, <<strtoull_l>>---string to unsigned long long
5 INDEX
6 strtoull
8 INDEX
9 strtoull_l
11 SYNOPSIS
12 #include <stdlib.h>
13 unsigned long long strtoull(const char *restrict <[s]>,
14 char **restrict <[ptr]>, int <[base]>);
16 #include <stdlib.h>
17 unsigned long long strtoull_l(const char *restrict <[s]>,
18 char **restrict <[ptr]>, int <[base]>,
19 locale_t <[locale]>);
21 unsigned long long _strtoull_r(void *<[reent]>,
22 const char *restrict <[s]>,
23 char **restrict <[ptr]>, int <[base]>);
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.
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.
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.
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>>.
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>>).
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.
69 The alternate function <<_strtoull_r>> is a reentrant version. The
70 extra argument <[reent]> is a pointer to a reentrancy structure.
72 RETURNS
73 <<strtoull>>, <<strtoull_l>> return the converted value, if any. If no
74 conversion was made, <<0>> is returned.
76 <<strtoull>>, <<strtoull_l>> return <<ULONG_LONG_MAX>> if the magnitude
77 of the converted value is too large, and sets <<errno>> to <<ERANGE>>.
79 PORTABILITY
80 <<strtoull>> is ANSI.
81 <<strtoull_l>> is a GNU extension.
83 <<strtoull>> requires no supporting OS subroutines.
87 * Copyright (c) 1990 Regents of the University of California.
88 * All rights reserved.
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.
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.
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 <reent.h>
122 #include "../locale/setlocale.h"
125 * Convert a string to an unsigned long long integer.
127 static unsigned long long
128 _strtoull_l (struct _reent *rptr, const char *__restrict nptr,
129 char **__restrict endptr, int base, locale_t loc)
131 register const unsigned char *s = (const unsigned char *)nptr;
132 register unsigned long long acc;
133 register int c;
134 register unsigned long long cutoff;
135 register int neg = 0, any, cutlim;
138 * See strtol for comments as to the logic used.
140 do {
141 c = *s++;
142 } while (isspace_l(c, loc));
143 if (c == '-') {
144 neg = 1;
145 c = *s++;
146 } else if (c == '+')
147 c = *s++;
148 if ((base == 0 || base == 16) &&
149 c == '0' && (*s == 'x' || *s == 'X')) {
150 c = s[1];
151 s += 2;
152 base = 16;
154 if (base == 0)
155 base = c == '0' ? 8 : 10;
156 cutoff = (unsigned long long)ULONG_LONG_MAX / (unsigned long long)base;
157 cutlim = (unsigned long long)ULONG_LONG_MAX % (unsigned long long)base;
158 for (acc = 0, any = 0;; c = *s++) {
159 if (c >= '0' && c <= '9')
160 c -= '0';
161 else if (c >= 'A' && c <= 'Z')
162 c -= 'A' - 10;
163 else if (c >= 'a' && c <= 'z')
164 c -= 'a' - 10;
165 else
166 break;
167 if (c >= base)
168 break;
169 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
170 any = -1;
171 else {
172 any = 1;
173 acc *= base;
174 acc += c;
177 if (any < 0) {
178 acc = ULONG_LONG_MAX;
179 _REENT_ERRNO(rptr) = ERANGE;
180 } else if (neg)
181 acc = -acc;
182 if (endptr != 0)
183 *endptr = (char *) (any ? (char *)s - 1 : nptr);
184 return (acc);
187 unsigned long long
188 _strtoull_r (struct _reent *rptr,
189 const char *__restrict nptr,
190 char **__restrict endptr,
191 int base)
193 return _strtoull_l (rptr, nptr, endptr, base, __get_current_locale ());
196 #ifndef _REENT_ONLY
198 unsigned long long
199 strtoull_l (const char *__restrict s, char **__restrict ptr, int base,
200 locale_t loc)
202 return _strtoull_l (_REENT, s, ptr, base, loc);
205 unsigned long long
206 strtoull (const char *__restrict s,
207 char **__restrict ptr,
208 int base)
210 return _strtoull_l (_REENT, s, ptr, base, __get_current_locale ());
213 #endif