diff options
Diffstat (limited to 'gl')
-rw-r--r-- | gl/lib/xdectoimax.c | 6 | ||||
-rw-r--r-- | gl/lib/xdectoint.c | 85 | ||||
-rw-r--r-- | gl/lib/xdectoint.h | 36 | ||||
-rw-r--r-- | gl/lib/xdectoumax.c | 6 | ||||
-rw-r--r-- | gl/modules/xdectoint | 29 |
5 files changed, 162 insertions, 0 deletions
diff --git a/gl/lib/xdectoimax.c b/gl/lib/xdectoimax.c new file mode 100644 index 000000000..d4bb18fa1 --- /dev/null +++ b/gl/lib/xdectoimax.c @@ -0,0 +1,6 @@ +#define __xdectoint xdectoimax +#define __xnumtoint xnumtoimax +#define __xdectoint_t intmax_t +#define __xstrtol xstrtoimax +#define __xdectoint_signed 1 +#include "xdectoint.c" diff --git a/gl/lib/xdectoint.c b/gl/lib/xdectoint.c new file mode 100644 index 000000000..2164d1ef4 --- /dev/null +++ b/gl/lib/xdectoint.c @@ -0,0 +1,85 @@ +/* Convert decimal strings with bounds checking and exit on error. + + Copyright (C) 2014 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include "xdectoint.h" + +#include <errno.h> +#include <inttypes.h> +#include <stdlib.h> + +#include "error.h" +#include "quote.h" +#include "xstrtol.h" + +/* Parse numeric string N_STR of base BASE, and return the value. + Exit on parse error or if MIN or MAX are exceeded. + Strings can have multiplicative SUFFIXES if specified. + ERR is printed along with N_STR on error. */ + +__xdectoint_t +__xnumtoint (const char *n_str, int base, __xdectoint_t min, __xdectoint_t max, + const char *suffixes, const char *err, int err_exit) +{ + strtol_error s_err; + + __xdectoint_t tnum; + s_err = __xstrtol (n_str, NULL, base, &tnum, suffixes); + + if (s_err == LONGINT_OK) + { + if (tnum < min || max < tnum) + { + s_err = LONGINT_OVERFLOW; + /* Use have the INT range as a heuristic to distinguish + type overflow rather than other min/max limits. */ + if (tnum > INT_MAX/2) + errno = EOVERFLOW; +#if __xdectoint_signed + else if (tnum < INT_MIN/2) + errno = EOVERFLOW; +#endif + else + errno = ERANGE; + } + } + else if (s_err == LONGINT_OVERFLOW) + errno = EOVERFLOW; + else if (s_err == LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW) + errno = 0; /* Don't show ERANGE errors for invalid numbers. */ + + if (s_err != LONGINT_OK) + { + error (err_exit ? err_exit : EXIT_FAILURE, errno, + "%s: %s", err, quote (n_str)); + } + + return tnum; +} + +/* Parse decimal string N_STR, and return the value. + Exit on parse error or if MIN or MAX are exceeded. + Strings can have multiplicative SUFFIXES if specified. + ERR is printed along with N_STR on error. */ + +__xdectoint_t +__xdectoint (const char *n_str, __xdectoint_t min, __xdectoint_t max, + const char *suffixes, const char *err, int err_exit) +{ + return __xnumtoint (n_str, 10, min, max, suffixes, err, err_exit); +} diff --git a/gl/lib/xdectoint.h b/gl/lib/xdectoint.h new file mode 100644 index 000000000..770e18571 --- /dev/null +++ b/gl/lib/xdectoint.h @@ -0,0 +1,36 @@ +/* Convert decimal strings with bounds checking and exit on error. + + Copyright (C) 2014 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef XDECTOINT_H_ +# define XDECTOINT_H_ 1 + +# include <inttypes.h> + +# define _DECLARE_XDECTOINT(name, type) \ + type name (const char *n_str, type min, type max, \ + const char *suffixes, const char *err, int err_exit); +# define _DECLARE_XNUMTOINT(name, type) \ + type name (const char *n_str, int base, type min, type max, \ + const char *suffixes, const char *err, int err_exit); + +_DECLARE_XDECTOINT (xdectoimax, intmax_t) +_DECLARE_XDECTOINT (xdectoumax, uintmax_t) + +_DECLARE_XNUMTOINT (xnumtoimax, intmax_t) +_DECLARE_XNUMTOINT (xnumtoumax, uintmax_t) + +#endif /* not XDECTOINT_H_ */ diff --git a/gl/lib/xdectoumax.c b/gl/lib/xdectoumax.c new file mode 100644 index 000000000..412ddf962 --- /dev/null +++ b/gl/lib/xdectoumax.c @@ -0,0 +1,6 @@ +#define __xdectoint xdectoumax +#define __xnumtoint xnumtoumax +#define __xdectoint_t uintmax_t +#define __xstrtol xstrtoumax +#define __xdectoint_signed 0 +#include "xdectoint.c" diff --git a/gl/modules/xdectoint b/gl/modules/xdectoint new file mode 100644 index 000000000..cbd372666 --- /dev/null +++ b/gl/modules/xdectoint @@ -0,0 +1,29 @@ +Description: +Convert decimal string to '[u]intmax_t', with bounds checking and exit on error + +Files: +lib/xdectoint.h +lib/xdectoint.c +lib/xdectoimax.c +lib/xdectoumax.c + +Depends-on: +error +errno +quote +xstrtoimax +xstrtoumax + +configure.ac: + +Makefile.am: +lib_SOURCES += xdectoimax.c xdectoumax.c + +Include: +"xdectoint.h" + +License: +GPL + +Maintainer: +all |