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
32 /* Some pre-ANSI implementations (e.g. SunOS 4)
33 need stderr defined if assertion checking is enabled. */
48 /* The extra casts work around common compiler bugs. */
49 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
51 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
52 # define IN_CTYPE_DOMAIN(c) 1
54 # define IN_CTYPE_DOMAIN(c) isascii(c)
57 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
61 #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
62 intmax_t strtoimax ();
65 #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
66 uintmax_t strtoumax ();
70 bkm_scale (__strtol_t *x, int scale_factor)
72 __strtol_t product = *x * scale_factor;
73 if (*x != product / scale_factor)
80 bkm_scale_by_power (__strtol_t *x, int base, int power)
83 if (bkm_scale (x, base))
92 __xstrtol (const char *s, char **ptr, int strtol_base,
93 __strtol_t *val, const char *valid_suffixes)
99 assert (0 <= strtol_base && strtol_base <= 36);
101 p = (ptr ? ptr : &t_ptr);
103 if (! TYPE_SIGNED (__strtol_t))
106 while (ISSPACE ((unsigned char) *q))
109 return LONGINT_INVALID;
113 tmp = __strtol (s, p, strtol_base);
115 return LONGINT_OVERFLOW;
119 /* If there is no number but there is a valid suffix, assume the
120 number is 1. The string is invalid otherwise. */
121 if (valid_suffixes && **p && strchr (valid_suffixes, **p))
124 return LONGINT_INVALID;
127 /* Let valid_suffixes == NULL mean `allow any suffix'. */
128 /* FIXME: update all callers except the ones that allow suffixes
129 after the number, changing last parameter NULL to `""'. */
142 if (!strchr (valid_suffixes, **p))
145 return LONGINT_INVALID_SUFFIX_CHAR;
148 if (strchr (valid_suffixes, '0'))
150 /* The ``valid suffix'' '0' is a special flag meaning that
151 an optional second suffix is allowed, which can change
152 the base. A suffix "B" (e.g. "100MB") stands for a power
153 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
154 a power of 1024. If no suffix (e.g. "100M"), assume
165 case 'D': /* 'D' is obsolescent */
175 overflow = bkm_scale (&tmp, 512);
179 overflow = bkm_scale (&tmp, 1024);
186 case 'E': /* exa or exbi */
187 overflow = bkm_scale_by_power (&tmp, base, 6);
190 case 'G': /* giga or gibi */
191 case 'g': /* 'g' is undocumented; for compatibility only */
192 overflow = bkm_scale_by_power (&tmp, base, 3);
197 overflow = bkm_scale_by_power (&tmp, base, 1);
200 case 'M': /* mega or mebi */
201 case 'm': /* 'm' is undocumented; for compatibility only */
202 overflow = bkm_scale_by_power (&tmp, base, 2);
205 case 'P': /* peta or pebi */
206 overflow = bkm_scale_by_power (&tmp, base, 5);
209 case 'T': /* tera or tebi */
210 case 't': /* 't' is undocumented; for compatibility only */
211 overflow = bkm_scale_by_power (&tmp, base, 4);
215 overflow = bkm_scale (&tmp, 2);
218 case 'Y': /* yotta or 2**80 */
219 overflow = bkm_scale_by_power (&tmp, base, 8);
222 case 'Z': /* zetta or 2**70 */
223 overflow = bkm_scale_by_power (&tmp, base, 7);
228 return LONGINT_INVALID_SUFFIX_CHAR;
233 return LONGINT_OVERFLOW;
242 #ifdef TESTING_XSTRTO
250 main (int argc, char **argv)
255 program_name = argv[0];
256 for (i=1; i<argc; i++)
261 s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
262 if (s_err == LONGINT_OK)
264 printf ("%s->%lu (%s)\n", argv[i], val, p);
268 STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
274 #endif /* TESTING_XSTRTO */