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