xgetularg.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * xgetularg* implementations for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <limits.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include "libbb.h"
  29. #ifdef L_xgetularg_bnd_sfx
  30. extern
  31. unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
  32. unsigned long lower,
  33. unsigned long upper,
  34. const struct suffix_mult *suffixes)
  35. {
  36. unsigned long r;
  37. int old_errno;
  38. char *e;
  39. assert(arg);
  40. /* Disallow '-' and any leading whitespace. Speed isn't critical here
  41. * since we're parsing commandline args. So make sure we get the
  42. * actual isspace function rather than a larger macro implementaion. */
  43. if ((*arg == '-') || (isspace)(*arg)) {
  44. bb_show_usage();
  45. }
  46. /* Since this is a lib function, we're not allowed to reset errno to 0.
  47. * Doing so could break an app that is deferring checking of errno.
  48. * So, save the old value so that we can restore it if successful. */
  49. old_errno = errno;
  50. errno = 0;
  51. r = strtoul(arg, &e, base);
  52. /* Do the initial validity check. Note: The standards do not
  53. * guarantee that errno is set if no digits were found. So we
  54. * must test for this explicitly. */
  55. if (errno || (arg == e)) { /* error or no digits */
  56. bb_show_usage();
  57. }
  58. errno = old_errno; /* Ok. So restore errno. */
  59. /* Do optional suffix parsing. Allow 'empty' suffix tables.
  60. * Note that we also all nul suffixes with associated multipliers,
  61. * to allow for scaling of the arg by some default multiplier. */
  62. if (suffixes) {
  63. while (suffixes->suffix) {
  64. if (strcmp(suffixes->suffix, e) == 0) {
  65. if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
  66. bb_show_usage();
  67. }
  68. ++e;
  69. r *= suffixes->mult;
  70. break;
  71. }
  72. ++suffixes;
  73. }
  74. }
  75. /* Finally, check for illegal trailing chars and range limits. */
  76. /* Note: although we allow leading space (via stroul), trailing space
  77. * is an error. It would be easy enough to allow though if desired. */
  78. if (*e || (r < lower) || (r > upper)) {
  79. bb_show_usage();
  80. }
  81. return r;
  82. }
  83. #endif
  84. #ifdef L_xgetlarg_bnd_sfx
  85. extern
  86. long bb_xgetlarg_bnd_sfx(const char *arg, int base,
  87. long lower,
  88. long upper,
  89. const struct suffix_mult *suffixes)
  90. {
  91. unsigned long u = LONG_MAX;
  92. long r;
  93. const char *p = arg;
  94. if ((*p == '-') && (p[1] != '+')) {
  95. ++p;
  96. #if LONG_MAX == (-(LONG_MIN + 1))
  97. ++u; /* two's complement */
  98. #endif
  99. }
  100. r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
  101. if (*arg == '-') {
  102. r = -r;
  103. }
  104. if ((r < lower) || (r > upper)) {
  105. bb_show_usage();
  106. }
  107. return r;
  108. }
  109. #endif
  110. #ifdef L_getlarg10_sfx
  111. extern
  112. long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
  113. {
  114. return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
  115. }
  116. #endif
  117. #ifdef L_xgetularg_bnd
  118. extern
  119. unsigned long bb_xgetularg_bnd(const char *arg, int base,
  120. unsigned long lower,
  121. unsigned long upper)
  122. {
  123. return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
  124. }
  125. #endif
  126. #ifdef L_xgetularg10_bnd
  127. extern
  128. unsigned long bb_xgetularg10_bnd(const char *arg,
  129. unsigned long lower,
  130. unsigned long upper)
  131. {
  132. return bb_xgetularg_bnd(arg, 10, lower, upper);
  133. }
  134. #endif
  135. #ifdef L_xgetularg10
  136. extern
  137. unsigned long bb_xgetularg10(const char *arg)
  138. {
  139. return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
  140. }
  141. #endif