From 8ac75e31de0ece74515e98e0b22e54cc0a9808bd Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 4 Aug 2022 10:13:49 +0200
Subject: [PATCH] column: fix greedy mode on -l
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In the 'greedy' mode strtok() skips leading white chars, but code for
-l (merge remaining data to the last column) do not count the skipped
chars.

Old version:

$ printf '  a b c\n1 2 3\n' | column -t -o '-' -l2
a-a
1-2 3

Fixed version:

$ printf '  a b c\n1 2 3\n' | column -t -o '-' -l2
a-b c
1-2 3

Note, see leading white chars '  a b c'.

Fexes: https://github.com/util-linux/util-linux/issues/1763
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 text-utils/column.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/text-utils/column.c b/text-utils/column.c
index 3d068b08d..0a891117c 100644
--- a/text-utils/column.c
+++ b/text-utils/column.c
@@ -507,7 +507,7 @@ static void modify_table(struct column_control *ctl)
 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
 {
 	wchar_t *wcdata, *sv = NULL, *wcs = wcs0;
-	size_t n = 0, nchars = 0, len;
+	size_t n = 0, nchars = 0, skip = 0, len;
 	struct libscols_line *ln = NULL;
 
 	if (!ctl->tab)
@@ -519,13 +519,22 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
 		char *data;
 
 		if (ctl->maxncols && n + 1 == ctl->maxncols) {
-			if (nchars < len)
-				wcdata = wcs0 + nchars;
+			if (nchars + skip < len)
+				wcdata = wcs0 + (nchars + skip);
 			else
 				wcdata = NULL;
-		} else
+		} else {
 			wcdata = local_wcstok(ctl, wcs, &sv);
 
+			/* For the default separator ('greedy' mode) it uses
+			 * strtok() and it skips leading white chars. In this
+			 * case we need to remember size of the ignored white
+			 * chars due to wcdata calculation in maxncols case */
+			if (wcdata && ctl->greedy
+			    && n == 0 && nchars == 0 && wcdata > wcs)
+				skip = wcdata - wcs;
+		}
+
 		if (!wcdata)
 			break;
 		if (scols_table_get_ncols(ctl->tab) < n + 1) {
-- 
2.33.0

