11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
18251672Semaste * 3. Neither the name of the University nor the names of its contributors
191573Srgrimes *    may be used to endorse or promote products derived from this software
201573Srgrimes *    without specific prior written permission.
211573Srgrimes *
221573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321573Srgrimes * SUCH DAMAGE.
331573Srgrimes */
341573Srgrimes
351573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
361573Srgrimesstatic char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
371573Srgrimes#endif /* LIBC_SCCS and not lint */
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
401573Srgrimes
411573Srgrimes#include <limits.h>
421573Srgrimes#include <ctype.h>
431573Srgrimes#include <errno.h>
441573Srgrimes#include <stdlib.h>
45227753Stheraven#include "xlocale_private.h"
461573Srgrimes
471573Srgrimes
481573Srgrimes/*
491573Srgrimes * Convert a string to a long integer.
501573Srgrimes *
5182975Sache * Assumes that the upper and lower case
521573Srgrimes * alphabets and digits are each contiguous.
531573Srgrimes */
541573Srgrimeslong
55227753Stheravenstrtol_l(const char * __restrict nptr, char ** __restrict endptr, int base,
56227753Stheraven		locale_t locale)
571573Srgrimes{
5887016Sache	const char *s;
5987016Sache	unsigned long acc;
6087494Sache	char c;
6187016Sache	unsigned long cutoff;
6287494Sache	int neg, any, cutlim;
63227753Stheraven	FIX_LOCALE(locale);
641573Srgrimes
651573Srgrimes	/*
661573Srgrimes	 * Skip white space and pick up leading +/- sign if any.
671573Srgrimes	 * If base is 0, allow 0x for hex and 0 for octal, else
681573Srgrimes	 * assume decimal; if base is already 16, allow 0x.
691573Srgrimes	 */
7082975Sache	s = nptr;
711573Srgrimes	do {
721573Srgrimes		c = *s++;
73227753Stheraven	} while (isspace_l((unsigned char)c, locale));
741573Srgrimes	if (c == '-') {
751573Srgrimes		neg = 1;
761573Srgrimes		c = *s++;
7782975Sache	} else {
7882975Sache		neg = 0;
7982975Sache		if (c == '+')
8082975Sache			c = *s++;
8182975Sache	}
821573Srgrimes	if ((base == 0 || base == 16) &&
83140536Sache	    c == '0' && (*s == 'x' || *s == 'X') &&
84140577Sache	    ((s[1] >= '0' && s[1] <= '9') ||
85140577Sache	    (s[1] >= 'A' && s[1] <= 'F') ||
86140577Sache	    (s[1] >= 'a' && s[1] <= 'f'))) {
871573Srgrimes		c = s[1];
881573Srgrimes		s += 2;
891573Srgrimes		base = 16;
901573Srgrimes	}
911573Srgrimes	if (base == 0)
921573Srgrimes		base = c == '0' ? 8 : 10;
9382982Sache	acc = any = 0;
9487023Sfenner	if (base < 2 || base > 36)
9582975Sache		goto noconv;
961573Srgrimes
971573Srgrimes	/*
981573Srgrimes	 * Compute the cutoff value between legal numbers and illegal
991573Srgrimes	 * numbers.  That is the largest legal value, divided by the
1001573Srgrimes	 * base.  An input number that is greater than this value, if
1011573Srgrimes	 * followed by a legal input character, is too big.  One that
1021573Srgrimes	 * is equal to this value may be valid or not; the limit
1031573Srgrimes	 * between valid and invalid numbers is then based on the last
1041573Srgrimes	 * digit.  For instance, if the range for longs is
1051573Srgrimes	 * [-2147483648..2147483647] and the input base is 10,
1061573Srgrimes	 * cutoff will be set to 214748364 and cutlim to either
1071573Srgrimes	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
1081573Srgrimes	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
1091573Srgrimes	 * the number is too big, and we will return a range error.
1101573Srgrimes	 *
11182975Sache	 * Set 'any' if any `digits' consumed; make it negative to indicate
1121573Srgrimes	 * overflow.
1131573Srgrimes	 */
11487016Sache	cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX
11582975Sache	    : LONG_MAX;
11682975Sache	cutlim = cutoff % base;
11782975Sache	cutoff /= base;
11882982Sache	for ( ; ; c = *s++) {
11987494Sache		if (c >= '0' && c <= '9')
12087494Sache			c -= '0';
12187494Sache		else if (c >= 'A' && c <= 'Z')
12287494Sache			c -= 'A' - 10;
12387494Sache		else if (c >= 'a' && c <= 'z')
12487494Sache			c -= 'a' - 10;
1251573Srgrimes		else
1261573Srgrimes			break;
12787494Sache		if (c >= base)
1281573Srgrimes			break;
12987494Sache		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1301573Srgrimes			any = -1;
1311573Srgrimes		else {
1321573Srgrimes			any = 1;
1331573Srgrimes			acc *= base;
13487494Sache			acc += c;
1351573Srgrimes		}
1361573Srgrimes	}
1371573Srgrimes	if (any < 0) {
1381573Srgrimes		acc = neg ? LONG_MIN : LONG_MAX;
1391573Srgrimes		errno = ERANGE;
14082975Sache	} else if (!any) {
14182975Sachenoconv:
14282975Sache		errno = EINVAL;
1431573Srgrimes	} else if (neg)
1441573Srgrimes		acc = -acc;
14582975Sache	if (endptr != NULL)
1461573Srgrimes		*endptr = (char *)(any ? s - 1 : nptr);
1471573Srgrimes	return (acc);
1481573Srgrimes}
149227753Stheravenlong
150227753Stheravenstrtol(const char * __restrict nptr, char ** __restrict endptr, int base)
151227753Stheraven{
152227753Stheraven	return strtol_l(nptr, endptr, base, __get_locale());
153227753Stheraven}
154227753Stheravenlong double
155227753Stheravenstrtold(const char * __restrict nptr, char ** __restrict endptr)
156227753Stheraven{
157227753Stheraven	return strtold_l(nptr, endptr, __get_locale());
158227753Stheraven}
159