strtod.c 648 B

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