From 9f71f478ec6929d323c17f0482db8791de87b4fd Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 24 Jun 2022 10:59:09 -0500 Subject: [PATCH] =?utf8?q?shuf:=20better=20diagnostic=20for=20=E2=80=98shu?= =?utf8?q?f=20-i=20-10-10=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * src/shuf.c: Do not include xdectoint.h. (main): Improve diagnostic for ‘shuf -i -10-10’. Without this patch, the diagnostic was “shuf: invalid input range: ‘’” which is not helpful. Now it is “shuf: invalid input range: ‘-10-10’”. --- src/shuf.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/shuf.c b/src/shuf.c index 68ca609ebb..1c0037b7b5 100644 --- a/src/shuf.c +++ b/src/shuf.c @@ -32,7 +32,6 @@ #include "randperm.h" #include "read-file.h" #include "stdio--.h" -#include "xdectoint.h" #include "xstrtol.h" /* The official name of this program (e.g., no 'g' prefix). */ @@ -410,31 +409,37 @@ main (int argc, char **argv) case 'i': { - char *p = strchr (optarg, '-'); - char const *hi_optarg = optarg; - bool invalid = !p; - if (input_range) die (EXIT_FAILURE, 0, _("multiple -i options specified")); input_range = true; - if (p) + uintmax_t u; + char *lo_end; + strtol_error err = xstrtoumax (optarg, &lo_end, 10, &u, NULL); + if (err == LONGINT_OK) { - *p = '\0'; - lo_input = xdectoumax (optarg, 0, SIZE_MAX, "", - _("invalid input range"), 0); - *p = '-'; - hi_optarg = p + 1; + lo_input = u; + if (lo_input != u) + err = LONGINT_OVERFLOW; + else if (*lo_end != '-') + err = LONGINT_INVALID; + else + { + err = xstrtoumax (lo_end + 1, NULL, 10, &u, ""); + if (err == LONGINT_OK) + { + hi_input = u; + if (hi_input != u) + err = LONGINT_OVERFLOW; + } + } } - hi_input = xdectoumax (hi_optarg, 0, SIZE_MAX, "", - _("invalid input range"), 0); - n_lines = hi_input - lo_input + 1; - invalid |= ((lo_input <= hi_input) == (n_lines == 0)); - if (invalid) - die (EXIT_FAILURE, errno, "%s: %s", _("invalid input range"), - quote (optarg)); + + if (err != LONGINT_OK || (lo_input <= hi_input) == (n_lines == 0)) + die (EXIT_FAILURE, err == LONGINT_OVERFLOW ? EOVERFLOW : 0, + "%s: %s", _("invalid input range"), quote (optarg)); } break; -- 2.47.2