Home | History | Annotate | Line # | Download | only in stdlib
strtoll.c revision 1.2
      1  1.2  christos /*	$NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*-
      4  1.1   thorpej  * Copyright (c) 1992, 1993
      5  1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1   thorpej  * modification, are permitted provided that the following conditions
      9  1.1   thorpej  * are met:
     10  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     16  1.1   thorpej  *    may be used to endorse or promote products derived from this software
     17  1.1   thorpej  *    without specific prior written permission.
     18  1.1   thorpej  *
     19  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1   thorpej  * SUCH DAMAGE.
     30  1.1   thorpej  */
     31  1.1   thorpej 
     32  1.1   thorpej #if HAVE_NBTOOL_CONFIG_H
     33  1.1   thorpej #include "nbtool_config.h"
     34  1.1   thorpej #endif
     35  1.1   thorpej 
     36  1.1   thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
     37  1.1   thorpej #include <sys/cdefs.h>
     38  1.1   thorpej #if defined(LIBC_SCCS) && !defined(lint)
     39  1.1   thorpej #if 0
     40  1.1   thorpej static char sccsid[] = "from: @(#)strtoq.c	8.1 (Berkeley) 6/4/93";
     41  1.1   thorpej #else
     42  1.2  christos __RCSID("$NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $");
     43  1.1   thorpej #endif
     44  1.1   thorpej #endif /* LIBC_SCCS and not lint */
     45  1.1   thorpej 
     46  1.1   thorpej #ifdef _LIBC
     47  1.1   thorpej #include "namespace.h"
     48  1.1   thorpej #endif
     49  1.1   thorpej 
     50  1.1   thorpej #include <assert.h>
     51  1.1   thorpej #include <ctype.h>
     52  1.1   thorpej #include <errno.h>
     53  1.1   thorpej #include <limits.h>
     54  1.1   thorpej #include <stdlib.h>
     55  1.1   thorpej 
     56  1.1   thorpej #ifdef _LIBC
     57  1.1   thorpej #ifdef __weak_alias
     58  1.1   thorpej __weak_alias(strtoll, _strtoll)
     59  1.1   thorpej #endif
     60  1.1   thorpej #endif
     61  1.1   thorpej 
     62  1.1   thorpej #else /* !_KERNEL && !_STANDALONE */
     63  1.1   thorpej #include <sys/param.h>
     64  1.1   thorpej #include <lib/libkern/libkern.h>
     65  1.1   thorpej #define	isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r')
     66  1.1   thorpej #define	isdigit(x) ((x) >= '0' && (x) <= '9')
     67  1.1   thorpej #define	isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
     68  1.1   thorpej #define	toupper(x) ((x) & ~0x20)
     69  1.1   thorpej #endif
     70  1.1   thorpej 
     71  1.1   thorpej #if !HAVE_STRTOLL
     72  1.1   thorpej /*
     73  1.1   thorpej  * Convert a string to a long long integer.
     74  1.1   thorpej  *
     75  1.1   thorpej  * Ignores `locale' stuff.  Assumes that the upper and lower case
     76  1.1   thorpej  * alphabets and digits are each contiguous.
     77  1.1   thorpej  */
     78  1.1   thorpej /* LONGLONG */
     79  1.1   thorpej long long int
     80  1.1   thorpej #ifdef _LIBC
     81  1.1   thorpej _strtoll(nptr, endptr, base)
     82  1.1   thorpej #else
     83  1.1   thorpej strtoll(nptr, endptr, base)
     84  1.1   thorpej #endif
     85  1.1   thorpej 	const char *nptr;
     86  1.1   thorpej 	char **endptr;
     87  1.1   thorpej 	int base;
     88  1.1   thorpej {
     89  1.1   thorpej 	const char *s;
     90  1.1   thorpej 	/* LONGLONG */
     91  1.1   thorpej 	long long int acc, cutoff;
     92  1.1   thorpej 	int c;
     93  1.1   thorpej 	int neg, any, cutlim;
     94  1.1   thorpej 
     95  1.1   thorpej 	_DIAGASSERT(nptr != NULL);
     96  1.1   thorpej 	/* endptr may be NULL */
     97  1.1   thorpej 
     98  1.1   thorpej 	/*
     99  1.1   thorpej 	 * Skip white space and pick up leading +/- sign if any.
    100  1.1   thorpej 	 * If base is 0, allow 0x for hex and 0 for octal, else
    101  1.1   thorpej 	 * assume decimal; if base is already 16, allow 0x.
    102  1.1   thorpej 	 */
    103  1.1   thorpej 	s = nptr;
    104  1.1   thorpej 	do {
    105  1.1   thorpej 		c = (unsigned char) *s++;
    106  1.1   thorpej 	} while (isspace(c));
    107  1.1   thorpej 	if (c == '-') {
    108  1.1   thorpej 		neg = 1;
    109  1.1   thorpej 		c = *s++;
    110  1.1   thorpej 	} else {
    111  1.1   thorpej 		neg = 0;
    112  1.1   thorpej 		if (c == '+')
    113  1.1   thorpej 			c = *s++;
    114  1.1   thorpej 	}
    115  1.1   thorpej 	if ((base == 0 || base == 16) &&
    116  1.1   thorpej 	    c == '0' && (*s == 'x' || *s == 'X')) {
    117  1.1   thorpej 		c = s[1];
    118  1.1   thorpej 		s += 2;
    119  1.1   thorpej 		base = 16;
    120  1.1   thorpej 	}
    121  1.1   thorpej 	if (base == 0)
    122  1.1   thorpej 		base = c == '0' ? 8 : 10;
    123  1.1   thorpej 
    124  1.1   thorpej 	/*
    125  1.1   thorpej 	 * Compute the cutoff value between legal numbers and illegal
    126  1.1   thorpej 	 * numbers.  That is the largest legal value, divided by the
    127  1.1   thorpej 	 * base.  An input number that is greater than this value, if
    128  1.1   thorpej 	 * followed by a legal input character, is too big.  One that
    129  1.1   thorpej 	 * is equal to this value may be valid or not; the limit
    130  1.1   thorpej 	 * between valid and invalid numbers is then based on the last
    131  1.1   thorpej 	 * digit.  For instance, if the range for long longs is
    132  1.1   thorpej 	 * [-9223372036854775808..9223372036854775807] and the input base
    133  1.1   thorpej 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
    134  1.1   thorpej 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
    135  1.1   thorpej 	 * accumulated a value > 922337203685477580, or equal but the
    136  1.1   thorpej 	 * next digit is > 7 (or 8), the number is too big, and we will
    137  1.1   thorpej 	 * return a range error.
    138  1.1   thorpej 	 *
    139  1.1   thorpej 	 * Set any if any `digits' consumed; make it negative to indicate
    140  1.1   thorpej 	 * overflow.
    141  1.1   thorpej 	 */
    142  1.1   thorpej 	cutoff = neg ? LLONG_MIN : LLONG_MAX;
    143  1.1   thorpej 	cutlim = (int)(cutoff % base);
    144  1.1   thorpej 	cutoff /= base;
    145  1.1   thorpej 	if (neg) {
    146  1.1   thorpej 		if (cutlim > 0) {
    147  1.1   thorpej 			cutlim -= base;
    148  1.1   thorpej 			cutoff += 1;
    149  1.1   thorpej 		}
    150  1.1   thorpej 		cutlim = -cutlim;
    151  1.1   thorpej 	}
    152  1.1   thorpej 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
    153  1.1   thorpej 		if (isdigit(c))
    154  1.1   thorpej 			c -= '0';
    155  1.1   thorpej 		else if (isalpha(c)) {
    156  1.1   thorpej #if defined(_KERNEL) || defined(_STANDALONE)
    157  1.1   thorpej 			c = toupper(c) - 'A' + 10;
    158  1.1   thorpej #else
    159  1.1   thorpej 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
    160  1.1   thorpej #endif
    161  1.1   thorpej 		} else
    162  1.1   thorpej 			break;
    163  1.1   thorpej 		if (c >= base)
    164  1.1   thorpej 			break;
    165  1.1   thorpej 		if (any < 0)
    166  1.1   thorpej 			continue;
    167  1.1   thorpej 		if (neg) {
    168  1.1   thorpej 			if (acc < cutoff || (acc == cutoff && c > cutlim)) {
    169  1.1   thorpej #if defined(_KERNEL) || defined(_STANDALONE)
    170  1.1   thorpej 				if (endptr)
    171  1.1   thorpej 					*endptr = __UNCONST(nptr);
    172  1.1   thorpej 				return (LLONG_MIN);
    173  1.1   thorpej #else
    174  1.1   thorpej 				any = -1;
    175  1.1   thorpej 				acc = LLONG_MIN;
    176  1.1   thorpej 				errno = ERANGE;
    177  1.1   thorpej #endif
    178  1.1   thorpej 			} else {
    179  1.1   thorpej 				any = 1;
    180  1.1   thorpej 				acc *= base;
    181  1.1   thorpej 				acc -= c;
    182  1.1   thorpej 			}
    183  1.1   thorpej 		} else {
    184  1.1   thorpej 			if (acc > cutoff || (acc == cutoff && c > cutlim)) {
    185  1.1   thorpej #if defined(_KERNEL) || defined(_STANDALONE)
    186  1.1   thorpej 				if (endptr)
    187  1.1   thorpej 					*endptr = __UNCONST(nptr);
    188  1.1   thorpej 				return (LLONG_MAX);
    189  1.1   thorpej #else
    190  1.1   thorpej 				any = -1;
    191  1.1   thorpej 				acc = LLONG_MAX;
    192  1.1   thorpej 				errno = ERANGE;
    193  1.1   thorpej #endif
    194  1.1   thorpej 			} else {
    195  1.1   thorpej 				any = 1;
    196  1.1   thorpej 				acc *= base;
    197  1.1   thorpej 				acc += c;
    198  1.1   thorpej 			}
    199  1.1   thorpej 		}
    200  1.1   thorpej 	}
    201  1.1   thorpej 	if (endptr != 0)
    202  1.1   thorpej 		*endptr = __UNCONST(any ? s - 1 : nptr);
    203  1.1   thorpej 	return (acc);
    204  1.1   thorpej }
    205  1.1   thorpej #endif
    206