1 /* strtoul.c - replacement strtoul */
2 /* (C) 2004 by Matthias Andree. License: GNU GPL v2. */
3 
4 #include <errno.h>
5 #include <limits.h>
6 
strtoul(const char * str,char ** endptr,int base)7 unsigned long strtoul(const char *str, char **endptr, int base)
8 {
9     unsigned long ret = 0;
10     if (base != 10) { errno = EINVAL; return 0ul; }
11 
12     while (*str >= '0' && *str <= '9')
13     {
14 	if (ret * 10 < ret) goto ovl;
15 	ret *= 10;
16 	if (ret + (*str - '0') < ret) goto ovl;
17 	ret += (*str - '0');
18 	str++;
19     }
20     if (endptr) *endptr = (char *)str;
21     return 0;
22 ovl:
23     errno = ERANGE;
24     return ULONG_MAX;
25 }
26