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