3
0

xatonum.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ascii-to-numbers implementations for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #define type long long
  11. #define xstrtou(rest) xstrtoull##rest
  12. #define xstrto(rest) xstrtoll##rest
  13. #define xatou(rest) xatoull##rest
  14. #define xato(rest) xatoll##rest
  15. #define XSTR_UTYPE_MAX ULLONG_MAX
  16. #define XSTR_TYPE_MAX LLONG_MAX
  17. #define XSTR_TYPE_MIN LLONG_MIN
  18. #define XSTR_STRTOU strtoull
  19. #include "xatonum_template.c"
  20. #if ULONG_MAX != ULLONG_MAX
  21. #define type long
  22. #define xstrtou(rest) xstrtoul##rest
  23. #define xstrto(rest) xstrtol##rest
  24. #define xatou(rest) xatoul##rest
  25. #define xato(rest) xatol##rest
  26. #define XSTR_UTYPE_MAX ULONG_MAX
  27. #define XSTR_TYPE_MAX LONG_MAX
  28. #define XSTR_TYPE_MIN LONG_MIN
  29. #define XSTR_STRTOU strtoul
  30. #include "xatonum_template.c"
  31. #endif
  32. #if UINT_MAX != ULONG_MAX
  33. static ALWAYS_INLINE
  34. unsigned bb_strtoui(const char *str, char **end, int b)
  35. {
  36. unsigned long v = strtoul(str, end, b);
  37. if (v > UINT_MAX) {
  38. errno = ERANGE;
  39. return UINT_MAX;
  40. }
  41. return v;
  42. }
  43. #define type int
  44. #define xstrtou(rest) xstrtou##rest
  45. #define xstrto(rest) xstrtoi##rest
  46. #define xatou(rest) xatou##rest
  47. #define xato(rest) xatoi##rest
  48. #define XSTR_UTYPE_MAX UINT_MAX
  49. #define XSTR_TYPE_MAX INT_MAX
  50. #define XSTR_TYPE_MIN INT_MIN
  51. /* libc has no strtoui, so we need to create/use our own */
  52. #define XSTR_STRTOU bb_strtoui
  53. #include "xatonum_template.c"
  54. #endif
  55. /* A few special cases */
  56. int xatoi_u(const char *numstr)
  57. {
  58. return xatou_range(numstr, 0, INT_MAX);
  59. }
  60. uint16_t xatou16(const char *numstr)
  61. {
  62. return xatou_range(numstr, 0, 0xffff);
  63. }