xstrtol.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-2024
  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. #include <ctype.h>
  20. #include <errno.h>
  21. #include <inttypes.h>
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "lib/strutil.h"
  26. /*** global variables ****************************************************************************/
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** forward declarations (file scope functions) *************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. /*** file scope functions ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. static strtol_error_t
  35. bkm_scale (uintmax_t *x, int scale_factor)
  36. {
  37. if (UINTMAX_MAX / scale_factor < *x)
  38. {
  39. *x = UINTMAX_MAX;
  40. return LONGINT_OVERFLOW;
  41. }
  42. *x *= scale_factor;
  43. return LONGINT_OK;
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. static strtol_error_t
  47. bkm_scale_by_power (uintmax_t *x, int base, int power)
  48. {
  49. strtol_error_t err = LONGINT_OK;
  50. while (power-- != 0)
  51. err |= bkm_scale (x, base);
  52. return err;
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /*** public functions ****************************************************************************/
  56. /* --------------------------------------------------------------------------------------------- */
  57. strtol_error_t
  58. xstrtoumax (const char *s, char **ptr, int base, uintmax_t *val, const char *valid_suffixes)
  59. {
  60. char *t_ptr;
  61. char **p;
  62. uintmax_t tmp;
  63. strtol_error_t err = LONGINT_OK;
  64. g_assert (0 <= base && base <= 36);
  65. p = (ptr != NULL ? ptr : &t_ptr);
  66. {
  67. const char *q = s;
  68. unsigned char ch = *q;
  69. while (isspace (ch))
  70. ch = *++q;
  71. if (ch == '-')
  72. return LONGINT_INVALID;
  73. }
  74. errno = 0;
  75. tmp = strtol (s, p, base);
  76. if (*p == s)
  77. {
  78. /* If there is no number but there is a valid suffix, assume the
  79. number is 1. The string is invalid otherwise. */
  80. if (valid_suffixes != NULL && **p != '\0' && strchr (valid_suffixes, **p) != NULL)
  81. tmp = 1;
  82. else
  83. return LONGINT_INVALID;
  84. }
  85. else if (errno != 0)
  86. {
  87. if (errno != ERANGE)
  88. return LONGINT_INVALID;
  89. err = LONGINT_OVERFLOW;
  90. }
  91. /* Let valid_suffixes == NULL mean "allow any suffix". */
  92. /* FIXME: update all callers except the ones that allow suffixes
  93. after the number, changing last parameter NULL to "". */
  94. if (valid_suffixes == NULL)
  95. {
  96. *val = tmp;
  97. return err;
  98. }
  99. if (**p != '\0')
  100. {
  101. int suffixes = 1;
  102. strtol_error_t overflow;
  103. if (strchr (valid_suffixes, **p) == NULL)
  104. {
  105. *val = tmp;
  106. return err | LONGINT_INVALID_SUFFIX_CHAR;
  107. }
  108. base = 1024;
  109. switch (**p)
  110. {
  111. case 'E':
  112. case 'G':
  113. case 'g':
  114. case 'k':
  115. case 'K':
  116. case 'M':
  117. case 'm':
  118. case 'P':
  119. case 'Q':
  120. case 'R':
  121. case 'T':
  122. case 't':
  123. case 'Y':
  124. case 'Z':
  125. if (strchr (valid_suffixes, '0') != NULL)
  126. {
  127. /* The "valid suffix" '0' is a special flag meaning that
  128. an optional second suffix is allowed, which can change
  129. the base. A suffix "B" (e.g. "100MB") stands for a power
  130. of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
  131. a power of 1024. If no suffix (e.g. "100M"), assume
  132. power-of-1024. */
  133. switch (p[0][1])
  134. {
  135. case 'i':
  136. if (p[0][2] == 'B')
  137. suffixes += 2;
  138. break;
  139. case 'B':
  140. case 'D': /* 'D' is obsolescent */
  141. base = 1000;
  142. suffixes++;
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. switch (**p)
  153. {
  154. case 'b':
  155. overflow = bkm_scale (&tmp, 512);
  156. break;
  157. case 'B':
  158. /* This obsolescent first suffix is distinct from the 'B'
  159. second suffix above. E.g., 'tar -L 1000B' means change
  160. the tape after writing 1000 KiB of data. */
  161. overflow = bkm_scale (&tmp, 1024);
  162. break;
  163. case 'c':
  164. overflow = LONGINT_OK;
  165. break;
  166. case 'E': /* exa or exbi */
  167. overflow = bkm_scale_by_power (&tmp, base, 6);
  168. break;
  169. case 'G': /* giga or gibi */
  170. case 'g': /* 'g' is undocumented; for compatibility only */
  171. overflow = bkm_scale_by_power (&tmp, base, 3);
  172. break;
  173. case 'k': /* kilo */
  174. case 'K': /* kibi */
  175. overflow = bkm_scale_by_power (&tmp, base, 1);
  176. break;
  177. case 'M': /* mega or mebi */
  178. case 'm': /* 'm' is undocumented; for compatibility only */
  179. overflow = bkm_scale_by_power (&tmp, base, 2);
  180. break;
  181. case 'P': /* peta or pebi */
  182. overflow = bkm_scale_by_power (&tmp, base, 5);
  183. break;
  184. case 'Q': /* quetta or 2**100 */
  185. overflow = bkm_scale_by_power (&tmp, base, 10);
  186. break;
  187. case 'R': /* ronna or 2**90 */
  188. overflow = bkm_scale_by_power (&tmp, base, 9);
  189. break;
  190. case 'T': /* tera or tebi */
  191. case 't': /* 't' is undocumented; for compatibility only */
  192. overflow = bkm_scale_by_power (&tmp, base, 4);
  193. break;
  194. case 'w':
  195. overflow = bkm_scale (&tmp, 2);
  196. break;
  197. case 'Y': /* yotta or 2**80 */
  198. overflow = bkm_scale_by_power (&tmp, base, 8);
  199. break;
  200. case 'Z': /* zetta or 2**70 */
  201. overflow = bkm_scale_by_power (&tmp, base, 7);
  202. break;
  203. default:
  204. *val = tmp;
  205. return err | LONGINT_INVALID_SUFFIX_CHAR;
  206. }
  207. err |= overflow;
  208. *p += suffixes;
  209. if (**p != '\0')
  210. err |= LONGINT_INVALID_SUFFIX_CHAR;
  211. }
  212. *val = tmp;
  213. return err;
  214. }
  215. /* --------------------------------------------------------------------------------------------- */