xstrtol.c 8.8 KB

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