bb_strtonum.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, char *endptr)
  36. {
  37. if (endp) *endp = endptr;
  38. /* errno is already set to ERANGE by strtoXXX if value overflowed */
  39. if (endptr[0]) {
  40. /* "1234abcg" or out-of-range? */
  41. if (isalnum(endptr[0]) || 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. /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */
  53. /* I don't think that this is right. Preventing this... */
  54. if (!isalnum(arg[0])) return ret_ERANGE();
  55. /* not 100% correct for lib func, but convenient for the caller */
  56. errno = 0;
  57. v = strtoull(arg, &endptr, base);
  58. return handle_errors(v, endp, endptr);
  59. }
  60. long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base)
  61. {
  62. unsigned long long v;
  63. char *endptr;
  64. /* Check for the weird "feature":
  65. * a "-" string is apparently a valid "number" for strto[u]l[l]!
  66. * It returns zero and errno is 0! :( */
  67. char first = (arg[0] != '-' ? arg[0] : arg[1]);
  68. if (!isalnum(first)) return ret_ERANGE();
  69. errno = 0;
  70. v = strtoll(arg, &endptr, base);
  71. return handle_errors(v, endp, endptr);
  72. }
  73. #if ULONG_MAX != ULLONG_MAX
  74. unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base)
  75. {
  76. unsigned long v;
  77. char *endptr;
  78. if (!isalnum(arg[0])) return ret_ERANGE();
  79. errno = 0;
  80. v = strtoul(arg, &endptr, base);
  81. return handle_errors(v, endp, endptr);
  82. }
  83. long FAST_FUNC bb_strtol(const char *arg, char **endp, int base)
  84. {
  85. long v;
  86. char *endptr;
  87. char first = (arg[0] != '-' ? arg[0] : arg[1]);
  88. if (!isalnum(first)) return ret_ERANGE();
  89. errno = 0;
  90. v = strtol(arg, &endptr, base);
  91. return handle_errors(v, endp, endptr);
  92. }
  93. #endif
  94. #if UINT_MAX != ULONG_MAX
  95. unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base)
  96. {
  97. unsigned long v;
  98. char *endptr;
  99. if (!isalnum(arg[0])) return ret_ERANGE();
  100. errno = 0;
  101. v = strtoul(arg, &endptr, base);
  102. if (v > UINT_MAX) return ret_ERANGE();
  103. return handle_errors(v, endp, endptr);
  104. }
  105. int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
  106. {
  107. long v;
  108. char *endptr;
  109. char first = (arg[0] != '-' ? arg[0] : arg[1]);
  110. if (!isalnum(first)) return ret_ERANGE();
  111. errno = 0;
  112. v = strtol(arg, &endptr, base);
  113. if (v > INT_MAX) return ret_ERANGE();
  114. if (v < INT_MIN) return ret_ERANGE();
  115. return handle_errors(v, endp, endptr);
  116. }
  117. #endif