1 /* A more useful interface to strtol.
3 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006
4 Free 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by Jim Meyering. */
29 # define __strtol strtol
30 # define __strtol_t long int
31 # define __xstrtol xstrtol
32 # define STRTOL_T_MINIMUM LONG_MIN
33 # define STRTOL_T_MAXIMUM LONG_MAX
36 /* Some pre-ANSI implementations (e.g. SunOS 4)
37 need stderr defined if assertion checking is enabled. */
49 #ifndef STRTOL_T_MINIMUM
50 # define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
51 # define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
54 #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
55 intmax_t strtoimax ();
58 #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
59 uintmax_t strtoumax ();
63 bkm_scale (__strtol_t *x, int scale_factor)
65 if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
67 *x = STRTOL_T_MINIMUM;
68 return LONGINT_OVERFLOW;
70 if (STRTOL_T_MAXIMUM / scale_factor < *x)
72 *x = STRTOL_T_MAXIMUM;
73 return LONGINT_OVERFLOW;
80 bkm_scale_by_power (__strtol_t *x, int base, int power)
82 strtol_error err = LONGINT_OK;
84 err |= bkm_scale (x, base);
91 __xstrtol (const char *s, char **ptr, int strtol_base,
92 __strtol_t *val, const char *valid_suffixes)
97 strtol_error err = LONGINT_OK;
99 assert (0 <= strtol_base && strtol_base <= 36);
101 p = (ptr ? ptr : &t_ptr);
103 if (! TYPE_SIGNED (__strtol_t))
106 unsigned char ch = *q;
110 return LONGINT_INVALID;
114 tmp = __strtol (s, p, strtol_base);
118 /* If there is no number but there is a valid suffix, assume the
119 number is 1. The string is invalid otherwise. */
120 if (valid_suffixes && **p && strchr (valid_suffixes, **p))
123 return LONGINT_INVALID;
128 return LONGINT_INVALID;
129 err = LONGINT_OVERFLOW;
132 /* Let valid_suffixes == NULL mean `allow any suffix'. */
133 /* FIXME: update all callers except the ones that allow suffixes
134 after the number, changing last parameter NULL to `""'. */
145 strtol_error overflow;
147 if (!strchr (valid_suffixes, **p))
150 return err | LONGINT_INVALID_SUFFIX_CHAR;
153 if (strchr (valid_suffixes, '0'))
155 /* The ``valid suffix'' '0' is a special flag meaning that
156 an optional second suffix is allowed, which can change
157 the base. A suffix "B" (e.g. "100MB") stands for a power
158 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
159 a power of 1024. If no suffix (e.g. "100M"), assume
170 case 'D': /* 'D' is obsolescent */
180 overflow = bkm_scale (&tmp, 512);
184 overflow = bkm_scale (&tmp, 1024);
191 case 'E': /* exa or exbi */
192 overflow = bkm_scale_by_power (&tmp, base, 6);
195 case 'G': /* giga or gibi */
196 case 'g': /* 'g' is undocumented; for compatibility only */
197 overflow = bkm_scale_by_power (&tmp, base, 3);
202 overflow = bkm_scale_by_power (&tmp, base, 1);
205 case 'M': /* mega or mebi */
206 case 'm': /* 'm' is undocumented; for compatibility only */
207 overflow = bkm_scale_by_power (&tmp, base, 2);
210 case 'P': /* peta or pebi */
211 overflow = bkm_scale_by_power (&tmp, base, 5);
214 case 'T': /* tera or tebi */
215 case 't': /* 't' is undocumented; for compatibility only */
216 overflow = bkm_scale_by_power (&tmp, base, 4);
220 overflow = bkm_scale (&tmp, 2);
223 case 'Y': /* yotta or 2**80 */
224 overflow = bkm_scale_by_power (&tmp, base, 8);
227 case 'Z': /* zetta or 2**70 */
228 overflow = bkm_scale_by_power (&tmp, base, 7);
233 return err | LONGINT_INVALID_SUFFIX_CHAR;
239 err |= LONGINT_INVALID_SUFFIX_CHAR;
246 #ifdef TESTING_XSTRTO
254 main (int argc, char **argv)
259 program_name = argv[0];
260 for (i=1; i<argc; i++)
265 s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
266 if (s_err == LONGINT_OK)
268 printf ("%s->%lu (%s)\n", argv[i], val, p);
272 STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
278 #endif /* TESTING_XSTRTO */