merge with 1.11.1b
[gnulib.git] / lib / xstrtol.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include "xstrtol.h" /* Get definition for __P before use. */
6
7 #ifdef STDC_HEADERS
8 #include <stdlib.h>
9 #else
10 __unsigned long int __strtol __P ((const char *, char **, int base));
11 #endif
12
13 #ifdef HAVE_STRING_H
14 # include <string.h>
15 #else
16 # include <strings.h>
17 # ifndef strchr
18 #  define strchr index
19 # endif
20 #endif
21
22 #define NDEBUG
23 #include <assert.h>
24
25 #include <errno.h>
26 #ifndef errno
27 extern int errno;
28 #endif
29
30 #if HAVE_LIMITS_H
31 #include <limits.h>
32 #endif
33
34 #ifndef ULONG_MAX
35 #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
36 #endif
37
38 #ifndef LONG_MAX
39 #define LONG_MAX ((long int) (ULONG_MAX >> 1))
40 #endif
41
42 #define BKM_SCALE(x, scale_factor, error_return)                        \
43       do                                                                \
44         {                                                               \
45           if ((x) > (double) __ZLONG_MAX / (scale_factor))              \
46             return (error_return);                                      \
47           (x) *= (scale_factor);                                        \
48         }                                                               \
49       while (0)
50
51 __unsigned long int __strtol ();
52
53 /* FIXME: comment.  */
54
55 strtol_error
56 __xstrtol (s, ptr, base, val, valid_suffixes)
57      const char *s;
58      char **ptr;
59      int base;
60      __unsigned long int *val;
61      const char *valid_suffixes;
62 {
63   char *t_ptr;
64   char **p;
65   __unsigned long int tmp;
66
67   assert (0 <= base && base <= 36);
68
69   p = (ptr ? ptr : &t_ptr);
70
71   errno = 0;
72   tmp = __strtol (s, p, base);
73   if (errno != 0)
74     return LONGINT_OVERFLOW;
75   if (*p == s)
76     return LONGINT_INVALID;
77   if (!valid_suffixes)
78     {
79       if (**p == '\0')
80         {
81           *val = tmp;
82           return LONGINT_OK;
83         }
84       else
85         return LONGINT_INVALID_SUFFIX_CHAR;
86     }
87
88   if (**p != '\0' && strchr (valid_suffixes, **p))
89     {
90       switch (**p)
91         {
92         case 'b':
93           BKM_SCALE (tmp, 512, LONGINT_OVERFLOW);
94           ++(*p);
95           break;
96
97         case 'c':
98           ++(*p);
99           break;
100
101         case 'B':
102         case 'k':
103           BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW);
104           ++(*p);
105           break;
106
107         case 'm':
108           BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW);
109           ++(*p);
110           break;
111
112         case 'w':
113           BKM_SCALE (tmp, 2, LONGINT_OVERFLOW);
114           ++(*p);
115           break;
116
117         default:
118           return LONGINT_INVALID_SUFFIX_CHAR;
119           break;
120         }
121     }
122
123   *val = tmp;
124   return LONGINT_OK;
125 }
126
127 #ifdef TESTING_XSTRTO
128
129 #include <stdio.h>
130 #include "error.h"
131
132 char *program_name;
133
134 int
135 main (int argc, char** argv)
136 {
137   strtol_error s_err;
138   int i;
139
140   program_name = argv[0];
141   for (i=1; i<argc; i++)
142     {
143       char *p;
144       __unsigned long int val;
145
146       s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
147       if (s_err == LONGINT_OK)
148         {
149           printf ("%s->%lu (%s)\n", argv[i], val, p);
150         }
151       else
152         {
153           STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
154         }
155     }
156   exit (0);
157 }
158 #endif /* TESTING_XSTRTO */