199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1990, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LIBC_SCCS) && !defined(lint)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* LIBC_SCCS and not lint */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Convert a string to a long integer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ignores `locale' stuff. Assumes that the upper and lower case
199767f8919635c4928607450d9e0abb932109ceToomas Soome * alphabets and digits are each contiguous.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelong
199767f8919635c4928607450d9e0abb932109ceToomas Soomestrtol(nptr, endptr, base)
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *nptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **endptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long acc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long cutoff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int neg = 0, any, cutlim;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Be sensible about NULL strings */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nptr == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome nptr = "";
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = nptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Skip white space and pick up leading +/- sign if any.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If base is 0, allow 0x for hex and 0 for octal, else
199767f8919635c4928607450d9e0abb932109ceToomas Soome * assume decimal; if base is already 16, allow 0x.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = *s++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (isspace(c));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == '-') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome neg = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = *s++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (c == '+')
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = *s++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((base == 0 || base == 16) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome c == '0' && (*s == 'x' || *s == 'X')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = s[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome s += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = 16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (base == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome base = c == '0' ? 8 : 10;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Compute the cutoff value between legal numbers and illegal
199767f8919635c4928607450d9e0abb932109ceToomas Soome * numbers. That is the largest legal value, divided by the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * base. An input number that is greater than this value, if
199767f8919635c4928607450d9e0abb932109ceToomas Soome * followed by a legal input character, is too big. One that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is equal to this value may be valid or not; the limit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * between valid and invalid numbers is then based on the last
199767f8919635c4928607450d9e0abb932109ceToomas Soome * digit. For instance, if the range for longs is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * [-2147483648..2147483647] and the input base is 10,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * cutoff will be set to 214748364 and cutlim to either
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a value > 214748364, or equal but the next digit is > 7 (or 8),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the number is too big, and we will return a range error.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set any if any `digits' consumed; make it negative to indicate
199767f8919635c4928607450d9e0abb932109ceToomas Soome * overflow.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cutlim = cutoff % (unsigned long)base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cutoff /= (unsigned long)base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (acc = 0, any = 0;; c = *s++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!isascii(c))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (isdigit(c))
199767f8919635c4928607450d9e0abb932109ceToomas Soome c -= '0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (isalpha(c))
199767f8919635c4928607450d9e0abb932109ceToomas Soome c -= isupper(c) ? 'A' - 10 : 'a' - 10;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c >= base)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
199767f8919635c4928607450d9e0abb932109ceToomas Soome any = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome any = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome acc *= base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome acc += c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (any < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome acc = neg ? LONG_MIN : LONG_MAX;
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = ERANGE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (neg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome acc = -acc;
00b631909040bb219a21436e55f74fc367c62176Toomas Soome if (endptr != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *endptr = (char *)(any ? s - 1 : nptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (acc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}