3
0

xatonum.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. extern inline unsigned bb_strtoui(const char *str, char **end, int b)
  34. {
  35. unsigned long v = strtoul(str, end, b);
  36. if (v > UINT_MAX) {
  37. errno = ERANGE;
  38. return UINT_MAX;
  39. }
  40. return v;
  41. }
  42. #define type int
  43. #define xstrtou(rest) xstrtou##rest
  44. #define xstrto(rest) xstrtoi##rest
  45. #define xatou(rest) xatou##rest
  46. #define xato(rest) xatoi##rest
  47. #define XSTR_UTYPE_MAX UINT_MAX
  48. #define XSTR_TYPE_MAX INT_MAX
  49. #define XSTR_TYPE_MIN INT_MIN
  50. /* libc has no strtoui, so we need to create/use our own */
  51. #define XSTR_STRTOU bb_strtoui
  52. #include "xatonum_template.c"
  53. #endif
  54. /* A few special cases */
  55. int xatoi_u(const char *numstr)
  56. {
  57. return xatou_range(numstr, 0, INT_MAX);
  58. }
  59. uint16_t xatou16(const char *numstr)
  60. {
  61. return xatou_range(numstr, 0, 0xffff);
  62. }