4
0

xstrtol.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995, 1996, 1998-2000 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. #ifndef strtol
  64. long int strtol ();
  65. #endif
  66. #ifndef strtoul
  67. unsigned long int strtoul ();
  68. #endif
  69. #ifndef strtoumax
  70. uintmax_t strtoumax ();
  71. #endif
  72. static int
  73. bkm_scale (__strtol_t *x, int scale_factor)
  74. {
  75. __strtol_t product = *x * scale_factor;
  76. if (*x != product / scale_factor)
  77. return 1;
  78. *x = product;
  79. return 0;
  80. }
  81. static int
  82. bkm_scale_by_power (__strtol_t *x, int base, int power)
  83. {
  84. while (power--)
  85. if (bkm_scale (x, base))
  86. return 1;
  87. return 0;
  88. }
  89. /* FIXME: comment. */
  90. strtol_error
  91. __xstrtol (const char *s, char **ptr, int strtol_base,
  92. __strtol_t *val, const char *valid_suffixes)
  93. {
  94. char *t_ptr;
  95. char **p;
  96. __strtol_t tmp;
  97. assert (0 <= strtol_base && strtol_base <= 36);
  98. p = (ptr ? ptr : &t_ptr);
  99. if (! TYPE_SIGNED (__strtol_t))
  100. {
  101. const char *q = s;
  102. while (ISSPACE ((unsigned char) *q))
  103. ++q;
  104. if (*q == '-')
  105. return LONGINT_INVALID;
  106. }
  107. errno = 0;
  108. tmp = __strtol (s, p, strtol_base);
  109. if (errno != 0)
  110. return LONGINT_OVERFLOW;
  111. if (*p == s)
  112. return LONGINT_INVALID;
  113. /* Let valid_suffixes == NULL mean `allow any suffix'. */
  114. /* FIXME: update all callers except the ones that allow suffixes
  115. after the number, changing last parameter NULL to `""'. */
  116. if (!valid_suffixes)
  117. {
  118. *val = tmp;
  119. return LONGINT_OK;
  120. }
  121. if (**p != '\0')
  122. {
  123. int base = 1024;
  124. int suffixes = 1;
  125. int overflow;
  126. if (!strchr (valid_suffixes, **p))
  127. {
  128. *val = tmp;
  129. return LONGINT_INVALID_SUFFIX_CHAR;
  130. }
  131. if (strchr (valid_suffixes, '0'))
  132. {
  133. /* The ``valid suffix'' '0' is a special flag meaning that
  134. an optional second suffix is allowed, which can change
  135. the base, e.g. "100MD" for 100 megabytes decimal. */
  136. switch (p[0][1])
  137. {
  138. case 'B':
  139. suffixes++;
  140. break;
  141. case 'D':
  142. base = 1000;
  143. suffixes++;
  144. break;
  145. }
  146. }
  147. switch (**p)
  148. {
  149. case 'b':
  150. overflow = bkm_scale (&tmp, 512);
  151. break;
  152. case 'B':
  153. overflow = bkm_scale (&tmp, 1024);
  154. break;
  155. case 'c':
  156. overflow = 0;
  157. break;
  158. case 'E': /* Exa */
  159. overflow = bkm_scale_by_power (&tmp, base, 6);
  160. break;
  161. case 'G': /* Giga */
  162. case 'g': /* 'g' is undocumented; for compatibility only */
  163. overflow = bkm_scale_by_power (&tmp, base, 3);
  164. break;
  165. case 'k': /* kilo */
  166. overflow = bkm_scale_by_power (&tmp, base, 1);
  167. break;
  168. case 'M': /* Mega */
  169. case 'm': /* 'm' is undocumented; for compatibility only */
  170. overflow = bkm_scale_by_power (&tmp, base, 2);
  171. break;
  172. case 'P': /* Peta */
  173. overflow = bkm_scale_by_power (&tmp, base, 5);
  174. break;
  175. case 'T': /* Tera */
  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 */
  183. overflow = bkm_scale_by_power (&tmp, base, 8);
  184. break;
  185. case 'Z': /* Zetta */
  186. overflow = bkm_scale_by_power (&tmp, base, 7);
  187. break;
  188. default:
  189. *val = tmp;
  190. return LONGINT_INVALID_SUFFIX_CHAR;
  191. break;
  192. }
  193. if (overflow)
  194. return LONGINT_OVERFLOW;
  195. (*p) += suffixes;
  196. }
  197. *val = tmp;
  198. return LONGINT_OK;
  199. }
  200. #ifdef TESTING_XSTRTO
  201. # include <stdio.h>
  202. # include "error.h"
  203. char *program_name;
  204. int
  205. main (int argc, char** argv)
  206. {
  207. strtol_error s_err;
  208. int i;
  209. program_name = argv[0];
  210. for (i=1; i<argc; i++)
  211. {
  212. char *p;
  213. __strtol_t val;
  214. s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
  215. if (s_err == LONGINT_OK)
  216. {
  217. printf ("%s->%lu (%s)\n", argv[i], val, p);
  218. }
  219. else
  220. {
  221. STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
  222. }
  223. }
  224. exit (0);
  225. }
  226. #endif /* TESTING_XSTRTO */