.
[coreutils.git] / lib / xstrtol.c
blobad2bcdbf10fe7ae87b8ab7c54e2b6a1251e08945
1 /* A more useful interface to strtol.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Jim Meyering. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 #endif
28 #if HAVE_STRING_H
29 # include <string.h>
30 #else
31 # include <strings.h>
32 # ifndef strchr
33 # define strchr index
34 # endif
35 #endif
37 #define NDEBUG
38 #include <assert.h>
40 #include <errno.h>
41 #ifndef errno
42 extern int errno;
43 #endif
45 #if HAVE_LIMITS_H
46 # include <limits.h>
47 #endif
49 #ifndef ULONG_MAX
50 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
51 #endif
53 #ifndef LONG_MAX
54 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
55 #endif
57 #include "xstrtol.h"
59 #define BKM_SCALE(x, scale_factor, error_return) \
60 do \
61 { \
62 if ((x) > (double) __ZLONG_MAX / (scale_factor)) \
63 return (error_return); \
64 (x) *= (scale_factor); \
65 } \
66 while (0)
68 __unsigned long int __strtol ();
70 /* FIXME: comment. */
72 strtol_error
73 __xstrtol (s, ptr, base, val, valid_suffixes)
74 const char *s;
75 char **ptr;
76 int base;
77 __unsigned long int *val;
78 const char *valid_suffixes;
80 char *t_ptr;
81 char **p;
82 __unsigned long int tmp;
84 assert (0 <= base && base <= 36);
86 p = (ptr ? ptr : &t_ptr);
88 errno = 0;
89 tmp = __strtol (s, p, base);
90 if (errno != 0)
91 return LONGINT_OVERFLOW;
92 if (*p == s)
93 return LONGINT_INVALID;
95 /* Let valid_suffixes == NULL mean `allow any suffix'. */
96 /* FIXME: update all callers except the one in tail.c changing
97 last parameter NULL to `""'. */
98 if (!valid_suffixes)
100 *val = tmp;
101 return LONGINT_OK;
104 if (**p != '\0')
106 if (!strchr (valid_suffixes, **p))
107 return LONGINT_INVALID_SUFFIX_CHAR;
109 switch (**p)
111 case 'b':
112 BKM_SCALE (tmp, 512, LONGINT_OVERFLOW);
113 ++(*p);
114 break;
116 case 'c':
117 ++(*p);
118 break;
120 case 'B':
121 case 'k':
122 BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW);
123 ++(*p);
124 break;
126 case 'm':
127 BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW);
128 ++(*p);
129 break;
131 case 'w':
132 BKM_SCALE (tmp, 2, LONGINT_OVERFLOW);
133 ++(*p);
134 break;
136 default:
137 return LONGINT_INVALID_SUFFIX_CHAR;
138 break;
142 *val = tmp;
143 return LONGINT_OK;
146 #ifdef TESTING_XSTRTO
148 # include <stdio.h>
149 # include "error.h"
151 char *program_name;
154 main (int argc, char** argv)
156 strtol_error s_err;
157 int i;
159 program_name = argv[0];
160 for (i=1; i<argc; i++)
162 char *p;
163 __unsigned long int val;
165 s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
166 if (s_err == LONGINT_OK)
168 printf ("%s->%lu (%s)\n", argv[i], val, p);
170 else
172 STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
175 exit (0);
178 #endif /* TESTING_XSTRTO */