From: Paul Eggert Date: Fri, 24 Jun 2022 15:59:09 +0000 (-0500) Subject: shuf: better diagnostic for ‘shuf -i -10-10’ X-Git-Tag: v9.2~179 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f71f478ec6929d323c17f0482db8791de87b4fd;p=thirdparty%2Fcoreutils.git shuf: better diagnostic for ‘shuf -i -10-10’ * 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’”. --- 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;