>From 44299677c2d72621729acfe56feac346e25352b5 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 22 Aug 2015 18:30:36 -0700 Subject: [PATCH] ls: allow -w18446744073709551616 Problem reported by Beco in: http://bugs.gnu.org/21325 * src/ls.c (set_line_length): New function. (decode_switches): Use it to decode COLUMNS and -w. --- src/ls.c | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/ls.c b/src/ls.c index fe95a46..72e4af6 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1523,6 +1523,31 @@ main (int argc, char **argv) return exit_status; } +/* Set the line length to the value given by SPEC. Return true if + successful. */ + +static bool +set_line_length (char const *spec) +{ + uintmax_t val; + + /* Treat too-large values as if they were SIZE_MAX, which is + effectively infinity. */ + switch (xstrtoumax (spec, NULL, 0, &val, "")) + { + case LONGINT_OK: + line_length = MIN (val, SIZE_MAX); + return true; + + case LONGINT_OVERFLOW: + line_length = SIZE_MAX; + return true; + + default: + return false; + } +} + /* Set all the option flags according to the switches specified. Return the index of the first non-option argument. */ @@ -1591,21 +1616,10 @@ decode_switches (int argc, char **argv) line_length = 80; { char const *p = getenv ("COLUMNS"); - if (p && *p) - { - unsigned long int tmp_ulong; - if (xstrtoul (p, NULL, 0, &tmp_ulong, NULL) == LONGINT_OK - && 0 < tmp_ulong && tmp_ulong <= SIZE_MAX) - { - line_length = tmp_ulong; - } - else - { - error (0, 0, - _("ignoring invalid width in environment variable COLUMNS: %s"), - quotearg (p)); - } - } + if (p && *p && ! set_line_length (p)) + error (0, 0, + _("ignoring invalid width in environment variable COLUMNS: %s"), + quotearg (p)); } #ifdef TIOCGWINSZ @@ -1749,8 +1763,9 @@ decode_switches (int argc, char **argv) break; case 'w': - line_length = xnumtoumax (optarg, 0, 1, SIZE_MAX, "", - _("invalid line width"), LS_FAILURE); + if (! set_line_length (optarg)) + error (LS_FAILURE, 0, "%s: %s", _("invalid line width"), + quote (optarg)); break; case 'x': -- 2.1.0