a7386343f6b2f596c2522e0101af615ce59a35a1
[pspp] / lib / xstrtol.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #ifdef STDC_HEADERS
6 #include <stdlib.h>
7 #else
8 long int __strtol (const char *, char **, int base);
9 #endif
10
11 #ifdef HAVE_STRING_H
12 # include <string.h>
13 #else
14 # include <strings.h>
15 # ifndef strchr
16 #  define strchr index
17 # endif
18 #endif
19
20 #include <assert.h>
21 /* FIXME: define NDEBUG before release.  */
22
23 #include <errno.h>
24 #ifndef errno
25 extern int errno;
26 #endif
27
28 #if HAVE_LIMITS_H
29 #include <limits.h>
30 #endif
31
32 #ifndef ULONG_MAX
33 #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
34 #endif
35
36 #ifndef LONG_MAX
37 #define LONG_MAX ((long int) (ULONG_MAX >> 1))
38 #endif
39
40 #include "xstrtol.h"
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 'k':
102           BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW);
103           ++(*p);
104           break;
105
106         case 'm':
107           BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW);
108           ++(*p);
109           break;
110
111         case 'w':
112           BKM_SCALE (tmp, 2, LONGINT_OVERFLOW);
113           ++(*p);
114           break;
115
116         default:
117           return LONGINT_INVALID_SUFFIX_CHAR;
118           break;
119         }
120     }
121
122   *val = tmp;
123   return LONGINT_OK;
124 }
125
126 #ifdef TESTING_XSTRTO
127
128 #include <stdio.h>
129 #include "error.h"
130
131 char *program_name;
132
133 int
134 main (int argc, char** argv)
135 {
136   strtol_error s_err;
137   int i;
138
139   program_name = argv[0];
140   for (i=1; i<argc; i++)
141     {
142       char *p;
143       __unsigned long int val;
144
145       s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
146       if (s_err == LONGINT_OK)
147         {
148           printf ("%s->%lu (%s)\n", argv[i], val, p);
149         }
150       else
151         {
152           STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
153         }
154     }
155   exit (0);
156 }
157 #endif /* TESTING_XSTRTO */