4
0

xstrtol.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995, 1996, 1998-2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Jim Meyering. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #ifndef __strtol
  19. # define __strtol strtol
  20. # define __strtol_t long int
  21. # define __xstrtol xstrtol
  22. #endif
  23. /* Some pre-ANSI implementations (e.g. SunOS 4)
  24. need stderr defined if assertion checking is enabled. */
  25. #include <stdio.h>
  26. #if STDC_HEADERS
  27. # include <stdlib.h>
  28. #endif
  29. #if HAVE_STRING_H
  30. # include <string.h>
  31. #else
  32. # include <strings.h>
  33. # ifndef strchr
  34. # define strchr index
  35. # endif
  36. #endif
  37. #include <assert.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #ifndef errno
  41. extern int errno;
  42. #endif
  43. #if HAVE_LIMITS_H
  44. # include <limits.h>
  45. #endif
  46. #ifndef CHAR_BIT
  47. # define CHAR_BIT 8
  48. #endif
  49. /* The extra casts work around common compiler bugs. */
  50. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  51. /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
  52. It is necessary at least when t == time_t. */
  53. #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
  54. ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
  55. #define TYPE_MAXIMUM(t) (~ (t) 0 - TYPE_MINIMUM (t))
  56. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  57. # define IN_CTYPE_DOMAIN(c) 1
  58. #else
  59. # define IN_CTYPE_DOMAIN(c) isascii(c)
  60. #endif
  61. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  62. #include "xstrtol.h"
  63. #if !HAVE_DECL_STRTOL && !defined strtol
  64. long int strtol ();
  65. #endif
  66. #if !HAVE_DECL_STRTOUL && !defined strtoul
  67. unsigned long int strtoul ();
  68. #endif
  69. #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
  70. intmax_t strtoimax ();
  71. #endif
  72. #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
  73. uintmax_t strtoumax ();
  74. #endif
  75. static int
  76. bkm_scale (__strtol_t *x, int scale_factor)
  77. {
  78. __strtol_t product = *x * scale_factor;
  79. if (*x != product / scale_factor)
  80. return 1;
  81. *x = product;
  82. return 0;
  83. }
  84. static int
  85. bkm_scale_by_power (__strtol_t *x, int base, int power)
  86. {
  87. while (power--)
  88. if (bkm_scale (x, base))
  89. return 1;
  90. return 0;
  91. }
  92. /* FIXME: comment. */
  93. strtol_error
  94. __xstrtol (const char *s, char **ptr, int strtol_base,
  95. __strtol_t *val, const char *valid_suffixes)
  96. {
  97. char *t_ptr;
  98. char **p;
  99. __strtol_t tmp;
  100. assert (0 <= strtol_base && strtol_base <= 36);
  101. p = (ptr ? ptr : &t_ptr);
  102. if (! TYPE_SIGNED (__strtol_t))
  103. {
  104. const char *q = s;
  105. while (ISSPACE ((unsigned char) *q))
  106. ++q;
  107. if (*q == '-')
  108. return LONGINT_INVALID;
  109. }
  110. errno = 0;
  111. tmp = __strtol (s, p, strtol_base);
  112. if (errno != 0)
  113. return LONGINT_OVERFLOW;
  114. if (*p == s)
  115. return LONGINT_INVALID;
  116. /* Let valid_suffixes == NULL mean `allow any suffix'. */
  117. /* FIXME: update all callers except the ones that allow suffixes
  118. after the number, changing last parameter NULL to `""'. */
  119. if (!valid_suffixes)
  120. {
  121. *val = tmp;
  122. return LONGINT_OK;
  123. }
  124. if (**p != '\0')
  125. {
  126. int base = 1024;
  127. int suffixes = 1;
  128. int overflow;
  129. if (!strchr (valid_suffixes, **p))
  130. {
  131. *val = tmp;
  132. return LONGINT_INVALID_SUFFIX_CHAR;
  133. }
  134. if (strchr (valid_suffixes, '0'))
  135. {
  136. /* The ``valid suffix'' '0' is a special flag meaning that
  137. an optional second suffix is allowed, which can change
  138. the base, e.g. "100MD" for 100 megabytes decimal. */
  139. switch (p[0][1])
  140. {
  141. case 'B':
  142. suffixes++;
  143. break;
  144. case 'D':
  145. base = 1000;
  146. suffixes++;
  147. break;
  148. }
  149. }
  150. switch (**p)
  151. {
  152. case 'b':
  153. overflow = bkm_scale (&tmp, 512);
  154. break;
  155. case 'B':
  156. overflow = bkm_scale (&tmp, 1024);
  157. break;
  158. case 'c':
  159. overflow = 0;
  160. break;
  161. case 'E': /* Exa */
  162. overflow = bkm_scale_by_power (&tmp, base, 6);
  163. break;
  164. case 'G': /* Giga */
  165. case 'g': /* 'g' is undocumented; for compatibility only */
  166. overflow = bkm_scale_by_power (&tmp, base, 3);
  167. break;
  168. case 'k': /* kilo */
  169. overflow = bkm_scale_by_power (&tmp, base, 1);
  170. break;
  171. case 'M': /* Mega */
  172. case 'm': /* 'm' is undocumented; for compatibility only */
  173. overflow = bkm_scale_by_power (&tmp, base, 2);
  174. break;
  175. case 'P': /* Peta */
  176. overflow = bkm_scale_by_power (&tmp, base, 5);
  177. break;
  178. case 'T': /* Tera */
  179. case 't': /* 't' is undocumented; for compatibility only */
  180. overflow = bkm_scale_by_power (&tmp, base, 4);
  181. break;
  182. case 'w':
  183. overflow = bkm_scale (&tmp, 2);
  184. break;
  185. case 'Y': /* Yotta */
  186. overflow = bkm_scale_by_power (&tmp, base, 8);
  187. break;
  188. case 'Z': /* Zetta */
  189. overflow = bkm_scale_by_power (&tmp, base, 7);
  190. break;
  191. default:
  192. *val = tmp;
  193. return LONGINT_INVALID_SUFFIX_CHAR;
  194. break;
  195. }
  196. if (overflow)
  197. return LONGINT_OVERFLOW;
  198. (*p) += suffixes;
  199. }
  200. *val = tmp;
  201. return LONGINT_OK;
  202. }
  203. #ifdef TESTING_XSTRTO
  204. # include <stdio.h>
  205. # include "error.h"
  206. char *program_name;
  207. int
  208. main (int argc, char** argv)
  209. {
  210. strtol_error s_err;
  211. int i;
  212. program_name = argv[0];
  213. for (i=1; i<argc; i++)
  214. {
  215. char *p;
  216. __strtol_t val;
  217. s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
  218. if (s_err == LONGINT_OK)
  219. {
  220. printf ("%s->%lu (%s)\n", argv[i], val, p);
  221. }
  222. else
  223. {
  224. STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
  225. }
  226. }
  227. exit (0);
  228. }
  229. #endif /* TESTING_XSTRTO */