xref: /minix3/lib/libc/locale/_wcstoul.h (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: _wcstoul.h,v 1.6 2013/05/17 12:55:57 joerg Exp $ */
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  *
312fe8fb19SBen Gras  * Original version ID:
322fe8fb19SBen Gras  * @(#)strtoul.c	8.1 (Berkeley) 6/4/93
332fe8fb19SBen Gras  * Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstoul.c,v 1.2 2001/09/21 16:11:41 yamt Exp
342fe8fb19SBen Gras  * NetBSD: wcstoul.c,v 1.1 2001/09/27 16:30:37 yamt Exp
352fe8fb19SBen Gras  */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /*
382fe8fb19SBen Gras  * function template for wcstoul, wcstoull and wcstoumax.
392fe8fb19SBen Gras  *
402fe8fb19SBen Gras  * parameters:
412fe8fb19SBen Gras  *	_FUNCNAME  : function name
422fe8fb19SBen Gras  *      __UINT     : return type
432fe8fb19SBen Gras  *      __UINT_MAX : upper limit of the return type
442fe8fb19SBen Gras  */
452fe8fb19SBen Gras 
46*84d9c625SLionel Sambuc #include <locale.h>
47*84d9c625SLionel Sambuc #include "setlocale_local.h"
48*84d9c625SLionel Sambuc #define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
49*84d9c625SLionel Sambuc #define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
50*84d9c625SLionel Sambuc 
51*84d9c625SLionel Sambuc static __UINT
INT_FUNCNAME(_int_,_FUNCNAME,_l)52*84d9c625SLionel Sambuc INT_FUNCNAME(_int_, _FUNCNAME, _l)(const wchar_t *nptr, wchar_t **endptr,
53*84d9c625SLionel Sambuc 				   int base, locale_t loc)
542fe8fb19SBen Gras {
552fe8fb19SBen Gras 	const wchar_t *s;
562fe8fb19SBen Gras 	__UINT acc, cutoff;
572fe8fb19SBen Gras 	wint_t wc;
582fe8fb19SBen Gras 	int i;
592fe8fb19SBen Gras 	int neg, any, cutlim;
602fe8fb19SBen Gras 
612fe8fb19SBen Gras 	_DIAGASSERT(nptr != NULL);
622fe8fb19SBen Gras 	/* endptr may be NULL */
632fe8fb19SBen Gras 
642fe8fb19SBen Gras 	if (base && (base < 2 || base > 36)) {
652fe8fb19SBen Gras 		errno = EINVAL;
662fe8fb19SBen Gras 		return 0;
672fe8fb19SBen Gras 	}
682fe8fb19SBen Gras 
692fe8fb19SBen Gras 	/*
702fe8fb19SBen Gras 	 * Skip white space and pick up leading +/- sign if any.
712fe8fb19SBen Gras 	 * If base is 0, allow 0x for hex and 0 for octal, else
722fe8fb19SBen Gras 	 * assume decimal; if base is already 16, allow 0x.
732fe8fb19SBen Gras 	 */
742fe8fb19SBen Gras 	s = nptr;
752fe8fb19SBen Gras 	do {
762fe8fb19SBen Gras 		wc = (wchar_t) *s++;
77*84d9c625SLionel Sambuc 	} while (iswspace_l(wc, loc));
782fe8fb19SBen Gras 	if (wc == L'-') {
792fe8fb19SBen Gras 		neg = 1;
802fe8fb19SBen Gras 		wc = *s++;
812fe8fb19SBen Gras 	} else {
822fe8fb19SBen Gras 		neg = 0;
832fe8fb19SBen Gras 		if (wc == L'+')
842fe8fb19SBen Gras 			wc = *s++;
852fe8fb19SBen Gras 	}
862fe8fb19SBen Gras 	if ((base == 0 || base == 16) &&
872fe8fb19SBen Gras 	    wc == L'0' && (*s == L'x' || *s == L'X')) {
882fe8fb19SBen Gras 		wc = s[1];
892fe8fb19SBen Gras 		s += 2;
902fe8fb19SBen Gras 		base = 16;
912fe8fb19SBen Gras 	}
922fe8fb19SBen Gras 	if (base == 0)
932fe8fb19SBen Gras 		base = wc == L'0' ? 8 : 10;
942fe8fb19SBen Gras 
952fe8fb19SBen Gras 	/*
962fe8fb19SBen Gras 	 * See strtoul for comments as to the logic used.
972fe8fb19SBen Gras 	 */
982fe8fb19SBen Gras 	cutoff = __UINT_MAX / (__UINT)base;
992fe8fb19SBen Gras 	cutlim = (int)(__UINT_MAX % (__UINT)base);
1002fe8fb19SBen Gras 	for (acc = 0, any = 0;; wc = (wchar_t) *s++) {
1012fe8fb19SBen Gras 		i = __wctoint(wc);
1022fe8fb19SBen Gras 		if (i == (wint_t)-1)
1032fe8fb19SBen Gras 			break;
1042fe8fb19SBen Gras 		if (i >= base)
1052fe8fb19SBen Gras 			break;
1062fe8fb19SBen Gras 		if (any < 0)
1072fe8fb19SBen Gras 			continue;
1082fe8fb19SBen Gras 		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
1092fe8fb19SBen Gras 			any = -1;
1102fe8fb19SBen Gras 			acc = __UINT_MAX;
1112fe8fb19SBen Gras 			errno = ERANGE;
1122fe8fb19SBen Gras 		} else {
1132fe8fb19SBen Gras 			any = 1;
1142fe8fb19SBen Gras 			acc *= (__UINT)base;
1152fe8fb19SBen Gras 			acc += i;
1162fe8fb19SBen Gras 		}
1172fe8fb19SBen Gras 	}
1182fe8fb19SBen Gras 	if (neg && any > 0)
1192fe8fb19SBen Gras 		acc = -acc;
1202fe8fb19SBen Gras 	if (endptr != 0)
1212fe8fb19SBen Gras 		*endptr = __UNCONST(any ? s - 1 : nptr);
1222fe8fb19SBen Gras 	return (acc);
1232fe8fb19SBen Gras }
124*84d9c625SLionel Sambuc 
125*84d9c625SLionel Sambuc __UINT
_FUNCNAME(const wchar_t * nptr,wchar_t ** endptr,int base)126*84d9c625SLionel Sambuc _FUNCNAME(const wchar_t *nptr, wchar_t **endptr, int base)
127*84d9c625SLionel Sambuc {
128*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base,
129*84d9c625SLionel Sambuc 						  _current_locale());
130*84d9c625SLionel Sambuc }
131*84d9c625SLionel Sambuc 
132*84d9c625SLionel Sambuc __UINT
133*84d9c625SLionel Sambuc INT_FUNCNAME(, _FUNCNAME, _l)(const wchar_t *nptr, wchar_t **endptr,
134*84d9c625SLionel Sambuc 			      int base, locale_t loc)
135*84d9c625SLionel Sambuc {
136*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);
137*84d9c625SLionel Sambuc }
138