#include <sys/cdefs.h>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "from @(#)strtoul.c 8.1 (Berkeley) 6/4/93";
#endif
__FBSDID("FreeBSD: src/lib/libc/stdlib/strtoumax.c,v 1.8 2002/09/06 11:23:59 tjr Exp ");
#endif
__FBSDID("$FreeBSD: src/lib/libc/locale/wcstoumax.c,v 1.2 2007/01/09 00:28:01 imp Exp $");
#include "xlocale_private.h"
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
uintmax_t
wcstoumax_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
int base, locale_t loc)
{
const wchar_t *s;
uintmax_t acc;
wchar_t c;
uintmax_t cutoff;
int neg, any, cutlim;
NORMALIZE_LOCALE(loc);
s = nptr;
do {
c = *s++;
} while (iswspace_l(c, loc));
if (c == L'-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == L'+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == L'0' && (*s == L'x' || *s == L'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == L'0' ? 8 : 10;
acc = any = 0;
if (base < 2 || base > 36)
goto noconv;
cutoff = UINTMAX_MAX / base;
cutlim = UINTMAX_MAX % base;
for ( ; ; c = *s++) {
#ifdef notyet
if (iswdigit_l(c, loc))
c = digittoint_l(c, loc);
else
#endif
if (c >= L'0' && c <= L'9')
c -= L'0';
else if (c >= L'A' && c <= L'Z')
c -= L'A' - 10;
else if (c >= L'a' && c <= L'z')
c -= L'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = UINTMAX_MAX;
errno = ERANGE;
} else if (!any) {
noconv:
errno = EINVAL;
} else if (neg)
acc = -acc;
if (endptr != NULL)
*endptr = (wchar_t *)(any ? s - 1 : nptr);
return (acc);
}
uintmax_t
wcstoumax(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
int base)
{
return wcstoumax_l(nptr, endptr, base, __current_locale());
}