a898b1d421867146ac662e512894e4a67f6486c2
[musl] / src / stdlib / strtod.c
1 #include <stdlib.h>
2 #include "shgetc.h"
3 #include "floatscan.h"
4 #include "stdio_impl.h"
5
6 static long double strtox(const char *s, char **p, int prec)
7 {
8         FILE f = {
9                 .buf = (void *)s, .rpos = (void *)s,
10                 .rend = (void *)-1, .lock = -1
11         };
12         shlim(&f, 0);
13         long double y = __floatscan(&f, prec, 1);
14         off_t cnt = shcnt(&f);
15         if (p) *p = cnt ? (char *)s + cnt : (char *)s;
16         return y;
17 }
18
19 float strtof(const char *restrict s, char **restrict p)
20 {
21         return strtox(s, p, 0);
22 }
23
24 double strtod(const char *restrict s, char **restrict p)
25 {
26         return strtox(s, p, 1);
27 }
28
29 long double strtold(const char *restrict s, char **restrict p)
30 {
31         return strtox(s, p, 2);
32 }
33
34 weak_alias(strtof, strtof_l);
35 weak_alias(strtod, strtod_l);
36 weak_alias(strtold, strtold_l);
37 weak_alias(strtof, __strtof_l);
38 weak_alias(strtod, __strtod_l);
39 weak_alias(strtold, __strtold_l);