]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: allow -w18446744073709551616
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 23 Aug 2015 01:30:36 +0000 (18:30 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 23 Aug 2015 01:31:13 +0000 (18:31 -0700)
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

index fe95a46679d128ff167b7c55df5d02356c5a1ae1..72e4af6c794513567b7bba408172516cb3a540e1 100644 (file)
--- 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':