Home | History | Annotate | Line # | Download | only in locale
wcstol.c revision 1.1
      1 /*	$NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp $	*/
      2 /* $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstol.c,v 1.2 2001/09/21 16:11:41 yamt Exp $ */
      3 
      4 /*-
      5  * Copyright (c) 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 #if 0
     40 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
     41 #else
     42 __RCSID("$NetBSD: wcstol.c,v 1.1 2001/09/27 16:30:36 yamt Exp $");
     43 #endif
     44 #endif /* LIBC_SCCS and not lint */
     45 
     46 #include <assert.h>
     47 #include <ctype.h>
     48 #include <errno.h>
     49 #include <limits.h>
     50 #include <stdlib.h>
     51 #include <wchar.h>
     52 #include <wctype.h>
     53 
     54 #include "__wctoint.h"
     55 
     56 /*
     57  * Convert a wide-char string to a long integer.
     58  */
     59 long
     60 wcstol(nptr, endptr, base)
     61 	const wchar_t *nptr;
     62 	wchar_t **endptr;
     63 	int base;
     64 {
     65 	const wchar_t *s;
     66 	long acc, cutoff;
     67 	wint_t wc;
     68 	int i;
     69 	int neg, any, cutlim;
     70 
     71 	_DIAGASSERT(nptr != NULL);
     72 	/* endptr may be NULL */
     73 
     74 	/* check base value */
     75 	if (base && (base < 2 || base > 36)) {
     76 		errno = EINVAL;
     77 		return 0;
     78 	}
     79 
     80 	/*
     81 	 * Skip white space and pick up leading +/- sign if any.
     82 	 * If base is 0, allow 0x for hex and 0 for octal, else
     83 	 * assume decimal; if base is already 16, allow 0x.
     84 	 */
     85 	s = nptr;
     86 	do {
     87 		wc = (wchar_t) *s++;
     88 	} while (iswspace(wc));
     89 	if (wc == L'-') {
     90 		neg = 1;
     91 		wc = *s++;
     92 	} else {
     93 		neg = 0;
     94 		if (wc == L'+')
     95 			wc = *s++;
     96 	}
     97 	if ((base == 0 || base == 16) &&
     98 	    wc == L'0' && (*s == L'x' || *s == L'X')) {
     99 		wc = s[1];
    100 		s += 2;
    101 		base = 16;
    102 	}
    103 	if (base == 0)
    104 		base = wc == '0' ? 8 : 10;
    105 
    106 	/*
    107 	 * Compute the cutoff value between legal numbers and illegal
    108 	 * numbers.  That is the largest legal value, divided by the
    109 	 * base.  An input number that is greater than this value, if
    110 	 * followed by a legal input character, is too big.  One that
    111 	 * is equal to this value may be valid or not; the limit
    112 	 * between valid and invalid numbers is then based on the last
    113 	 * digit.  For instance, if the range for longs is
    114 	 * [-2147483648..2147483647] and the input base is 10,
    115 	 * cutoff will be set to 214748364 and cutlim to either
    116 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
    117 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
    118 	 * the number is too big, and we will return a range error.
    119 	 *
    120 	 * Set any if any `digits' consumed; make it negative to indicate
    121 	 * overflow.
    122 	 */
    123 	cutoff = neg ? LONG_MIN : LONG_MAX;
    124 	cutlim = (int)(cutoff % base);
    125 	cutoff /= base;
    126 	if (neg) {
    127 		if (cutlim > 0) {
    128 			cutlim -= base;
    129 			cutoff += 1;
    130 		}
    131 		cutlim = -cutlim;
    132 	}
    133 	for (acc = 0, any = 0;; wc = (wchar_t) *s++) {
    134 		i = __wctoint(wc);
    135 		if (i == -1)
    136 			break;
    137 		if (i >= base)
    138 			break;
    139 		if (any < 0)
    140 			continue;
    141 		if (neg) {
    142 			if (acc < cutoff || (acc == cutoff && i > cutlim)) {
    143 				any = -1;
    144 				acc = LONG_MIN;
    145 				errno = ERANGE;
    146 			} else {
    147 				any = 1;
    148 				acc *= base;
    149 				acc -= i;
    150 			}
    151 		} else {
    152 			if (acc > cutoff || (acc == cutoff && i > cutlim)) {
    153 				any = -1;
    154 				acc = LONG_MAX;
    155 				errno = ERANGE;
    156 			} else {
    157 				any = 1;
    158 				acc *= base;
    159 				acc += i;
    160 			}
    161 		}
    162 	}
    163 	if (endptr != 0)
    164 		/* LINTED interface specification */
    165 		*endptr = (wchar_t *)(any ? s - 1 : nptr);
    166 	return (acc);
    167 }
    168