3
0

xatonum.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 source tree.
  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 FAST_FUNC xatoi_positive(const char *numstr)
  57. {
  58. return xatou_range(numstr, 0, INT_MAX);
  59. }
  60. uint16_t FAST_FUNC xatou16(const char *numstr)
  61. {
  62. return xatou_range(numstr, 0, 0xffff);
  63. }
  64. const struct suffix_mult bkm_suffixes[] = {
  65. { "b", 512 },
  66. { "k", 1024 },
  67. { "m", 1024*1024 },
  68. { "", 0 }
  69. };
  70. const struct suffix_mult cwbkMG_suffixes[] = {
  71. { "c", 1 },
  72. { "w", 2 },
  73. { "b", 512 },
  74. { "kB", 1000 },
  75. { "kD", 1000 },
  76. { "k", 1024 },
  77. { "KB", 1000 }, /* compat with coreutils dd */
  78. { "KD", 1000 }, /* compat with coreutils dd */
  79. { "K", 1024 }, /* compat with coreutils dd */
  80. { "MB", 1000000 },
  81. { "MD", 1000000 },
  82. { "M", 1024*1024 },
  83. { "GB", 1000000000 },
  84. { "GD", 1000000000 },
  85. { "G", 1024*1024*1024 },
  86. /* "D" suffix for decimal is not in coreutils manpage, looks like it's deprecated */
  87. /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */
  88. { "", 0 }
  89. };
  90. const struct suffix_mult kmg_i_suffixes[] = {
  91. { "KiB", 1024 },
  92. { "kiB", 1024 },
  93. { "K", 1024 },
  94. { "k", 1024 },
  95. { "MiB", 1048576 },
  96. { "miB", 1048576 },
  97. { "M", 1048576 },
  98. { "m", 1048576 },
  99. { "GiB", 1073741824 },
  100. { "giB", 1073741824 },
  101. { "G", 1073741824 },
  102. { "g", 1073741824 },
  103. { "KB", 1000 },
  104. { "MB", 1000000 },
  105. { "GB", 1000000000 },
  106. { "", 0 }
  107. };