b444f53056ba7f92a338017e4538ece774a78c2b
[musl] / src / stdlib / strtod.c
1 #include <stdlib.h>
2 #include "floatscan.h"
3 #include "stdio_impl.h"
4
5 static long double strtox(const char *s, char **p, int prec)
6 {
7         char *t = (char *)s;
8         while (isspace(*t)) t++;
9         FILE f = {
10                 .buf = (void *)t, .rpos = (void *)t,
11                 .rend = (void *)-1, .lock = -1
12         };
13         off_t cnt;
14         long double y = __floatscan(&f, -1, prec, 1, &cnt);
15         if (p) *p = cnt ? t + cnt : (char *)s;
16         return y;
17 }
18
19 float strtof(const char *s, char **p)
20 {
21         return strtox(s, p, 0);
22 }
23
24 double strtod(const char *s, char **p)
25 {
26         return strtox(s, p, 1);
27 }
28
29 long double strtold(const char *s, char **p)
30 {
31         return strtox(s, p, 2);
32 }