* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / xstrtol.c
1 /* A more useful interface to strtol.
2
3    Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006
4    Free Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* Written by Jim Meyering. */
21
22 #include <config.h>
23
24 #include "xstrtol.h"
25
26 #ifndef __strtol
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
32 #endif
33
34 /* Some pre-ANSI implementations (e.g. SunOS 4)
35    need stderr defined if assertion checking is enabled.  */
36 #include <stdio.h>
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "intprops.h"
46
47 #ifndef STRTOL_T_MINIMUM
48 # define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
49 # define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
50 #endif
51
52 #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
53 intmax_t strtoimax ();
54 #endif
55
56 #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
57 uintmax_t strtoumax ();
58 #endif
59
60 static strtol_error
61 bkm_scale (__strtol_t *x, int scale_factor)
62 {
63   if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
64     {
65       *x = STRTOL_T_MINIMUM;
66       return LONGINT_OVERFLOW;
67     }
68   if (STRTOL_T_MAXIMUM / scale_factor < *x)
69     {
70       *x = STRTOL_T_MAXIMUM;
71       return LONGINT_OVERFLOW;
72     }
73   *x *= scale_factor;
74   return LONGINT_OK;
75 }
76
77 static strtol_error
78 bkm_scale_by_power (__strtol_t *x, int base, int power)
79 {
80   strtol_error err = LONGINT_OK;
81   while (power--)
82     err |= bkm_scale (x, base);
83   return err;
84 }
85
86 /* FIXME: comment.  */
87
88 strtol_error
89 __xstrtol (const char *s, char **ptr, int strtol_base,
90            __strtol_t *val, const char *valid_suffixes)
91 {
92   char *t_ptr;
93   char **p;
94   __strtol_t tmp;
95   strtol_error err = LONGINT_OK;
96
97   assert (0 <= strtol_base && strtol_base <= 36);
98
99   p = (ptr ? ptr : &t_ptr);
100
101   if (! TYPE_SIGNED (__strtol_t))
102     {
103       const char *q = s;
104       unsigned char ch = *q;
105       while (isspace (ch))
106         ch = *++q;
107       if (ch == '-')
108         return LONGINT_INVALID;
109     }
110
111   errno = 0;
112   tmp = __strtol (s, p, strtol_base);
113
114   if (*p == s)
115     {
116       /* If there is no number but there is a valid suffix, assume the
117          number is 1.  The string is invalid otherwise.  */
118       if (valid_suffixes && **p && strchr (valid_suffixes, **p))
119         tmp = 1;
120       else
121         return LONGINT_INVALID;
122     }
123   else if (errno != 0)
124     {
125       if (errno != ERANGE)
126         return LONGINT_INVALID;
127       err = LONGINT_OVERFLOW;
128     }
129
130   /* Let valid_suffixes == NULL mean `allow any suffix'.  */
131   /* FIXME: update all callers except the ones that allow suffixes
132      after the number, changing last parameter NULL to `""'.  */
133   if (!valid_suffixes)
134     {
135       *val = tmp;
136       return err;
137     }
138
139   if (**p != '\0')
140     {
141       int base = 1024;
142       int suffixes = 1;
143       strtol_error overflow;
144
145       if (!strchr (valid_suffixes, **p))
146         {
147           *val = tmp;
148           return err | LONGINT_INVALID_SUFFIX_CHAR;
149         }
150
151       if (strchr (valid_suffixes, '0'))
152         {
153           /* The ``valid suffix'' '0' is a special flag meaning that
154              an optional second suffix is allowed, which can change
155              the base.  A suffix "B" (e.g. "100MB") stands for a power
156              of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
157              a power of 1024.  If no suffix (e.g. "100M"), assume
158              power-of-1024.  */
159
160           switch (p[0][1])
161             {
162             case 'i':
163               if (p[0][2] == 'B')
164                 suffixes += 2;
165               break;
166
167             case 'B':
168             case 'D': /* 'D' is obsolescent */
169               base = 1000;
170               suffixes++;
171               break;
172             }
173         }
174
175       switch (**p)
176         {
177         case 'b':
178           overflow = bkm_scale (&tmp, 512);
179           break;
180
181         case 'B':
182           overflow = bkm_scale (&tmp, 1024);
183           break;
184
185         case 'c':
186           overflow = 0;
187           break;
188
189         case 'E': /* exa or exbi */
190           overflow = bkm_scale_by_power (&tmp, base, 6);
191           break;
192
193         case 'G': /* giga or gibi */
194         case 'g': /* 'g' is undocumented; for compatibility only */
195           overflow = bkm_scale_by_power (&tmp, base, 3);
196           break;
197
198         case 'k': /* kilo */
199         case 'K': /* kibi */
200           overflow = bkm_scale_by_power (&tmp, base, 1);
201           break;
202
203         case 'M': /* mega or mebi */
204         case 'm': /* 'm' is undocumented; for compatibility only */
205           overflow = bkm_scale_by_power (&tmp, base, 2);
206           break;
207
208         case 'P': /* peta or pebi */
209           overflow = bkm_scale_by_power (&tmp, base, 5);
210           break;
211
212         case 'T': /* tera or tebi */
213         case 't': /* 't' is undocumented; for compatibility only */
214           overflow = bkm_scale_by_power (&tmp, base, 4);
215           break;
216
217         case 'w':
218           overflow = bkm_scale (&tmp, 2);
219           break;
220
221         case 'Y': /* yotta or 2**80 */
222           overflow = bkm_scale_by_power (&tmp, base, 8);
223           break;
224
225         case 'Z': /* zetta or 2**70 */
226           overflow = bkm_scale_by_power (&tmp, base, 7);
227           break;
228
229         default:
230           *val = tmp;
231           return err | LONGINT_INVALID_SUFFIX_CHAR;
232         }
233
234       err |= overflow;
235       *p += suffixes;
236       if (**p)
237         err |= LONGINT_INVALID_SUFFIX_CHAR;
238     }
239
240   *val = tmp;
241   return err;
242 }
243
244 #ifdef TESTING_XSTRTO
245
246 # include <stdio.h>
247 # include "error.h"
248
249 char *program_name;
250
251 int
252 main (int argc, char **argv)
253 {
254   strtol_error s_err;
255   int i;
256
257   program_name = argv[0];
258   for (i=1; i<argc; i++)
259     {
260       char *p;
261       __strtol_t val;
262
263       s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
264       if (s_err == LONGINT_OK)
265         {
266           printf ("%s->%lu (%s)\n", argv[i], val, p);
267         }
268       else
269         {
270           STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
271         }
272     }
273   exit (0);
274 }
275
276 #endif /* TESTING_XSTRTO */