1*b6cbf720SGianluca Guida /* $NetBSD: _strtoul.h,v 1.1 2008/08/20 12:42:26 joerg Exp $ */ 2*b6cbf720SGianluca Guida 3*b6cbf720SGianluca Guida /*- 4*b6cbf720SGianluca Guida * Copyright (c) 1990, 1993 5*b6cbf720SGianluca Guida * The Regents of the University of California. All rights reserved. 6*b6cbf720SGianluca Guida * 7*b6cbf720SGianluca Guida * Redistribution and use in source and binary forms, with or without 8*b6cbf720SGianluca Guida * modification, are permitted provided that the following conditions 9*b6cbf720SGianluca Guida * are met: 10*b6cbf720SGianluca Guida * 1. Redistributions of source code must retain the above copyright 11*b6cbf720SGianluca Guida * notice, this list of conditions and the following disclaimer. 12*b6cbf720SGianluca Guida * 2. Redistributions in binary form must reproduce the above copyright 13*b6cbf720SGianluca Guida * notice, this list of conditions and the following disclaimer in the 14*b6cbf720SGianluca Guida * documentation and/or other materials provided with the distribution. 15*b6cbf720SGianluca Guida * 3. Neither the name of the University nor the names of its contributors 16*b6cbf720SGianluca Guida * may be used to endorse or promote products derived from this software 17*b6cbf720SGianluca Guida * without specific prior written permission. 18*b6cbf720SGianluca Guida * 19*b6cbf720SGianluca Guida * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20*b6cbf720SGianluca Guida * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21*b6cbf720SGianluca Guida * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22*b6cbf720SGianluca Guida * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23*b6cbf720SGianluca Guida * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24*b6cbf720SGianluca Guida * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25*b6cbf720SGianluca Guida * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26*b6cbf720SGianluca Guida * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27*b6cbf720SGianluca Guida * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28*b6cbf720SGianluca Guida * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29*b6cbf720SGianluca Guida * SUCH DAMAGE. 30*b6cbf720SGianluca Guida * 31*b6cbf720SGianluca Guida * Original version ID: 32*b6cbf720SGianluca Guida * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp 33*b6cbf720SGianluca Guida */ 34*b6cbf720SGianluca Guida 35*b6cbf720SGianluca Guida /* 36*b6cbf720SGianluca Guida * function template for strtoul, strtoull and strtoumax. 37*b6cbf720SGianluca Guida * 38*b6cbf720SGianluca Guida * parameters: 39*b6cbf720SGianluca Guida * _FUNCNAME : function name 40*b6cbf720SGianluca Guida * __UINT : return type 41*b6cbf720SGianluca Guida * __UINT_MAX : upper limit of the return type 42*b6cbf720SGianluca Guida */ 43*b6cbf720SGianluca Guida 44*b6cbf720SGianluca Guida __UINT 45*b6cbf720SGianluca Guida _FUNCNAME(const char *nptr, char **endptr, int base) 46*b6cbf720SGianluca Guida { 47*b6cbf720SGianluca Guida const char *s; 48*b6cbf720SGianluca Guida __UINT acc, cutoff; 49*b6cbf720SGianluca Guida unsigned char c; 50*b6cbf720SGianluca Guida int i, neg, any, cutlim; 51*b6cbf720SGianluca Guida 52*b6cbf720SGianluca Guida _DIAGASSERT(nptr != NULL); 53*b6cbf720SGianluca Guida /* endptr may be NULL */ 54*b6cbf720SGianluca Guida 55*b6cbf720SGianluca Guida /* check base value */ 56*b6cbf720SGianluca Guida if (base && (base < 2 || base > 36)) { 57*b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE) 58*b6cbf720SGianluca Guida errno = EINVAL; 59*b6cbf720SGianluca Guida return(0); 60*b6cbf720SGianluca Guida #else 61*b6cbf720SGianluca Guida panic("%s: invalid base %d", __func__, base); 62*b6cbf720SGianluca Guida #endif 63*b6cbf720SGianluca Guida } 64*b6cbf720SGianluca Guida 65*b6cbf720SGianluca Guida /* 66*b6cbf720SGianluca Guida * Skip white space and pick up leading +/- sign if any. 67*b6cbf720SGianluca Guida * If base is 0, allow 0x for hex and 0 for octal, else 68*b6cbf720SGianluca Guida * assume decimal; if base is already 16, allow 0x. 69*b6cbf720SGianluca Guida */ 70*b6cbf720SGianluca Guida s = nptr; 71*b6cbf720SGianluca Guida do { 72*b6cbf720SGianluca Guida c = *s++; 73*b6cbf720SGianluca Guida } while (isspace(c)); 74*b6cbf720SGianluca Guida if (c == '-') { 75*b6cbf720SGianluca Guida neg = 1; 76*b6cbf720SGianluca Guida c = *s++; 77*b6cbf720SGianluca Guida } else { 78*b6cbf720SGianluca Guida neg = 0; 79*b6cbf720SGianluca Guida if (c == '+') 80*b6cbf720SGianluca Guida c = *s++; 81*b6cbf720SGianluca Guida } 82*b6cbf720SGianluca Guida if ((base == 0 || base == 16) && 83*b6cbf720SGianluca Guida c == '0' && (*s == 'x' || *s == 'X')) { 84*b6cbf720SGianluca Guida c = s[1]; 85*b6cbf720SGianluca Guida s += 2; 86*b6cbf720SGianluca Guida base = 16; 87*b6cbf720SGianluca Guida } 88*b6cbf720SGianluca Guida if (base == 0) 89*b6cbf720SGianluca Guida base = (c == '0' ? 8 : 10); 90*b6cbf720SGianluca Guida 91*b6cbf720SGianluca Guida /* 92*b6cbf720SGianluca Guida * See strtol for comments as to the logic used. 93*b6cbf720SGianluca Guida */ 94*b6cbf720SGianluca Guida cutoff = __UINT_MAX / (__UINT)base; 95*b6cbf720SGianluca Guida cutlim = (int)(__UINT_MAX % (__UINT)base); 96*b6cbf720SGianluca Guida for (acc = 0, any = 0;; c = *s++) { 97*b6cbf720SGianluca Guida if (isdigit(c)) 98*b6cbf720SGianluca Guida i = c - '0'; 99*b6cbf720SGianluca Guida else if (isalpha(c)) 100*b6cbf720SGianluca Guida i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); 101*b6cbf720SGianluca Guida else 102*b6cbf720SGianluca Guida break; 103*b6cbf720SGianluca Guida if (i >= base) 104*b6cbf720SGianluca Guida break; 105*b6cbf720SGianluca Guida if (any < 0) 106*b6cbf720SGianluca Guida continue; 107*b6cbf720SGianluca Guida if (acc > cutoff || (acc == cutoff && i > cutlim)) { 108*b6cbf720SGianluca Guida acc = __UINT_MAX; 109*b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE) 110*b6cbf720SGianluca Guida any = -1; 111*b6cbf720SGianluca Guida errno = ERANGE; 112*b6cbf720SGianluca Guida #else 113*b6cbf720SGianluca Guida any = 0; 114*b6cbf720SGianluca Guida break; 115*b6cbf720SGianluca Guida #endif 116*b6cbf720SGianluca Guida } else { 117*b6cbf720SGianluca Guida any = 1; 118*b6cbf720SGianluca Guida acc *= (__UINT)base; 119*b6cbf720SGianluca Guida acc += i; 120*b6cbf720SGianluca Guida } 121*b6cbf720SGianluca Guida } 122*b6cbf720SGianluca Guida if (neg && any > 0) 123*b6cbf720SGianluca Guida acc = -acc; 124*b6cbf720SGianluca Guida if (endptr != NULL) 125*b6cbf720SGianluca Guida /* LINTED interface specification */ 126*b6cbf720SGianluca Guida *endptr = __UNCONST(any ? s - 1 : nptr); 127*b6cbf720SGianluca Guida return(acc); 128*b6cbf720SGianluca Guida } 129