xref: /haiku/src/system/libroot/posix/stdlib/strtol.c (revision bc328a435bb135d767798374d3653072255f0c48)
15af32e75SAxel Dörfler /*-
2*bc328a43SAugustin Cavalier  * SPDX-License-Identifier: BSD-3-Clause
3*bc328a43SAugustin Cavalier  *
45af32e75SAxel Dörfler  * Copyright (c) 1990, 1993
55af32e75SAxel Dörfler  *	The Regents of the University of California.  All rights reserved.
65af32e75SAxel Dörfler  *
7*bc328a43SAugustin Cavalier  * Copyright (c) 2011 The FreeBSD Foundation
8*bc328a43SAugustin Cavalier  *
9*bc328a43SAugustin Cavalier  * Portions of this software were developed by David Chisnall
10*bc328a43SAugustin Cavalier  * under sponsorship from the FreeBSD Foundation.
11*bc328a43SAugustin Cavalier  *
125af32e75SAxel Dörfler  * Redistribution and use in source and binary forms, with or without
135af32e75SAxel Dörfler  * modification, are permitted provided that the following conditions
145af32e75SAxel Dörfler  * are met:
155af32e75SAxel Dörfler  * 1. Redistributions of source code must retain the above copyright
165af32e75SAxel Dörfler  *    notice, this list of conditions and the following disclaimer.
175af32e75SAxel Dörfler  * 2. Redistributions in binary form must reproduce the above copyright
185af32e75SAxel Dörfler  *    notice, this list of conditions and the following disclaimer in the
195af32e75SAxel Dörfler  *    documentation and/or other materials provided with the distribution.
20*bc328a43SAugustin Cavalier  * 3. Neither the name of the University nor the names of its contributors
215af32e75SAxel Dörfler  *    may be used to endorse or promote products derived from this software
225af32e75SAxel Dörfler  *    without specific prior written permission.
235af32e75SAxel Dörfler  *
245af32e75SAxel Dörfler  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
255af32e75SAxel Dörfler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
265af32e75SAxel Dörfler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
275af32e75SAxel Dörfler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
285af32e75SAxel Dörfler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
295af32e75SAxel Dörfler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
305af32e75SAxel Dörfler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
315af32e75SAxel Dörfler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
325af32e75SAxel Dörfler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
335af32e75SAxel Dörfler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
345af32e75SAxel Dörfler  * SUCH DAMAGE.
355af32e75SAxel Dörfler  */
365af32e75SAxel Dörfler 
375af32e75SAxel Dörfler 
385af32e75SAxel Dörfler #include <limits.h>
395af32e75SAxel Dörfler #include <ctype.h>
405af32e75SAxel Dörfler #include <errno.h>
415af32e75SAxel Dörfler #include <stdlib.h>
425af32e75SAxel Dörfler 
435af32e75SAxel Dörfler /*
445af32e75SAxel Dörfler  * Convert a string to a long integer.
455af32e75SAxel Dörfler  *
465af32e75SAxel Dörfler  * Assumes that the upper and lower case
475af32e75SAxel Dörfler  * alphabets and digits are each contiguous.
485af32e75SAxel Dörfler  */
495af32e75SAxel Dörfler 
505af32e75SAxel Dörfler long
strtol(const char * __restrict nptr,char ** __restrict endptr,int base)515af32e75SAxel Dörfler strtol(const char * __restrict nptr, char ** __restrict endptr, int base)
525af32e75SAxel Dörfler {
535af32e75SAxel Dörfler 	const char *s;
545af32e75SAxel Dörfler 	unsigned long acc;
555af32e75SAxel Dörfler 	char c;
565af32e75SAxel Dörfler 	unsigned long cutoff;
575af32e75SAxel Dörfler 	int neg, any, cutlim;
585af32e75SAxel Dörfler 
595af32e75SAxel Dörfler 	/*
605af32e75SAxel Dörfler 	 * Skip white space and pick up leading +/- sign if any.
615af32e75SAxel Dörfler 	 * If base is 0, allow 0x for hex and 0 for octal, else
625af32e75SAxel Dörfler 	 * assume decimal; if base is already 16, allow 0x.
635af32e75SAxel Dörfler 	 */
645af32e75SAxel Dörfler 	s = nptr;
655af32e75SAxel Dörfler 	do {
665af32e75SAxel Dörfler 		c = *s++;
675af32e75SAxel Dörfler 	} while (isspace((unsigned char)c));
685af32e75SAxel Dörfler 	if (c == '-') {
695af32e75SAxel Dörfler 		neg = 1;
705af32e75SAxel Dörfler 		c = *s++;
715af32e75SAxel Dörfler 	} else {
725af32e75SAxel Dörfler 		neg = 0;
735af32e75SAxel Dörfler 		if (c == '+')
745af32e75SAxel Dörfler 			c = *s++;
755af32e75SAxel Dörfler 	}
765af32e75SAxel Dörfler 	if ((base == 0 || base == 16) &&
77*bc328a43SAugustin Cavalier 		c == '0' && (*s == 'x' || *s == 'X') &&
78*bc328a43SAugustin Cavalier 		((s[1] >= '0' && s[1] <= '9') ||
79*bc328a43SAugustin Cavalier 		(s[1] >= 'A' && s[1] <= 'F') ||
80*bc328a43SAugustin Cavalier 		(s[1] >= 'a' && s[1] <= 'f'))) {
815af32e75SAxel Dörfler 		c = s[1];
825af32e75SAxel Dörfler 		s += 2;
835af32e75SAxel Dörfler 		base = 16;
845af32e75SAxel Dörfler 	}
85*bc328a43SAugustin Cavalier 	if ((base == 0 || base == 2) &&
86*bc328a43SAugustin Cavalier 		c == '0' && (*s == 'b' || *s == 'B') &&
87*bc328a43SAugustin Cavalier 		(s[1] >= '0' && s[1] <= '1')) {
88*bc328a43SAugustin Cavalier 		c = s[1];
89*bc328a43SAugustin Cavalier 		s += 2;
90*bc328a43SAugustin Cavalier 		base = 2;
91*bc328a43SAugustin Cavalier 	}
925af32e75SAxel Dörfler 	if (base == 0)
935af32e75SAxel Dörfler 		base = c == '0' ? 8 : 10;
945af32e75SAxel Dörfler 	acc = any = 0;
955af32e75SAxel Dörfler 	if (base < 2 || base > 36)
965af32e75SAxel Dörfler 		goto noconv;
975af32e75SAxel Dörfler 
985af32e75SAxel Dörfler 	/*
995af32e75SAxel Dörfler 	 * Compute the cutoff value between legal numbers and illegal
1005af32e75SAxel Dörfler 	 * numbers.  That is the largest legal value, divided by the
1015af32e75SAxel Dörfler 	 * base.  An input number that is greater than this value, if
1025af32e75SAxel Dörfler 	 * followed by a legal input character, is too big.  One that
1035af32e75SAxel Dörfler 	 * is equal to this value may be valid or not; the limit
1045af32e75SAxel Dörfler 	 * between valid and invalid numbers is then based on the last
1055af32e75SAxel Dörfler 	 * digit.  For instance, if the range for longs is
1065af32e75SAxel Dörfler 	 * [-2147483648..2147483647] and the input base is 10,
1075af32e75SAxel Dörfler 	 * cutoff will be set to 214748364 and cutlim to either
1085af32e75SAxel Dörfler 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
1095af32e75SAxel Dörfler 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
1105af32e75SAxel Dörfler 	 * the number is too big, and we will return a range error.
1115af32e75SAxel Dörfler 	 *
1125af32e75SAxel Dörfler 	 * Set 'any' if any `digits' consumed; make it negative to indicate
1135af32e75SAxel Dörfler 	 * overflow.
1145af32e75SAxel Dörfler 	 */
1155af32e75SAxel Dörfler 	cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX
1165af32e75SAxel Dörfler 		: LONG_MAX;
1175af32e75SAxel Dörfler 	cutlim = cutoff % base;
1185af32e75SAxel Dörfler 	cutoff /= base;
1195af32e75SAxel Dörfler 	for ( ; ; c = *s++) {
1205af32e75SAxel Dörfler 		if (c >= '0' && c <= '9')
1215af32e75SAxel Dörfler 			c -= '0';
1225af32e75SAxel Dörfler 		else if (c >= 'A' && c <= 'Z')
1235af32e75SAxel Dörfler 			c -= 'A' - 10;
1245af32e75SAxel Dörfler 		else if (c >= 'a' && c <= 'z')
1255af32e75SAxel Dörfler 			c -= 'a' - 10;
1265af32e75SAxel Dörfler 		else
1275af32e75SAxel Dörfler 			break;
1285af32e75SAxel Dörfler 		if (c >= base)
1295af32e75SAxel Dörfler 			break;
1305af32e75SAxel Dörfler 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1315af32e75SAxel Dörfler 			any = -1;
1325af32e75SAxel Dörfler 		else {
1335af32e75SAxel Dörfler 			any = 1;
1345af32e75SAxel Dörfler 			acc *= base;
1355af32e75SAxel Dörfler 			acc += c;
1365af32e75SAxel Dörfler 		}
1375af32e75SAxel Dörfler 	}
1385af32e75SAxel Dörfler 	if (any < 0) {
1395af32e75SAxel Dörfler 		acc = neg ? LONG_MIN : LONG_MAX;
140*bc328a43SAugustin Cavalier 		errno = ERANGE;
1415af32e75SAxel Dörfler 	} else if (!any) {
1425af32e75SAxel Dörfler noconv:
143*bc328a43SAugustin Cavalier 		errno = EINVAL;
1445af32e75SAxel Dörfler 	} else if (neg)
1455af32e75SAxel Dörfler 		acc = -acc;
1465af32e75SAxel Dörfler 	if (endptr != NULL)
1475af32e75SAxel Dörfler 		*endptr = (char *)(any ? s - 1 : nptr);
1485af32e75SAxel Dörfler 	return (acc);
1495af32e75SAxel Dörfler }
1505af32e75SAxel Dörfler 
1515af32e75SAxel Dörfler 
152*bc328a43SAugustin Cavalier #ifdef __HAIKU__
1535af32e75SAxel Dörfler long __strtol_internal(const char *number, char **_end, int base, int group);
1545af32e75SAxel Dörfler 
1555af32e75SAxel Dörfler long
__strtol_internal(const char * number,char ** _end,int base,int group)1565af32e75SAxel Dörfler __strtol_internal(const char *number, char **_end, int base, int group)
1575af32e75SAxel Dörfler {
1585af32e75SAxel Dörfler 	// ToDo: group is currently not supported!
1595af32e75SAxel Dörfler 	(void)group;
1605af32e75SAxel Dörfler 
1615af32e75SAxel Dörfler 	return strtol(number, _end, base);
1625af32e75SAxel Dörfler }
163*bc328a43SAugustin Cavalier #endif
164