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