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