blob: 39dc8ebb15b95a31270c680fd592a120e3a3dfb2 (
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
extern int errno;
int get_value(char c, long base) {
int r;
int l = tolower(c);
if (l >= '0' && l <= '9') {
r = l - '0';
} else if (l >= 'a' && l <= 'z') {
r = (l - 'a') + 10;
} else {
return -1;
}
if (r >= base) {
return -1;
}
return r;
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
unsigned long strtoul(const char *restrict str, char **restrict endptr,
int base) {
unsigned long ret_value = 0;
if (endptr)
*endptr = (char *)str;
// Ignore inital white-space sequence
for (; *str && isspace(*str); str++)
;
if (!*str)
return ret_value;
// int sign = 0;
if ('-' == *str) {
// FIXME
// sign = 1;
str++;
assert(0);
} else if ('+' == *str) {
str++;
}
if (0 == base) {
char prefix = *str;
if ('0' == prefix) {
str++;
if ('x' == tolower(*str)) {
str++;
base = 16;
} else {
base = 8;
}
} else {
base = 10;
}
}
if (2 <= base && 36 >= base) {
for (; *str; str++) {
int val = get_value(*str, base);
if (-1 == val) {
break;
}
if (-1 == val) {
break;
}
ret_value *= base;
if (ret_value > ULONG_MAX - val) {
errno = ERANGE;
return 0;
}
ret_value += val;
}
} else {
errno = EINVAL;
return 0;
}
if (endptr)
*endptr = (char *)str;
return ret_value;
}
|