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