]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: fix too few display columns edge case
authorPádraig Brady <P@draigBrady.com>
Mon, 4 May 2026 14:34:01 +0000 (15:34 +0100)
committerPádraig Brady <P@draigBrady.com>
Mon, 4 May 2026 22:57:48 +0000 (23:57 +0100)
In the edge case where the right most column would
consist of only files taking 1 or 2 cells, we accounted
for 3 cells, which would result in using 1 column too few.
This should have been part of commit v8.24-59-ge71be1292

* src/ls.c (init_column_info): Don't account for separator
in the the final column.
* tests/ls/w-option.sh: Add a test case.

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

index 7dbcfa75d08d44a0dbd4c63a88195d25d2bb0155..fa7926f86afa024b40c1a65862db8c83f7185eb3 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -993,8 +993,8 @@ static struct column_info *column_info;
 /* Maximum number of columns ever possible for this display.  */
 static size_t max_idx;
 
-/* The minimum width of a column is 3: 1 character for the name and 2
-   for the separating white space.  */
+/* The minimum width of a non-final column is 3: 1 character for the name
+   and 2 for the separating white space.  The final column is not padded.  */
 enum { MIN_COLUMN_WIDTH = 3 };
 
 
@@ -5276,9 +5276,10 @@ init_column_info (idx_t max_cols)
   for (idx_t i = 0; i < max_cols; ++i)
     {
       column_info[i].valid_len = true;
-      column_info[i].line_len = (i + 1) * MIN_COLUMN_WIDTH;
-      for (idx_t j = 0; j <= i; ++j)
+      column_info[i].line_len = i * MIN_COLUMN_WIDTH + 1;
+      for (idx_t j = 0; j < i; ++j)
         column_info[i].col_arr[j] = MIN_COLUMN_WIDTH;
+      column_info[i].col_arr[i] = 1;
     }
 }
 
index bf10d6e578a2f9b9752821100fa3a7a6325e9b55..288f9a571d5a5a2d2aa4d1adfb88723e562eaa67 100755 (executable)
@@ -45,4 +45,21 @@ TERM=xterm ls -w0 -x --color=always || fail=1
 ls -w4 -x -T0 a b > out || fail=1
 compare exp out || fail=1
 
+# coreutils <= 9.11 could display 1 column too few
+touch aa c || framework_failure_
+cat <<\EOF > exp || framework_failure_
+aa  b
+c
+EOF
+ls -w6 -x -T0 aa b c > out || fail=1
+compare exp out || fail=1
+
+# coreutils <= 9.11 could display 1 column too few
+cat <<\EOF > exp || framework_failure_
+aa  c
+b
+EOF
+ls -w6 -C -T0 aa b c > out || fail=1
+compare exp out || fail=1
+
 Exit $fail