3
0

xgetularg.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
  31. unsigned long lower,
  32. unsigned long upper,
  33. const struct suffix_mult *suffixes)
  34. {
  35. unsigned long r;
  36. int old_errno;
  37. char *e;
  38. assert(arg);
  39. /* Disallow '-' and any leading whitespace. Speed isn't critical here
  40. * since we're parsing commandline args. So make sure we get the
  41. * actual isspace function rather than a larger macro implementaion. */
  42. if ((*arg == '-') || (isspace)(*arg)) {
  43. bb_show_usage();
  44. }
  45. /* Since this is a lib function, we're not allowed to reset errno to 0.
  46. * Doing so could break an app that is deferring checking of errno.
  47. * So, save the old value so that we can restore it if successful. */
  48. old_errno = errno;
  49. errno = 0;
  50. r = strtoul(arg, &e, base);
  51. /* Do the initial validity check. Note: The standards do not
  52. * guarantee that errno is set if no digits were found. So we
  53. * must test for this explicitly. */
  54. if (errno || (arg == e)) { /* error or no digits */
  55. bb_show_usage();
  56. }
  57. errno = old_errno; /* Ok. So restore errno. */
  58. /* Do optional suffix parsing. Allow 'empty' suffix tables.
  59. * Note that we also all nul suffixes with associated multipliers,
  60. * to allow for scaling of the arg by some default multiplier. */
  61. if (suffixes) {
  62. while (suffixes->suffix) {
  63. if (strcmp(suffixes->suffix, e) == 0) {
  64. if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
  65. bb_show_usage();
  66. }
  67. ++e;
  68. r *= suffixes->mult;
  69. break;
  70. }
  71. ++suffixes;
  72. }
  73. }
  74. /* Finally, check for illegal trailing chars and range limits. */
  75. /* Note: although we allow leading space (via stroul), trailing space
  76. * is an error. It would be easy enough to allow though if desired. */
  77. if (*e || (r < lower) || (r > upper)) {
  78. bb_show_usage();
  79. }
  80. return r;
  81. }
  82. #endif
  83. #ifdef L_xgetlarg_bnd_sfx
  84. long bb_xgetlarg_bnd_sfx(const char *arg, int base,
  85. long lower,
  86. long upper,
  87. const struct suffix_mult *suffixes)
  88. {
  89. unsigned long u = LONG_MAX;
  90. long r;
  91. const char *p = arg;
  92. if ((*p == '-') && (p[1] != '+')) {
  93. ++p;
  94. #if LONG_MAX == (-(LONG_MIN + 1))
  95. ++u; /* two's complement */
  96. #endif
  97. }
  98. r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
  99. if (*arg == '-') {
  100. r = -r;
  101. }
  102. if ((r < lower) || (r > upper)) {
  103. bb_show_usage();
  104. }
  105. return r;
  106. }
  107. #endif
  108. #ifdef L_getlarg10_sfx
  109. long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
  110. {
  111. return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
  112. }
  113. #endif
  114. #ifdef L_xgetularg_bnd
  115. unsigned long bb_xgetularg_bnd(const char *arg, int base,
  116. unsigned long lower,
  117. unsigned long upper)
  118. {
  119. return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
  120. }
  121. #endif
  122. #ifdef L_xgetularg10_bnd
  123. unsigned long bb_xgetularg10_bnd(const char *arg,
  124. unsigned long lower,
  125. unsigned long upper)
  126. {
  127. return bb_xgetularg_bnd(arg, 10, lower, upper);
  128. }
  129. #endif
  130. #ifdef L_xgetularg10
  131. unsigned long bb_xgetularg10(const char *arg)
  132. {
  133. return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
  134. }
  135. #endif