Home | History | Annotate | Line # | Download | only in locale
wcstoul.c revision 1.1.2.2
      1  1.1.2.2  nathanw /*	$NetBSD: wcstoul.c,v 1.1.2.2 2001/10/08 20:19:59 nathanw Exp $	*/
      2  1.1.2.2  nathanw /* $Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstoul.c,v 1.2 2001/09/21 16:11:41 yamt Exp $ */
      3  1.1.2.2  nathanw 
      4  1.1.2.2  nathanw /*
      5  1.1.2.2  nathanw  * Copyright (c) 1990, 1993
      6  1.1.2.2  nathanw  *	The Regents of the University of California.  All rights reserved.
      7  1.1.2.2  nathanw  *
      8  1.1.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.2  nathanw  * modification, are permitted provided that the following conditions
     10  1.1.2.2  nathanw  * are met:
     11  1.1.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     17  1.1.2.2  nathanw  *    must display the following acknowledgement:
     18  1.1.2.2  nathanw  *	This product includes software developed by the University of
     19  1.1.2.2  nathanw  *	California, Berkeley and its contributors.
     20  1.1.2.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     21  1.1.2.2  nathanw  *    may be used to endorse or promote products derived from this software
     22  1.1.2.2  nathanw  *    without specific prior written permission.
     23  1.1.2.2  nathanw  *
     24  1.1.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1.2.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1.2.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1.2.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1.2.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1.2.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1.2.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1.2.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1.2.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1.2.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1.2.2  nathanw  * SUCH DAMAGE.
     35  1.1.2.2  nathanw  */
     36  1.1.2.2  nathanw 
     37  1.1.2.2  nathanw #include <sys/cdefs.h>
     38  1.1.2.2  nathanw #if defined(LIBC_SCCS) && !defined(lint)
     39  1.1.2.2  nathanw #if 0
     40  1.1.2.2  nathanw static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
     41  1.1.2.2  nathanw #else
     42  1.1.2.2  nathanw __RCSID("$NetBSD: wcstoul.c,v 1.1.2.2 2001/10/08 20:19:59 nathanw Exp $");
     43  1.1.2.2  nathanw #endif
     44  1.1.2.2  nathanw #endif /* LIBC_SCCS and not lint */
     45  1.1.2.2  nathanw 
     46  1.1.2.2  nathanw #include <assert.h>
     47  1.1.2.2  nathanw #include <ctype.h>
     48  1.1.2.2  nathanw #include <errno.h>
     49  1.1.2.2  nathanw #include <limits.h>
     50  1.1.2.2  nathanw #include <stdlib.h>
     51  1.1.2.2  nathanw #include <wchar.h>
     52  1.1.2.2  nathanw #include <wctype.h>
     53  1.1.2.2  nathanw 
     54  1.1.2.2  nathanw #include "__wctoint.h"
     55  1.1.2.2  nathanw 
     56  1.1.2.2  nathanw /*
     57  1.1.2.2  nathanw  * Convert a wide-char string to an unsigned long integer.
     58  1.1.2.2  nathanw  */
     59  1.1.2.2  nathanw unsigned long
     60  1.1.2.2  nathanw wcstoul(nptr, endptr, base)
     61  1.1.2.2  nathanw 	const wchar_t *nptr;
     62  1.1.2.2  nathanw 	wchar_t **endptr;
     63  1.1.2.2  nathanw 	int base;
     64  1.1.2.2  nathanw {
     65  1.1.2.2  nathanw 	const wchar_t *s;
     66  1.1.2.2  nathanw 	unsigned long acc, cutoff;
     67  1.1.2.2  nathanw 	wint_t wc;
     68  1.1.2.2  nathanw 	int i;
     69  1.1.2.2  nathanw 	int neg, any, cutlim;
     70  1.1.2.2  nathanw 
     71  1.1.2.2  nathanw 	_DIAGASSERT(nptr != NULL);
     72  1.1.2.2  nathanw 	/* endptr may be NULL */
     73  1.1.2.2  nathanw 
     74  1.1.2.2  nathanw 	/* check base value */
     75  1.1.2.2  nathanw 	if (base && (base < 2 || base > 36)) {
     76  1.1.2.2  nathanw 		errno = EINVAL;
     77  1.1.2.2  nathanw 		return 0;
     78  1.1.2.2  nathanw 	}
     79  1.1.2.2  nathanw 
     80  1.1.2.2  nathanw 	/*
     81  1.1.2.2  nathanw 	 * See strtol for comments as to the logic used.
     82  1.1.2.2  nathanw 	 */
     83  1.1.2.2  nathanw 	s = nptr;
     84  1.1.2.2  nathanw 	do {
     85  1.1.2.2  nathanw 		wc = (wchar_t) *s++;
     86  1.1.2.2  nathanw 	} while (iswspace(wc));
     87  1.1.2.2  nathanw 	if (wc == L'-') {
     88  1.1.2.2  nathanw 		neg = 1;
     89  1.1.2.2  nathanw 		wc = *s++;
     90  1.1.2.2  nathanw 	} else {
     91  1.1.2.2  nathanw 		neg = 0;
     92  1.1.2.2  nathanw 		if (wc == L'+')
     93  1.1.2.2  nathanw 			wc = *s++;
     94  1.1.2.2  nathanw 	}
     95  1.1.2.2  nathanw 	if ((base == 0 || base == 16) &&
     96  1.1.2.2  nathanw 	    wc == L'0' && (*s == L'x' || *s == L'X')) {
     97  1.1.2.2  nathanw 		wc = s[1];
     98  1.1.2.2  nathanw 		s += 2;
     99  1.1.2.2  nathanw 		base = 16;
    100  1.1.2.2  nathanw 	}
    101  1.1.2.2  nathanw 	if (base == 0)
    102  1.1.2.2  nathanw 		base = wc == L'0' ? 8 : 10;
    103  1.1.2.2  nathanw 
    104  1.1.2.2  nathanw 	cutoff = ULONG_MAX / (unsigned long)base;
    105  1.1.2.2  nathanw 	cutlim = (int)(ULONG_MAX % (unsigned long)base);
    106  1.1.2.2  nathanw 	for (acc = 0, any = 0;; wc = (wchar_t) *s++) {
    107  1.1.2.2  nathanw 		i = __wctoint(wc);
    108  1.1.2.2  nathanw 		if (i == (wint_t)-1)
    109  1.1.2.2  nathanw 			break;
    110  1.1.2.2  nathanw 		if (i >= base)
    111  1.1.2.2  nathanw 			break;
    112  1.1.2.2  nathanw 		if (any < 0)
    113  1.1.2.2  nathanw 			continue;
    114  1.1.2.2  nathanw 		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
    115  1.1.2.2  nathanw 			any = -1;
    116  1.1.2.2  nathanw 			acc = ULONG_MAX;
    117  1.1.2.2  nathanw 			errno = ERANGE;
    118  1.1.2.2  nathanw 		} else {
    119  1.1.2.2  nathanw 			any = 1;
    120  1.1.2.2  nathanw 			acc *= (unsigned long)base;
    121  1.1.2.2  nathanw 			acc += i;
    122  1.1.2.2  nathanw 		}
    123  1.1.2.2  nathanw 	}
    124  1.1.2.2  nathanw 	if (neg && any > 0)
    125  1.1.2.2  nathanw 		acc = -acc;
    126  1.1.2.2  nathanw 	if (endptr != 0)
    127  1.1.2.2  nathanw 		/* LINTED interface specification */
    128  1.1.2.2  nathanw 		*endptr = (wchar_t *)(any ? s - 1 : nptr);
    129  1.1.2.2  nathanw 	return (acc);
    130  1.1.2.2  nathanw }
    131