Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / strtol.c
blob09d4333ed05f497ec00b7192204644349441e58d
1 /*
2 FUNCTION
3 <<strtol>>, <<strtol_l>>---string to long
5 INDEX
6 strtol
8 INDEX
9 strtol_l
11 INDEX
12 _strtol_r
14 SYNOPSIS
15 #include <stdlib.h>
16 long strtol(const char *restrict <[s]>, char **restrict <[ptr]>,
17 int <[base]>);
19 #include <stdlib.h>
20 long strtol_l(const char *restrict <[s]>, char **restrict <[ptr]>,
21 int <[base]>, locale_t <[locale]>);
23 long _strtol_r(void *<[reent]>, const char *restrict <[s]>,
24 char **restrict <[ptr]>,int <[base]>);
26 DESCRIPTION
27 The function <<strtol>> converts the string <<*<[s]>>> to
28 a <<long>>. First, it breaks down the string into three parts:
29 leading whitespace, which is ignored; a subject string consisting
30 of characters resembling an integer in the radix specified by <[base]>;
31 and a trailing portion consisting of zero or more unparseable characters,
32 and always including the terminating null character. Then, it attempts
33 to convert the subject string into a <<long>> and returns the
34 result.
36 If the value of <[base]> is 0, the subject string is expected to look
37 like a normal C integer constant: an optional sign, a possible `<<0x>>'
38 indicating a hexadecimal base, and a number. If <[base]> is between
39 2 and 36, the expected form of the subject is a sequence of letters
40 and digits representing an integer in the radix specified by <[base]>,
41 with an optional plus or minus sign. The letters <<a>>--<<z>> (or,
42 equivalently, <<A>>--<<Z>>) are used to signify values from 10 to 35;
43 only letters whose ascribed values are less than <[base]> are
44 permitted. If <[base]> is 16, a leading <<0x>> is permitted.
46 The subject sequence is the longest initial sequence of the input
47 string that has the expected form, starting with the first
48 non-whitespace character. If the string is empty or consists entirely
49 of whitespace, or if the first non-whitespace character is not a
50 permissible letter or digit, the subject string is empty.
52 If the subject string is acceptable, and the value of <[base]> is zero,
53 <<strtol>> attempts to determine the radix from the input string. A
54 string with a leading <<0x>> is treated as a hexadecimal value; a string with
55 a leading 0 and no <<x>> is treated as octal; all other strings are
56 treated as decimal. If <[base]> is between 2 and 36, it is used as the
57 conversion radix, as described above. If the subject string begins with
58 a minus sign, the value is negated. Finally, a pointer to the first
59 character past the converted subject string is stored in <[ptr]>, if
60 <[ptr]> is not <<NULL>>.
62 If the subject string is empty (or not in acceptable form), no conversion
63 is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
64 not <<NULL>>).
66 <<strtol_l>> is like <<strtol>> but performs the conversion based on the
67 locale specified by the locale object locale. If <[locale]> is
68 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
70 The alternate function <<_strtol_r>> is a reentrant version. The
71 extra argument <[reent]> is a pointer to a reentrancy structure.
73 RETURNS
74 <<strtol>>, <<strtol_l>> return the converted value, if any. If no
75 conversion was made, 0 is returned.
77 <<strtol>>, <<strtol_l>> return <<LONG_MAX>> or <<LONG_MIN>> if the
78 magnitude of the converted value is too large, and sets <<errno>>
79 to <<ERANGE>>.
81 PORTABILITY
82 <<strtol>> is ANSI.
83 <<strtol_l>> is a GNU extension.
85 No supporting OS subroutines are required.
88 /*-
89 * Copyright (c) 1990 The Regents of the University of California.
90 * All rights reserved.
92 * Redistribution and use in source and binary forms, with or without
93 * modification, are permitted provided that the following conditions
94 * are met:
95 * 1. Redistributions of source code must retain the above copyright
96 * notice, this list of conditions and the following disclaimer.
97 * 2. Redistributions in binary form must reproduce the above copyright
98 * notice, this list of conditions and the following disclaimer in the
99 * documentation and/or other materials provided with the distribution.
100 * 3. Neither the name of the University nor the names of its contributors
101 * may be used to endorse or promote products derived from this software
102 * without specific prior written permission.
104 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
105 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
107 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
108 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
109 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
110 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
111 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
112 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
113 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
114 * SUCH DAMAGE.
117 #define _GNU_SOURCE
118 #include <_ansi.h>
119 #include <limits.h>
120 #include <ctype.h>
121 #include <errno.h>
122 #include <stdlib.h>
123 #include <reent.h>
124 #include "../locale/setlocale.h"
127 * Convert a string to a long integer.
129 static long
130 _strtol_l (struct _reent *rptr, const char *__restrict nptr,
131 char **__restrict endptr, int base, locale_t loc)
133 register const unsigned char *s = (const unsigned char *)nptr;
134 register unsigned long acc;
135 register int c;
136 register unsigned long cutoff;
137 register int neg = 0, any, cutlim;
139 if (base < 0 || base == 1 || base > 36) {
140 errno = EINVAL;
141 return 0;
145 * Skip white space and pick up leading +/- sign if any.
146 * If base is 0, allow 0x for hex and 0 for octal, else
147 * assume decimal; if base is already 16, allow 0x.
149 do {
150 c = *s++;
151 } while (isspace_l(c, loc));
152 if (c == '-') {
153 neg = 1;
154 c = *s++;
155 } else if (c == '+')
156 c = *s++;
157 if ((base == 0 || base == 16) &&
158 c == '0' && (*s == 'x' || *s == 'X')) {
159 c = s[1];
160 s += 2;
161 base = 16;
163 if (base == 0)
164 base = c == '0' ? 8 : 10;
167 * Compute the cutoff value between legal numbers and illegal
168 * numbers. That is the largest legal value, divided by the
169 * base. An input number that is greater than this value, if
170 * followed by a legal input character, is too big. One that
171 * is equal to this value may be valid or not; the limit
172 * between valid and invalid numbers is then based on the last
173 * digit. For instance, if the range for longs is
174 * [-2147483648..2147483647] and the input base is 10,
175 * cutoff will be set to 214748364 and cutlim to either
176 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
177 * a value > 214748364, or equal but the next digit is > 7 (or 8),
178 * the number is too big, and we will return a range error.
180 * Set any if any `digits' consumed; make it negative to indicate
181 * overflow.
183 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
184 cutlim = cutoff % (unsigned long)base;
185 cutoff /= (unsigned long)base;
186 for (acc = 0, any = 0;; c = *s++) {
187 if (c >= '0' && c <= '9')
188 c -= '0';
189 else if (c >= 'A' && c <= 'Z')
190 c -= 'A' - 10;
191 else if (c >= 'a' && c <= 'z')
192 c -= 'a' - 10;
193 else
194 break;
195 if (c >= base)
196 break;
197 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
198 any = -1;
199 } else {
200 any = 1;
201 acc *= base;
202 acc += c;
205 if (any < 0) {
206 acc = neg ? LONG_MIN : LONG_MAX;
207 _REENT_ERRNO(rptr) = ERANGE;
208 } else if (neg)
209 acc = -acc;
210 if (endptr != 0)
211 *endptr = (char *) (any ? (char *)s - 1 : nptr);
212 return (acc);
215 long
216 _strtol_r (struct _reent *rptr,
217 const char *__restrict nptr,
218 char **__restrict endptr,
219 int base)
221 return _strtol_l (rptr, nptr, endptr, base, __get_current_locale ());
224 #ifndef _REENT_ONLY
226 long
227 strtol_l (const char *__restrict s, char **__restrict ptr, int base,
228 locale_t loc)
230 return _strtol_l (_REENT, s, ptr, base, loc);
233 long
234 strtol (const char *__restrict s,
235 char **__restrict ptr,
236 int base)
238 return _strtol_l (_REENT, s, ptr, base, __get_current_locale ());
241 #endif