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. FILE f;
  10. sh_fromstring(&f, s);
  11. shlim(&f, 0);
  12. unsigned long long y = __intscan(&f, base, 1, lim);
  13. if (p) {
  14. size_t cnt = shcnt(&f);
  15. *p = (char *)s + cnt;
  16. }
  17. return y;
  18. }
  19. unsigned long long strtoull(const char *restrict s, char **restrict p, int base)
  20. {
  21. return strtox(s, p, base, ULLONG_MAX);
  22. }
  23. long long strtoll(const char *restrict s, char **restrict p, int base)
  24. {
  25. return strtox(s, p, base, LLONG_MIN);
  26. }
  27. unsigned long strtoul(const char *restrict s, char **restrict p, int base)
  28. {
  29. return strtox(s, p, base, ULONG_MAX);
  30. }
  31. long strtol(const char *restrict s, char **restrict p, int base)
  32. {
  33. return strtox(s, p, base, 0UL+LONG_MIN);
  34. }
  35. intmax_t strtoimax(const char *restrict s, char **restrict p, int base)
  36. {
  37. return strtoll(s, p, base);
  38. }
  39. uintmax_t strtoumax(const char *restrict s, char **restrict p, int base)
  40. {
  41. return strtoull(s, p, base);
  42. }
  43. weak_alias(strtol, __strtol_internal);
  44. weak_alias(strtoul, __strtoul_internal);
  45. weak_alias(strtoll, __strtoll_internal);
  46. weak_alias(strtoull, __strtoull_internal);
  47. weak_alias(strtoimax, __strtoimax_internal);
  48. weak_alias(strtoumax, __strtoumax_internal);