blob: 9c347d0daef96f0ba537341210e777305b0e9834 (
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
|
#include <ctype.h>
#include <errno.h>
#include <string.h>
long strtol(const char *restrict s, char **restrict end, int base) {
long res = 0;
int sign = 1;
while (isspace(*s)) s++;
if (*s == '+') {
s++;
} else if (*s == '-') {
s++;
sign = -1;
}
if (base == 0) {
if (*s == '0') {
s++;
if (*s == 'x' || *s == 'X') {
s++;
base = 16;
} else {
base = 8;
}
} else {
base = 10;
}
}
for (;;) {
unsigned char digit = *s;
if ('0' <= digit && digit <= '9') digit -= '0';
else if ('a' <= digit && digit <= 'z') digit -= 'a' - 10;
else if ('A' <= digit && digit <= 'Z') digit -= 'A' - 10;
else break;
if (digit >= base) break;
// TODO overflow check
res *= base;
res += digit;
s++;
}
if (end) *end = (void*)s;
return res * sign;
}
char *strchr(const char *s, int c) {
while (*s) {
if (*s == c) return (char*)s;
s++;
}
return NULL;
}
|