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