1 /* A more useful interface to strtol.
3 Copyright (C) 1995-2024
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. */
30 #include "lib/strutil.h"
32 /*** global variables ****************************************************************************/
34 /*** file scope macro definitions ****************************************************************/
36 /*** file scope type declarations ****************************************************************/
38 /*** forward declarations (file scope functions) *************************************************/
40 /*** file scope variables ************************************************************************/
42 /* --------------------------------------------------------------------------------------------- */
43 /*** file scope functions ************************************************************************/
44 /* --------------------------------------------------------------------------------------------- */
47 bkm_scale (uintmax_t *x
, int scale_factor
)
49 if (UINTMAX_MAX
/ scale_factor
< *x
)
52 return LONGINT_OVERFLOW
;
59 /* --------------------------------------------------------------------------------------------- */
62 bkm_scale_by_power (uintmax_t *x
, int base
, int power
)
64 strtol_error_t err
= LONGINT_OK
;
66 err
|= bkm_scale (x
, base
);
70 /* --------------------------------------------------------------------------------------------- */
71 /*** public functions ****************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
74 /* Act like the system's strtol (NPTR, ENDPTR, BASE) except:
75 - The TYPE of the result might be something other than long int.
76 - Return strtol_error, and store any result through an additional
77 TYPE *VAL pointer instead of returning the result.
78 - If TYPE is unsigned, reject leading '-'.
79 - Behavior is undefined if BASE is negative, 1, or greater than 36.
80 (In this respect xstrtol acts like the C standard, not like POSIX.)
81 - Accept an additional char const *VALID_SUFFIXES pointer to a
82 possibly-empty string containing allowed numeric suffixes,
83 which multiply the value. These include SI suffixes like 'k' and 'M';
84 these normally stand for powers of 1024, but if VALID_SUFFIXES also
85 includes '0' they can be followed by "B" to stand for the usual
86 SI powers of 1000 (or by "iB" to stand for powers of 1024 as before).
87 Other supported suffixes include 'K' for 1024 or 1000, 'b' for 512,
88 'c' for 1, and 'w' for 2.
89 - Suppose that after the initial whitespace, the number is missing
90 but there is a valid suffix. Then the number is treated as 1.
93 xstrtoumax (const char *nptr
, char **endptr
, int base
, uintmax_t *val
, const char *valid_suffixes
)
98 strtol_error_t err
= LONGINT_OK
;
100 p
= endptr
!= NULL
? endptr
: &t_ptr
;
103 const char *q
= nptr
;
104 unsigned char ch
= *q
;
112 return LONGINT_INVALID
;
117 tmp
= strtol (nptr
, p
, base
);
121 /* If there is no number but there is a valid suffix, assume the
122 number is 1. The string is invalid otherwise. */
123 if (!(valid_suffixes
!= NULL
&& *nptr
!= '\0' && strchr (valid_suffixes
, *nptr
) != NULL
))
124 return LONGINT_INVALID
;
130 return LONGINT_INVALID
;
131 err
= LONGINT_OVERFLOW
;
134 /* Let valid_suffixes == NULL mean "allow any suffix". */
135 /* FIXME: update all callers except the ones that allow suffixes
136 after the number, changing last parameter NULL to "". */
137 if (valid_suffixes
== NULL
)
147 strtol_error_t overflow
;
149 if (strchr (valid_suffixes
, **p
) == NULL
)
152 return err
| LONGINT_INVALID_SUFFIX_CHAR
;
171 if (strchr (valid_suffixes
, '0') != NULL
)
173 /* The "valid suffix" '0' is a special flag meaning that
174 an optional second suffix is allowed, which can change
175 the base. A suffix "B" (e.g. "100MB") stands for a power
176 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
177 a power of 1024. If no suffix (e.g. "100M"), assume
188 case 'D': /* 'D' is obsolescent */
204 overflow
= bkm_scale (&tmp
, 512);
208 /* This obsolescent first suffix is distinct from the 'B'
209 second suffix above. E.g., 'tar -L 1000B' means change
210 the tape after writing 1000 KiB of data. */
211 overflow
= bkm_scale (&tmp
, 1024);
215 overflow
= LONGINT_OK
;
218 case 'E': /* exa or exbi */
219 overflow
= bkm_scale_by_power (&tmp
, xbase
, 6);
222 case 'G': /* giga or gibi */
223 case 'g': /* 'g' is undocumented; for compatibility only */
224 overflow
= bkm_scale_by_power (&tmp
, xbase
, 3);
229 overflow
= bkm_scale_by_power (&tmp
, xbase
, 1);
232 case 'M': /* mega or mebi */
233 case 'm': /* 'm' is undocumented; for compatibility only */
234 overflow
= bkm_scale_by_power (&tmp
, xbase
, 2);
237 case 'P': /* peta or pebi */
238 overflow
= bkm_scale_by_power (&tmp
, xbase
, 5);
241 case 'Q': /* quetta or 2**100 */
242 overflow
= bkm_scale_by_power (&tmp
, xbase
, 10);
245 case 'R': /* ronna or 2**90 */
246 overflow
= bkm_scale_by_power (&tmp
, xbase
, 9);
249 case 'T': /* tera or tebi */
250 case 't': /* 't' is undocumented; for compatibility only */
251 overflow
= bkm_scale_by_power (&tmp
, xbase
, 4);
255 overflow
= bkm_scale (&tmp
, 2);
258 case 'Y': /* yotta or 2**80 */
259 overflow
= bkm_scale_by_power (&tmp
, xbase
, 8);
262 case 'Z': /* zetta or 2**70 */
263 overflow
= bkm_scale_by_power (&tmp
, xbase
, 7);
268 return err
| LONGINT_INVALID_SUFFIX_CHAR
;
274 err
|= LONGINT_INVALID_SUFFIX_CHAR
;
281 /* --------------------------------------------------------------------------------------------- */