bb_strtonum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* On exit: errno = 0 only if there was non-empty, '\0' terminated value
  11. * errno = EINVAL if value was not '\0' terminated, but otherwise ok
  12. * Return value is still valid, caller should just check whether end[0]
  13. * is a valid terminating char for particular case. OTOH, if caller
  14. * requires '\0' terminated input, [s]he can just check errno == 0.
  15. * errno = ERANGE if value had alphanumeric terminating char ("1234abcg").
  16. * errno = ERANGE if value is out of range, missing, etc.
  17. * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok )
  18. * return value is all-ones in this case.
  19. *
  20. * Test code:
  21. * char *endptr;
  22. * const char *minus = "-";
  23. * errno = 0;
  24. * bb_strtoi(minus, &endptr, 0); // must set ERANGE
  25. * printf("minus:%p endptr:%p errno:%d EINVAL:%d\n", minus, endptr, errno, EINVAL);
  26. * errno = 0;
  27. * bb_strtoi("-0-", &endptr, 0); // must set EINVAL and point to second '-'
  28. * printf("endptr[0]:%c errno:%d EINVAL:%d\n", endptr[0], errno, EINVAL);
  29. */
  30. static unsigned long long ret_ERANGE(void)
  31. {
  32. errno = ERANGE; /* this ain't as small as it looks (on glibc) */
  33. return ULLONG_MAX;
  34. }
  35. static unsigned long long handle_errors(unsigned long long v, char **endp)
  36. {
  37. char next_ch = **endp;
  38. /* errno is already set to ERANGE by strtoXXX if value overflowed */
  39. if (next_ch) {
  40. /* "1234abcg" or out-of-range? */
  41. if (isalnum(next_ch) || errno)
  42. return ret_ERANGE();
  43. /* good number, just suspicious terminator */
  44. errno = EINVAL;
  45. }
  46. return v;
  47. }
  48. unsigned long long FAST_FUNC bb_strtoull(const char *arg, char **endp, int base)
  49. {
  50. unsigned long long v;
  51. char *endptr;
  52. if (!endp) endp = &endptr;
  53. *endp = (char*) arg;
  54. /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */
  55. /* I don't think that this is right. Preventing this... */
  56. if (!isalnum(arg[0])) return ret_ERANGE();
  57. /* not 100% correct for lib func, but convenient for the caller */
  58. errno = 0;
  59. v = strtoull(arg, endp, base);
  60. return handle_errors(v, endp);
  61. }
  62. long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base)
  63. {
  64. unsigned long long v;
  65. char *endptr;
  66. char first;
  67. if (!endp) endp = &endptr;
  68. *endp = (char*) arg;
  69. /* Check for the weird "feature":
  70. * a "-" string is apparently a valid "number" for strto[u]l[l]!
  71. * It returns zero and errno is 0! :( */
  72. first = (arg[0] != '-' ? arg[0] : arg[1]);
  73. if (!isalnum(first)) return ret_ERANGE();
  74. errno = 0;
  75. v = strtoll(arg, endp, base);
  76. return handle_errors(v, endp);
  77. }
  78. #if ULONG_MAX != ULLONG_MAX
  79. unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base)
  80. {
  81. unsigned long v;
  82. char *endptr;
  83. if (!endp) endp = &endptr;
  84. *endp = (char*) arg;
  85. if (!isalnum(arg[0])) return ret_ERANGE();
  86. errno = 0;
  87. v = strtoul(arg, endp, base);
  88. return handle_errors(v, endp);
  89. }
  90. long FAST_FUNC bb_strtol(const char *arg, char **endp, int base)
  91. {
  92. long v;
  93. char *endptr;
  94. char first;
  95. if (!endp) endp = &endptr;
  96. *endp = (char*) arg;
  97. first = (arg[0] != '-' ? arg[0] : arg[1]);
  98. if (!isalnum(first)) return ret_ERANGE();
  99. errno = 0;
  100. v = strtol(arg, endp, base);
  101. return handle_errors(v, endp);
  102. }
  103. #endif
  104. #if UINT_MAX != ULONG_MAX
  105. unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base)
  106. {
  107. unsigned long v;
  108. char *endptr;
  109. if (!endp) endp = &endptr;
  110. *endp = (char*) arg;
  111. if (!isalnum(arg[0])) return ret_ERANGE();
  112. errno = 0;
  113. v = strtoul(arg, endp, base);
  114. if (v > UINT_MAX) return ret_ERANGE();
  115. return handle_errors(v, endp);
  116. }
  117. int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
  118. {
  119. long v;
  120. char *endptr;
  121. char first;
  122. if (!endp) endp = &endptr;
  123. *endp = (char*) arg;
  124. first = (arg[0] != '-' ? arg[0] : arg[1]);
  125. if (!isalnum(first)) return ret_ERANGE();
  126. errno = 0;
  127. v = strtol(arg, endp, base);
  128. if (v > INT_MAX) return ret_ERANGE();
  129. if (v < INT_MIN) return ret_ERANGE();
  130. return handle_errors(v, endp);
  131. }
  132. #endif