summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2015-08-22 18:30:36 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2015-08-22 18:31:13 -0700
commit97678abb6ba0c053bcc07d3c14440bbef5269aab (patch)
treec09e4fc2471d554fa985fd0aa83beab8d2947db0
parent4ec9ccad7981e7c098125fc967a5bdb3a56a1d25 (diff)
downloadcoreutils-97678abb6ba0c053bcc07d3c14440bbef5269aab.tar.xz
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.
-rw-r--r--src/ls.c49
1 files changed, 32 insertions, 17 deletions
diff --git a/src/ls.c b/src/ls.c
index fe95a4667..72e4af6c7 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':