Commit | Line | Data |
---|---|---|
bd99994b RP |
1 | #include <string.h> |
2 | #include <stdlib.h> | |
3 | #include <limits.h> | |
4 | ||
5 | long strntol(const char *str, size_t sz, char **end, int base) | |
6 | { | |
7 | /* Expect that digit representation of LONG_MAX/MIN | |
8 | * not greater than this buffer */ | |
9 | char buf[24]; | |
10 | long ret; | |
11 | const char *beg = str; | |
12 | ||
13 | /* Catch up leading spaces */ | |
14 | for (; beg && sz && *beg == ' '; beg++, sz--) | |
15 | ; | |
16 | ||
17 | if (!sz || sz >= sizeof(buf)) { | |
18 | if (end) | |
19 | *end = (char *)str; | |
20 | return 0; | |
21 | } | |
22 | ||
23 | memcpy(buf, beg, sz); | |
24 | buf[sz] = '\0'; | |
25 | ret = strtol(buf, end, base); | |
26 | if (ret == LONG_MIN || ret == LONG_MAX) | |
27 | return ret; | |
28 | if (end) | |
29 | *end = (char *)str + (*end - buf); | |
30 | return ret; | |
31 | } |