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