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