clean up stdio_impl.h
[musl] / src / stdlib / strtol.c
1 #include "stdio_impl.h"
2 #include "intscan.h"
3 #include "shgetc.h"
4 #include <inttypes.h>
5 #include <limits.h>
6 #include <ctype.h>
7
8 static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim)
9 {
10         /* FIXME: use a helper function or macro to setup the FILE */
11         FILE f;
12         f.flags = 0;
13         f.buf = f.rpos = (void *)s;
14         if ((size_t)s > (size_t)-1/2)
15                 f.rend = (void *)-1;
16         else
17                 f.rend = (unsigned char *)s+(size_t)-1/2;
18         f.lock = -1;
19         shlim(&f, 0);
20         unsigned long long y = __intscan(&f, base, 1, lim);
21         if (p) {
22                 size_t cnt = shcnt(&f);
23                 *p = (char *)s + cnt;
24         }
25         return y;
26 }
27
28 unsigned long long strtoull(const char *restrict s, char **restrict p, int base)
29 {
30         return strtox(s, p, base, ULLONG_MAX);
31 }
32
33 long long strtoll(const char *restrict s, char **restrict p, int base)
34 {
35         return strtox(s, p, base, LLONG_MIN);
36 }
37
38 unsigned long strtoul(const char *restrict s, char **restrict p, int base)
39 {
40         return strtox(s, p, base, ULONG_MAX);
41 }
42
43 long strtol(const char *restrict s, char **restrict p, int base)
44 {
45         return strtox(s, p, base, 0UL+LONG_MIN);
46 }
47
48 intmax_t strtoimax(const char *restrict s, char **restrict p, int base)
49 {
50         return strtoll(s, p, base);
51 }
52
53 uintmax_t strtoumax(const char *restrict s, char **restrict p, int base)
54 {
55         return strtoull(s, p, base);
56 }