1 /* A more useful interface to strtol.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Jim Meyering (meyering@na-net.ornl.gov) */
50 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
54 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
59 #define BKM_SCALE(x, scale_factor, error_return) \
62 if ((x) > (double) __ZLONG_MAX / (scale_factor)) \
63 return (error_return); \
64 (x) *= (scale_factor); \
68 __unsigned long int __strtol ();
73 __xstrtol (s, ptr, base, val, valid_suffixes)
77 __unsigned long int *val;
78 const char *valid_suffixes;
82 __unsigned long int tmp;
84 assert (0 <= base && base <= 36);
86 p = (ptr ? ptr : &t_ptr);
89 tmp = __strtol (s, p, base);
91 return LONGINT_OVERFLOW;
93 return LONGINT_INVALID;
102 return LONGINT_INVALID_SUFFIX_CHAR;
105 if (**p != '\0' && strchr (valid_suffixes, **p))
110 BKM_SCALE (tmp, 512, LONGINT_OVERFLOW);
120 BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW);
125 BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW);
130 BKM_SCALE (tmp, 2, LONGINT_OVERFLOW);
135 return LONGINT_INVALID_SUFFIX_CHAR;
144 #ifdef TESTING_XSTRTO
152 main (int argc, char** argv)
157 program_name = argv[0];
158 for (i=1; i<argc; i++)
161 __unsigned long int val;
163 s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
164 if (s_err == LONGINT_OK)
166 printf ("%s->%lu (%s)\n", argv[i], val, p);
170 STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
175 #endif /* TESTING_XSTRTO */