xref: /haiku/src/system/libroot/posix/stdlib/strtoul.c (revision bc328a435bb135d767798374d3653072255f0c48)
1*bc328a43SAugustin Cavalier /*-
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 an unsigned 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 unsigned long
strtoul(const char * __restrict nptr,char ** __restrict endptr,int base)515af32e75SAxel Dörfler strtoul(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 	 * See strtol for comments as to the logic used.
615af32e75SAxel Dörfler 	 */
625af32e75SAxel Dörfler 	s = nptr;
635af32e75SAxel Dörfler 	do {
645af32e75SAxel Dörfler 		c = *s++;
655af32e75SAxel Dörfler 	} while (isspace((unsigned char)c));
665af32e75SAxel Dörfler 	if (c == '-') {
675af32e75SAxel Dörfler 		neg = 1;
685af32e75SAxel Dörfler 		c = *s++;
695af32e75SAxel Dörfler 	} else {
705af32e75SAxel Dörfler 		neg = 0;
715af32e75SAxel Dörfler 		if (c == '+')
725af32e75SAxel Dörfler 			c = *s++;
735af32e75SAxel Dörfler 	}
745af32e75SAxel Dörfler 	if ((base == 0 || base == 16) &&
75*bc328a43SAugustin Cavalier 		c == '0' && (*s == 'x' || *s == 'X') &&
76*bc328a43SAugustin Cavalier 		((s[1] >= '0' && s[1] <= '9') ||
77*bc328a43SAugustin Cavalier 		(s[1] >= 'A' && s[1] <= 'F') ||
78*bc328a43SAugustin Cavalier 		(s[1] >= 'a' && s[1] <= 'f'))) {
795af32e75SAxel Dörfler 		c = s[1];
805af32e75SAxel Dörfler 		s += 2;
815af32e75SAxel Dörfler 		base = 16;
825af32e75SAxel Dörfler 	}
83*bc328a43SAugustin Cavalier 	if ((base == 0 || base == 2) &&
84*bc328a43SAugustin Cavalier 		c == '0' && (*s == 'b' || *s == 'B') &&
85*bc328a43SAugustin Cavalier 		(s[1] >= '0' && s[1] <= '1')) {
86*bc328a43SAugustin Cavalier 		c = s[1];
87*bc328a43SAugustin Cavalier 		s += 2;
88*bc328a43SAugustin Cavalier 		base = 2;
89*bc328a43SAugustin Cavalier 	}
905af32e75SAxel Dörfler 	if (base == 0)
915af32e75SAxel Dörfler 		base = c == '0' ? 8 : 10;
925af32e75SAxel Dörfler 	acc = any = 0;
935af32e75SAxel Dörfler 	if (base < 2 || base > 36)
945af32e75SAxel Dörfler 		goto noconv;
955af32e75SAxel Dörfler 
965af32e75SAxel Dörfler 	cutoff = ULONG_MAX / base;
975af32e75SAxel Dörfler 	cutlim = ULONG_MAX % base;
985af32e75SAxel Dörfler 	for ( ; ; c = *s++) {
995af32e75SAxel Dörfler 		if (c >= '0' && c <= '9')
1005af32e75SAxel Dörfler 			c -= '0';
1015af32e75SAxel Dörfler 		else if (c >= 'A' && c <= 'Z')
1025af32e75SAxel Dörfler 			c -= 'A' - 10;
1035af32e75SAxel Dörfler 		else if (c >= 'a' && c <= 'z')
1045af32e75SAxel Dörfler 			c -= 'a' - 10;
1055af32e75SAxel Dörfler 		else
1065af32e75SAxel Dörfler 			break;
1075af32e75SAxel Dörfler 		if (c >= base)
1085af32e75SAxel Dörfler 			break;
1095af32e75SAxel Dörfler 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1105af32e75SAxel Dörfler 			any = -1;
1115af32e75SAxel Dörfler 		else {
1125af32e75SAxel Dörfler 			any = 1;
1135af32e75SAxel Dörfler 			acc *= base;
1145af32e75SAxel Dörfler 			acc += c;
1155af32e75SAxel Dörfler 		}
1165af32e75SAxel Dörfler 	}
1175af32e75SAxel Dörfler 	if (any < 0) {
1185af32e75SAxel Dörfler 		acc = ULONG_MAX;
119*bc328a43SAugustin Cavalier 		errno = ERANGE;
1205af32e75SAxel Dörfler 	} else if (!any) {
1215af32e75SAxel Dörfler noconv:
122*bc328a43SAugustin Cavalier 		errno = EINVAL;
1235af32e75SAxel Dörfler 	} else if (neg)
1245af32e75SAxel Dörfler 		acc = -acc;
1255af32e75SAxel Dörfler 	if (endptr != NULL)
1265af32e75SAxel Dörfler 		*endptr = (char *)(any ? s - 1 : nptr);
1275af32e75SAxel Dörfler 	return (acc);
1285af32e75SAxel Dörfler }
1295af32e75SAxel Dörfler 
1305af32e75SAxel Dörfler 
131*bc328a43SAugustin Cavalier #ifdef __HAIKU__
1325af32e75SAxel Dörfler unsigned long __strtoul_internal(const char *number, char **_end, int base, int group);
1335af32e75SAxel Dörfler 
1345af32e75SAxel Dörfler unsigned long
__strtoul_internal(const char * number,char ** _end,int base,int group)1355af32e75SAxel Dörfler __strtoul_internal(const char *number, char **_end, int base, int group)
1365af32e75SAxel Dörfler {
1375af32e75SAxel Dörfler 	// ToDo: group is currently not supported!
1385af32e75SAxel Dörfler 	(void)group;
1395af32e75SAxel Dörfler 
1405af32e75SAxel Dörfler 	return strtoul(number, _end, base);
1415af32e75SAxel Dörfler }
142*bc328a43SAugustin Cavalier #endif
143