summaryrefslogtreecommitdiff
path: root/src/stdlib/wcstoimax.c
blob: 861fcb547955c3d87b154027876975121ad11d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <wchar.h>
#include <inttypes.h>
#include <errno.h>

intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base)
{
	int sign = 0;
	uintmax_t x;

	/* Initial whitespace */
	for (; iswspace(*s); s++);

	/* Optional sign */
	if (*s == '-') sign = *s++;
	else if (*s == '+') s++;

	x = wcstoumax(s, p, base);
	if (x > INTMAX_MAX) {
		if (!sign || -x != INTMAX_MIN)
			errno = ERANGE;
		return sign ? INTMAX_MIN : INTMAX_MAX;
	}
	return sign ? -x : x;
}