xatonum_template.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. You need to define the following (example):
  3. #define type long
  4. #define xstrtou(rest) xstrtoul##rest
  5. #define xstrto(rest) xstrtol##rest
  6. #define xatou(rest) xatoul##rest
  7. #define xato(rest) xatol##rest
  8. #define XSTR_UTYPE_MAX ULONG_MAX
  9. #define XSTR_TYPE_MAX LONG_MAX
  10. #define XSTR_TYPE_MIN LONG_MIN
  11. #define XSTR_STRTOU strtoul
  12. */
  13. unsigned type FAST_FUNC xstrtou(_range_sfx)(const char *numstr, int base,
  14. unsigned type lower,
  15. unsigned type upper,
  16. const struct suffix_mult *suffixes)
  17. {
  18. unsigned type r;
  19. int old_errno;
  20. char *e;
  21. /* Disallow '-' and any leading whitespace. Make sure we get the
  22. * actual isspace function rather than a macro implementaion. */
  23. if (*numstr == '-' || *numstr == '+' || (isspace)(*numstr))
  24. goto inval;
  25. /* Since this is a lib function, we're not allowed to reset errno to 0.
  26. * Doing so could break an app that is deferring checking of errno.
  27. * So, save the old value so that we can restore it if successful. */
  28. old_errno = errno;
  29. errno = 0;
  30. r = XSTR_STRTOU(numstr, &e, base);
  31. /* Do the initial validity check. Note: The standards do not
  32. * guarantee that errno is set if no digits were found. So we
  33. * must test for this explicitly. */
  34. if (errno || numstr == e)
  35. goto inval; /* error / no digits / illegal trailing chars */
  36. errno = old_errno; /* Ok. So restore errno. */
  37. /* Do optional suffix parsing. Allow 'empty' suffix tables.
  38. * Note that we also allow nul suffixes with associated multipliers,
  39. * to allow for scaling of the numstr by some default multiplier. */
  40. if (suffixes) {
  41. while (suffixes->mult) {
  42. if (strcmp(suffixes->suffix, e) == 0) {
  43. if (XSTR_UTYPE_MAX / suffixes->mult < r)
  44. goto range; /* overflow! */
  45. r *= suffixes->mult;
  46. goto chk_range;
  47. }
  48. ++suffixes;
  49. }
  50. }
  51. /* Note: trailing space is an error.
  52. It would be easy enough to allow though if desired. */
  53. if (*e)
  54. goto inval;
  55. chk_range:
  56. /* Finally, check for range limits. */
  57. if (r >= lower && r <= upper)
  58. return r;
  59. range:
  60. bb_error_msg_and_die("number %s is not in %llu..%llu range",
  61. numstr, (unsigned long long)lower,
  62. (unsigned long long)upper);
  63. inval:
  64. bb_error_msg_and_die("invalid number '%s'", numstr);
  65. }
  66. unsigned type FAST_FUNC xstrtou(_range)(const char *numstr, int base,
  67. unsigned type lower,
  68. unsigned type upper)
  69. {
  70. return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
  71. }
  72. unsigned type FAST_FUNC xstrtou(_sfx)(const char *numstr, int base,
  73. const struct suffix_mult *suffixes)
  74. {
  75. return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
  76. }
  77. unsigned type FAST_FUNC xstrtou()(const char *numstr, int base)
  78. {
  79. return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
  80. }
  81. unsigned type FAST_FUNC xatou(_range_sfx)(const char *numstr,
  82. unsigned type lower,
  83. unsigned type upper,
  84. const struct suffix_mult *suffixes)
  85. {
  86. return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
  87. }
  88. unsigned type FAST_FUNC xatou(_range)(const char *numstr,
  89. unsigned type lower,
  90. unsigned type upper)
  91. {
  92. return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
  93. }
  94. unsigned type FAST_FUNC xatou(_sfx)(const char *numstr,
  95. const struct suffix_mult *suffixes)
  96. {
  97. return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
  98. }
  99. unsigned type FAST_FUNC xatou()(const char *numstr)
  100. {
  101. return xatou(_sfx)(numstr, NULL);
  102. }
  103. /* Signed ones */
  104. type FAST_FUNC xstrto(_range_sfx)(const char *numstr, int base,
  105. type lower,
  106. type upper,
  107. const struct suffix_mult *suffixes)
  108. {
  109. unsigned type u = XSTR_TYPE_MAX;
  110. type r;
  111. const char *p = numstr;
  112. /* NB: if you'll decide to disallow '+':
  113. * at least renice applet needs to allow it */
  114. if (p[0] == '+' || p[0] == '-') {
  115. ++p;
  116. if (p[0] == '-')
  117. ++u; /* = <type>_MIN (01111... + 1 == 10000...) */
  118. }
  119. r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
  120. if (*numstr == '-') {
  121. r = -r;
  122. }
  123. if (r < lower || r > upper) {
  124. bb_error_msg_and_die("number %s is not in %lld..%lld range",
  125. numstr, (long long)lower, (long long)upper);
  126. }
  127. return r;
  128. }
  129. type FAST_FUNC xstrto(_range)(const char *numstr, int base, type lower, type upper)
  130. {
  131. return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
  132. }
  133. type FAST_FUNC xato(_range_sfx)(const char *numstr,
  134. type lower,
  135. type upper,
  136. const struct suffix_mult *suffixes)
  137. {
  138. return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
  139. }
  140. type FAST_FUNC xato(_range)(const char *numstr, type lower, type upper)
  141. {
  142. return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
  143. }
  144. type FAST_FUNC xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
  145. {
  146. return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
  147. }
  148. type FAST_FUNC xato()(const char *numstr)
  149. {
  150. return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
  151. }
  152. #undef type
  153. #undef xstrtou
  154. #undef xstrto
  155. #undef xatou
  156. #undef xato
  157. #undef XSTR_UTYPE_MAX
  158. #undef XSTR_TYPE_MAX
  159. #undef XSTR_TYPE_MIN
  160. #undef XSTR_STRTOU