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