xstrtol.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-2015
  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. }
  133. }
  134. switch (**p)
  135. {
  136. case 'b':
  137. overflow = bkm_scale (&tmp, 512);
  138. break;
  139. case 'B':
  140. overflow = bkm_scale (&tmp, 1024);
  141. break;
  142. case 'c':
  143. overflow = LONGINT_OK;
  144. break;
  145. case 'E': /* exa or exbi */
  146. overflow = bkm_scale_by_power (&tmp, base, 6);
  147. break;
  148. case 'G': /* giga or gibi */
  149. case 'g': /* 'g' is undocumented; for compatibility only */
  150. overflow = bkm_scale_by_power (&tmp, base, 3);
  151. break;
  152. case 'k': /* kilo */
  153. case 'K': /* kibi */
  154. overflow = bkm_scale_by_power (&tmp, base, 1);
  155. break;
  156. case 'M': /* mega or mebi */
  157. case 'm': /* 'm' is undocumented; for compatibility only */
  158. overflow = bkm_scale_by_power (&tmp, base, 2);
  159. break;
  160. case 'P': /* peta or pebi */
  161. overflow = bkm_scale_by_power (&tmp, base, 5);
  162. break;
  163. case 'T': /* tera or tebi */
  164. case 't': /* 't' is undocumented; for compatibility only */
  165. overflow = bkm_scale_by_power (&tmp, base, 4);
  166. break;
  167. case 'w':
  168. overflow = bkm_scale (&tmp, 2);
  169. break;
  170. case 'Y': /* yotta or 2**80 */
  171. overflow = bkm_scale_by_power (&tmp, base, 8);
  172. break;
  173. case 'Z': /* zetta or 2**70 */
  174. overflow = bkm_scale_by_power (&tmp, base, 7);
  175. break;
  176. default:
  177. *val = tmp;
  178. return err | LONGINT_INVALID_SUFFIX_CHAR;
  179. }
  180. err |= overflow;
  181. *p += suffixes;
  182. if (**p != '\0')
  183. err |= LONGINT_INVALID_SUFFIX_CHAR;
  184. }
  185. *val = tmp;
  186. return err;
  187. }
  188. /* --------------------------------------------------------------------------------------------- */