strtol.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim)
  8. {
  9. /* FIXME: use a helper function or macro to setup the FILE */
  10. FILE f;
  11. f.flags = 0;
  12. f.buf = f.rpos = (void *)s;
  13. if ((size_t)s > (size_t)-1/2)
  14. f.rend = (void *)-1;
  15. else
  16. f.rend = (unsigned char *)s+(size_t)-1/2;
  17. f.lock = -1;
  18. shlim(&f, 0);
  19. unsigned long long y = __intscan(&f, base, 1, lim);
  20. if (p) {
  21. size_t cnt = shcnt(&f);
  22. *p = (char *)s + cnt;
  23. }
  24. return y;
  25. }
  26. unsigned long long strtoull(const char *restrict s, char **restrict p, int base)
  27. {
  28. return strtox(s, p, base, ULLONG_MAX);
  29. }
  30. long long strtoll(const char *restrict s, char **restrict p, int base)
  31. {
  32. return strtox(s, p, base, LLONG_MIN);
  33. }
  34. unsigned long strtoul(const char *restrict s, char **restrict p, int base)
  35. {
  36. return strtox(s, p, base, ULONG_MAX);
  37. }
  38. long strtol(const char *restrict s, char **restrict p, int base)
  39. {
  40. return strtox(s, p, base, 0UL+LONG_MIN);
  41. }
  42. intmax_t strtoimax(const char *restrict s, char **restrict p, int base)
  43. {
  44. return strtoll(s, p, base);
  45. }
  46. uintmax_t strtoumax(const char *restrict s, char **restrict p, int base)
  47. {
  48. return strtoull(s, p, base);
  49. }