]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: fix off by one error when determining max display columns
authorPádraig Brady <P@draigBrady.com>
Wed, 21 Oct 2015 12:57:41 +0000 (13:57 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 21 Oct 2015 15:13:57 +0000 (16:13 +0100)
* src/ls.c (main): Account for the first column not including
a separator when calculating max_idx.
* tests/ls/w-option.sh: Add a test case.
* NEWS: Mention the bug fix.

NEWS
src/ls.c
tests/ls/w-option.sh

diff --git a/NEWS b/NEWS
index 07b88b09cb49361e4897d816d97901018683deef..e7715856252e18345cd23edefe9522ba211188dc 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Bug fixes
 
+  ls no longer prematurely wraps lines when printing short file names.
+  [bug introduced in 5.1.0]
+
   shred again uses defined patterns for all iteration counts.
   [bug introduced in coreutils-5.93]
 
index 0c9dc78db411a780e8429ad36f7df88805b28054..ef372553f181eb14d08f1617b46694dd4a55d920 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -1979,7 +1979,11 @@ decode_switches (int argc, char **argv)
         }
     }
 
-  max_idx = MAX (1, line_length / MIN_COLUMN_WIDTH);
+  /* Determine the max possible number of display columns.  */
+  max_idx = line_length / MIN_COLUMN_WIDTH;
+  /* Account for first display column not having a separator,
+     or line_lengths shorter than MIN_COLUMN_WIDTH.  */
+  max_idx += line_length % MIN_COLUMN_WIDTH != 0;
 
   filename_quoting_options = clone_quoting_options (NULL);
   if (get_quoting_style (filename_quoting_options) == escape_quoting_style)
index f49c02815d8cb890f811f3c5e8e6e27241f504f2..6361aaf597eafd828bcf48ddecd4d9cef32f26b9 100755 (executable)
@@ -41,4 +41,8 @@ compare exp out || fail=1
 # Ensure that 0 line length doesn't cause division by zero
 TERM=xterm ls -w0 -x --color=always || fail=1
 
+# coreutils <= 8.24 could display 1 column too few
+ls -w4 -x -T0 a b > out || fail=1
+compare exp out || fail=1
+
 Exit $fail