4
0

xstrtol.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-1996, 1998-2001, 2003-2007, 2009-2015 Free Software
  3. 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. #ifndef __strtol
  16. # define __strtol strtol
  17. # define __strtol_t long int
  18. # define __xstrtol xstrtol
  19. # define STRTOL_T_MINIMUM LONG_MIN
  20. # define STRTOL_T_MAXIMUM LONG_MAX
  21. #endif
  22. #include <config.h>
  23. #include "xstrtol.h"
  24. /* Some pre-ANSI implementations (e.g. SunOS 4)
  25. need stderr defined if assertion checking is enabled. */
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <limits.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "assure.h"
  33. #include "intprops.h"
  34. /* xstrtoll.c and xstrtoull.c, which include this file, require that
  35. ULLONG_MAX, LLONG_MAX, LLONG_MIN are defined, but <limits.h> does not
  36. define them on all platforms. */
  37. #ifndef ULLONG_MAX
  38. # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
  39. #endif
  40. #ifndef LLONG_MAX
  41. # define LLONG_MAX TYPE_MAXIMUM (long long int)
  42. #endif
  43. #ifndef LLONG_MIN
  44. # define LLONG_MIN TYPE_MINIMUM (long long int)
  45. #endif
  46. static strtol_error
  47. bkm_scale (__strtol_t *x, int scale_factor)
  48. {
  49. if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
  50. {
  51. *x = STRTOL_T_MINIMUM;
  52. return LONGINT_OVERFLOW;
  53. }
  54. if (STRTOL_T_MAXIMUM / scale_factor < *x)
  55. {
  56. *x = STRTOL_T_MAXIMUM;
  57. return LONGINT_OVERFLOW;
  58. }
  59. *x *= scale_factor;
  60. return LONGINT_OK;
  61. }
  62. static strtol_error
  63. bkm_scale_by_power (__strtol_t *x, int base, int power)
  64. {
  65. strtol_error err = LONGINT_OK;
  66. while (power--)
  67. err |= bkm_scale (x, base);
  68. return err;
  69. }
  70. /* FIXME: comment. */
  71. strtol_error
  72. __xstrtol (const char *s, char **ptr, int strtol_base,
  73. __strtol_t *val, const char *valid_suffixes)
  74. {
  75. char *t_ptr;
  76. char **p;
  77. __strtol_t tmp;
  78. strtol_error err = LONGINT_OK;
  79. assure (0 <= strtol_base && strtol_base <= 36);
  80. p = (ptr ? ptr : &t_ptr);
  81. errno = 0;
  82. if (! TYPE_SIGNED (__strtol_t))
  83. {
  84. const char *q = s;
  85. unsigned char ch = *q;
  86. while (isspace (ch))
  87. ch = *++q;
  88. if (ch == '-')
  89. return LONGINT_INVALID;
  90. }
  91. tmp = __strtol (s, p, strtol_base);
  92. if (*p == s)
  93. {
  94. /* If there is no number but there is a valid suffix, assume the
  95. number is 1. The string is invalid otherwise. */
  96. if (valid_suffixes && **p && strchr (valid_suffixes, **p))
  97. tmp = 1;
  98. else
  99. return LONGINT_INVALID;
  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)
  111. {
  112. *val = tmp;
  113. return err;
  114. }
  115. if (**p != '\0')
  116. {
  117. int base = 1024;
  118. int suffixes = 1;
  119. strtol_error overflow;
  120. if (!strchr (valid_suffixes, **p))
  121. {
  122. *val = tmp;
  123. return err | LONGINT_INVALID_SUFFIX_CHAR;
  124. }
  125. if (strchr (valid_suffixes, '0'))
  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. }
  145. }
  146. switch (**p)
  147. {
  148. case 'b':
  149. overflow = bkm_scale (&tmp, 512);
  150. break;
  151. case 'B':
  152. overflow = bkm_scale (&tmp, 1024);
  153. break;
  154. case 'c':
  155. overflow = LONGINT_OK;
  156. break;
  157. case 'E': /* exa or exbi */
  158. overflow = bkm_scale_by_power (&tmp, base, 6);
  159. break;
  160. case 'G': /* giga or gibi */
  161. case 'g': /* 'g' is undocumented; for compatibility only */
  162. overflow = bkm_scale_by_power (&tmp, base, 3);
  163. break;
  164. case 'k': /* kilo */
  165. case 'K': /* kibi */
  166. overflow = bkm_scale_by_power (&tmp, base, 1);
  167. break;
  168. case 'M': /* mega or mebi */
  169. case 'm': /* 'm' is undocumented; for compatibility only */
  170. overflow = bkm_scale_by_power (&tmp, base, 2);
  171. break;
  172. case 'P': /* peta or pebi */
  173. overflow = bkm_scale_by_power (&tmp, base, 5);
  174. break;
  175. case 'T': /* tera or tebi */
  176. case 't': /* 't' is undocumented; for compatibility only */
  177. overflow = bkm_scale_by_power (&tmp, base, 4);
  178. break;
  179. case 'w':
  180. overflow = bkm_scale (&tmp, 2);
  181. break;
  182. case 'Y': /* yotta or 2**80 */
  183. overflow = bkm_scale_by_power (&tmp, base, 8);
  184. break;
  185. case 'Z': /* zetta or 2**70 */
  186. overflow = bkm_scale_by_power (&tmp, base, 7);
  187. break;
  188. default:
  189. *val = tmp;
  190. return err | LONGINT_INVALID_SUFFIX_CHAR;
  191. }
  192. err |= overflow;
  193. *p += suffixes;
  194. if (**p)
  195. err |= LONGINT_INVALID_SUFFIX_CHAR;
  196. }
  197. *val = tmp;
  198. return err;
  199. }