Update ebuild.syntax to include new EAPI 6 keywords
[midnight-commander.git] / lib / strutil / xstrtol.c
blob6fa4aa1981582783a88177db2636c6516c59fab8
1 /* A more useful interface to strtol.
3 Copyright (C) 1995-2016
4 Free Software Foundation, Inc.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Jim Meyering. */
21 #include <config.h>
23 /* Some pre-ANSI implementations (e.g. SunOS 4)
24 need stderr defined if assertion checking is enabled. */
25 #include <stdio.h>
27 #ifdef HAVE_ASSERT_H
28 #include <assert.h>
29 #endif
30 #include <ctype.h>
31 #include <errno.h>
32 #include <inttypes.h>
33 #include <limits.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "lib/strutil.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 static strtol_error_t
52 bkm_scale (uintmax_t * x, int scale_factor)
54 if (UINTMAX_MAX / scale_factor < *x)
56 *x = UINTMAX_MAX;
57 return LONGINT_OVERFLOW;
60 *x *= scale_factor;
61 return LONGINT_OK;
64 /* --------------------------------------------------------------------------------------------- */
66 static strtol_error_t
67 bkm_scale_by_power (uintmax_t * x, int base, int power)
69 strtol_error_t err = LONGINT_OK;
70 while (power-- != 0)
71 err |= bkm_scale (x, base);
72 return err;
75 /* --------------------------------------------------------------------------------------------- */
76 /*** public functions ****************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
79 strtol_error_t
80 xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *valid_suffixes)
82 char *t_ptr;
83 char **p;
84 uintmax_t tmp;
85 strtol_error_t err = LONGINT_OK;
87 #ifdef HAVE_ASSERT_H
88 assert (0 <= base && base <= 36);
89 #endif
91 p = (ptr != NULL ? ptr : &t_ptr);
94 const char *q = s;
95 unsigned char ch = *q;
97 while (isspace (ch))
98 ch = *++q;
100 if (ch == '-')
101 return LONGINT_INVALID;
104 errno = 0;
105 tmp = strtol (s, p, base);
107 if (*p == s)
109 /* If there is no number but there is a valid suffix, assume the
110 number is 1. The string is invalid otherwise. */
111 if (valid_suffixes != NULL && **p != '\0' && strchr (valid_suffixes, **p) != NULL)
112 tmp = 1;
113 else
114 return LONGINT_INVALID;
116 else if (errno != 0)
118 if (errno != ERANGE)
119 return LONGINT_INVALID;
120 err = LONGINT_OVERFLOW;
123 /* Let valid_suffixes == NULL mean "allow any suffix". */
124 /* FIXME: update all callers except the ones that allow suffixes
125 after the number, changing last parameter NULL to "". */
126 if (valid_suffixes == NULL)
128 *val = tmp;
129 return err;
132 if (**p != '\0')
134 int suffixes = 1;
135 strtol_error_t overflow;
137 if (strchr (valid_suffixes, **p) == NULL)
139 *val = tmp;
140 return err | LONGINT_INVALID_SUFFIX_CHAR;
143 base = 1024;
145 switch (**p)
147 case 'E':
148 case 'G':
149 case 'g':
150 case 'k':
151 case 'K':
152 case 'M':
153 case 'm':
154 case 'P':
155 case 'T':
156 case 't':
157 case 'Y':
158 case 'Z':
159 if (strchr (valid_suffixes, '0') != NULL)
161 /* The "valid suffix" '0' is a special flag meaning that
162 an optional second suffix is allowed, which can change
163 the base. A suffix "B" (e.g. "100MB") stands for a power
164 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
165 a power of 1024. If no suffix (e.g. "100M"), assume
166 power-of-1024. */
168 switch (p[0][1])
170 case 'i':
171 if (p[0][2] == 'B')
172 suffixes += 2;
173 break;
175 case 'B':
176 case 'D': /* 'D' is obsolescent */
177 base = 1000;
178 suffixes++;
179 break;
180 default:
181 break;
184 default:
185 break;
188 switch (**p)
190 case 'b':
191 overflow = bkm_scale (&tmp, 512);
192 break;
194 case 'B':
195 /* This obsolescent first suffix is distinct from the 'B'
196 second suffix above. E.g., 'tar -L 1000B' means change
197 the tape after writing 1000 KiB of data. */
198 overflow = bkm_scale (&tmp, 1024);
199 break;
201 case 'c':
202 overflow = LONGINT_OK;
203 break;
205 case 'E': /* exa or exbi */
206 overflow = bkm_scale_by_power (&tmp, base, 6);
207 break;
209 case 'G': /* giga or gibi */
210 case 'g': /* 'g' is undocumented; for compatibility only */
211 overflow = bkm_scale_by_power (&tmp, base, 3);
212 break;
214 case 'k': /* kilo */
215 case 'K': /* kibi */
216 overflow = bkm_scale_by_power (&tmp, base, 1);
217 break;
219 case 'M': /* mega or mebi */
220 case 'm': /* 'm' is undocumented; for compatibility only */
221 overflow = bkm_scale_by_power (&tmp, base, 2);
222 break;
224 case 'P': /* peta or pebi */
225 overflow = bkm_scale_by_power (&tmp, base, 5);
226 break;
228 case 'T': /* tera or tebi */
229 case 't': /* 't' is undocumented; for compatibility only */
230 overflow = bkm_scale_by_power (&tmp, base, 4);
231 break;
233 case 'w':
234 overflow = bkm_scale (&tmp, 2);
235 break;
237 case 'Y': /* yotta or 2**80 */
238 overflow = bkm_scale_by_power (&tmp, base, 8);
239 break;
241 case 'Z': /* zetta or 2**70 */
242 overflow = bkm_scale_by_power (&tmp, base, 7);
243 break;
245 default:
246 *val = tmp;
247 return err | LONGINT_INVALID_SUFFIX_CHAR;
250 err |= overflow;
251 *p += suffixes;
252 if (**p != '\0')
253 err |= LONGINT_INVALID_SUFFIX_CHAR;
256 *val = tmp;
257 return err;
260 /* --------------------------------------------------------------------------------------------- */