1 /* A more useful interface to strtol.
3 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by Jim Meyering. */
27 # define __strtol strtol
28 # define __strtol_t long int
29 # define __xstrtol xstrtol
30 # define STRTOL_T_MINIMUM LONG_MIN
31 # define STRTOL_T_MAXIMUM LONG_MAX
34 /* Some pre-ANSI implementations (e.g. SunOS 4)
35 need stderr defined if assertion checking is enabled. */
50 /* The extra casts work around common compiler bugs. */
51 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
52 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
53 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
55 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
57 #ifndef STRTOL_T_MINIMUM
58 # define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
59 # define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
62 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
63 # define IN_CTYPE_DOMAIN(c) 1
65 # define IN_CTYPE_DOMAIN(c) isascii(c)
68 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
72 #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
73 intmax_t strtoimax ();
76 #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
77 uintmax_t strtoumax ();
81 bkm_scale (__strtol_t *x, int scale_factor)
83 if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
85 *x = STRTOL_T_MINIMUM;
86 return LONGINT_OVERFLOW;
88 if (STRTOL_T_MAXIMUM / scale_factor < *x)
90 *x = STRTOL_T_MAXIMUM;
91 return LONGINT_OVERFLOW;
98 bkm_scale_by_power (__strtol_t *x, int base, int power)
100 strtol_error err = LONGINT_OK;
102 err |= bkm_scale (x, base);
106 /* FIXME: comment. */
109 __xstrtol (const char *s, char **ptr, int strtol_base,
110 __strtol_t *val, const char *valid_suffixes)
115 strtol_error err = LONGINT_OK;
117 assert (0 <= strtol_base && strtol_base <= 36);
119 p = (ptr ? ptr : &t_ptr);
121 if (! TYPE_SIGNED (__strtol_t))
124 while (ISSPACE ((unsigned char) *q))
127 return LONGINT_INVALID;
131 tmp = __strtol (s, p, strtol_base);
135 /* If there is no number but there is a valid suffix, assume the
136 number is 1. The string is invalid otherwise. */
137 if (valid_suffixes && **p && strchr (valid_suffixes, **p))
140 return LONGINT_INVALID;
145 return LONGINT_INVALID;
146 err = LONGINT_OVERFLOW;
149 /* Let valid_suffixes == NULL mean `allow any suffix'. */
150 /* FIXME: update all callers except the ones that allow suffixes
151 after the number, changing last parameter NULL to `""'. */
162 strtol_error overflow;
164 if (!strchr (valid_suffixes, **p))
167 return err | LONGINT_INVALID_SUFFIX_CHAR;
170 if (strchr (valid_suffixes, '0'))
172 /* The ``valid suffix'' '0' is a special flag meaning that
173 an optional second suffix is allowed, which can change
174 the base. A suffix "B" (e.g. "100MB") stands for a power
175 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
176 a power of 1024. If no suffix (e.g. "100M"), assume
187 case 'D': /* 'D' is obsolescent */
197 overflow = bkm_scale (&tmp, 512);
201 overflow = bkm_scale (&tmp, 1024);
208 case 'E': /* exa or exbi */
209 overflow = bkm_scale_by_power (&tmp, base, 6);
212 case 'G': /* giga or gibi */
213 case 'g': /* 'g' is undocumented; for compatibility only */
214 overflow = bkm_scale_by_power (&tmp, base, 3);
219 overflow = bkm_scale_by_power (&tmp, base, 1);
222 case 'M': /* mega or mebi */
223 case 'm': /* 'm' is undocumented; for compatibility only */
224 overflow = bkm_scale_by_power (&tmp, base, 2);
227 case 'P': /* peta or pebi */
228 overflow = bkm_scale_by_power (&tmp, base, 5);
231 case 'T': /* tera or tebi */
232 case 't': /* 't' is undocumented; for compatibility only */
233 overflow = bkm_scale_by_power (&tmp, base, 4);
237 overflow = bkm_scale (&tmp, 2);
240 case 'Y': /* yotta or 2**80 */
241 overflow = bkm_scale_by_power (&tmp, base, 8);
244 case 'Z': /* zetta or 2**70 */
245 overflow = bkm_scale_by_power (&tmp, base, 7);
250 return err | LONGINT_INVALID_SUFFIX_CHAR;
257 err |= LONGINT_INVALID_SUFFIX_CHAR;
264 #ifdef TESTING_XSTRTO
272 main (int argc, char **argv)
277 program_name = argv[0];
278 for (i=1; i<argc; i++)
283 s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
284 if (s_err == LONGINT_OK)
286 printf ("%s->%lu (%s)\n", argv[i], val, p);
290 STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
296 #endif /* TESTING_XSTRTO */