xstrtol.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-2016
  3. Free Software Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by Jim Meyering. */
  15. #include <config.h>
  16. /* Some pre-ANSI implementations (e.g. SunOS 4)
  17. need stderr defined if assertion checking is enabled. */
  18. #include <stdio.h>
  19. #ifdef HAVE_ASSERT_H
  20. #include <assert.h>
  21. #endif
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <inttypes.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "lib/strutil.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. static strtol_error_t
  37. bkm_scale (uintmax_t * x, int scale_factor)
  38. {
  39. if (UINTMAX_MAX / scale_factor < *x)
  40. {
  41. *x = UINTMAX_MAX;
  42. return LONGINT_OVERFLOW;
  43. }
  44. *x *= scale_factor;
  45. return LONGINT_OK;
  46. }
  47. /* --------------------------------------------------------------------------------------------- */
  48. static strtol_error_t
  49. bkm_scale_by_power (uintmax_t * x, int base, int power)
  50. {
  51. strtol_error_t err = LONGINT_OK;
  52. while (power-- != 0)
  53. err |= bkm_scale (x, base);
  54. return err;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /*** public functions ****************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. strtol_error_t
  60. xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *valid_suffixes)
  61. {
  62. char *t_ptr;
  63. char **p;
  64. uintmax_t tmp;
  65. strtol_error_t err = LONGINT_OK;
  66. #ifdef HAVE_ASSERT_H
  67. assert (0 <= base && base <= 36);
  68. #endif
  69. p = (ptr != NULL ? ptr : &t_ptr);
  70. {
  71. const char *q = s;
  72. unsigned char ch = *q;
  73. while (isspace (ch))
  74. ch = *++q;
  75. if (ch == '-')
  76. return LONGINT_INVALID;
  77. }
  78. errno = 0;
  79. tmp = strtol (s, p, base);
  80. if (*p == s)
  81. {
  82. /* If there is no number but there is a valid suffix, assume the
  83. number is 1. The string is invalid otherwise. */
  84. if (valid_suffixes != NULL && **p != '\0' && strchr (valid_suffixes, **p) != NULL)
  85. tmp = 1;
  86. else
  87. return LONGINT_INVALID;
  88. }
  89. else if (errno != 0)
  90. {
  91. if (errno != ERANGE)
  92. return LONGINT_INVALID;
  93. err = LONGINT_OVERFLOW;
  94. }
  95. /* Let valid_suffixes == NULL mean "allow any suffix". */
  96. /* FIXME: update all callers except the ones that allow suffixes
  97. after the number, changing last parameter NULL to "". */
  98. if (valid_suffixes == NULL)
  99. {
  100. *val = tmp;
  101. return err;
  102. }
  103. if (**p != '\0')
  104. {
  105. int suffixes = 1;
  106. strtol_error_t overflow;
  107. if (strchr (valid_suffixes, **p) == NULL)
  108. {
  109. *val = tmp;
  110. return err | LONGINT_INVALID_SUFFIX_CHAR;
  111. }
  112. base = 1024;
  113. if (strchr (valid_suffixes, '0') != NULL)
  114. {
  115. /* The "valid suffix" '0' is a special flag meaning that
  116. an optional second suffix is allowed, which can change
  117. the base. A suffix "B" (e.g. "100MB") stands for a power
  118. of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
  119. a power of 1024. If no suffix (e.g. "100M"), assume
  120. power-of-1024. */
  121. switch (p[0][1])
  122. {
  123. case 'i':
  124. if (p[0][2] == 'B')
  125. suffixes += 2;
  126. break;
  127. case 'B':
  128. case 'D': /* 'D' is obsolescent */
  129. base = 1000;
  130. suffixes++;
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. switch (**p)
  137. {
  138. case 'b':
  139. overflow = bkm_scale (&tmp, 512);
  140. break;
  141. case 'B':
  142. overflow = bkm_scale (&tmp, 1024);
  143. break;
  144. case 'c':
  145. overflow = LONGINT_OK;
  146. break;
  147. case 'E': /* exa or exbi */
  148. overflow = bkm_scale_by_power (&tmp, base, 6);
  149. break;
  150. case 'G': /* giga or gibi */
  151. case 'g': /* 'g' is undocumented; for compatibility only */
  152. overflow = bkm_scale_by_power (&tmp, base, 3);
  153. break;
  154. case 'k': /* kilo */
  155. case 'K': /* kibi */
  156. overflow = bkm_scale_by_power (&tmp, base, 1);
  157. break;
  158. case 'M': /* mega or mebi */
  159. case 'm': /* 'm' is undocumented; for compatibility only */
  160. overflow = bkm_scale_by_power (&tmp, base, 2);
  161. break;
  162. case 'P': /* peta or pebi */
  163. overflow = bkm_scale_by_power (&tmp, base, 5);
  164. break;
  165. case 'T': /* tera or tebi */
  166. case 't': /* 't' is undocumented; for compatibility only */
  167. overflow = bkm_scale_by_power (&tmp, base, 4);
  168. break;
  169. case 'w':
  170. overflow = bkm_scale (&tmp, 2);
  171. break;
  172. case 'Y': /* yotta or 2**80 */
  173. overflow = bkm_scale_by_power (&tmp, base, 8);
  174. break;
  175. case 'Z': /* zetta or 2**70 */
  176. overflow = bkm_scale_by_power (&tmp, base, 7);
  177. break;
  178. default:
  179. *val = tmp;
  180. return err | LONGINT_INVALID_SUFFIX_CHAR;
  181. }
  182. err |= overflow;
  183. *p += suffixes;
  184. if (**p != '\0')
  185. err |= LONGINT_INVALID_SUFFIX_CHAR;
  186. }
  187. *val = tmp;
  188. return err;
  189. }
  190. /* --------------------------------------------------------------------------------------------- */