From 6dd30a72e7074493152e8ef9c76759218f489985 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 2 Aug 2023 12:57:37 +0200
Subject: [PATCH] column: fix -l
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The original implementation is complicated and broken.

It's possible to calculate the rest of the string (for the last
column) from the current position rather than calculate it
continuously. Use the last wcstok() result also means that it will
work as expected independently on "greedy" mode (skips repeating
separators.

 # printf 'a   b c d\n1 2 3 4\n' | ./column -t -o '|' -l3
 a|b|c d
 1|2|3 4

(see space between 'a' and 'b' on input)

References: 8ac75e31de0ece74515e98e0b22e54cc0a9808bd
Fixes: https://github.com/util-linux/util-linux/issues/1763
Signed-off-by: Karel Zak <kzak@redhat.com>

Reference:https://github.com/util-linux/util-linux/commit/6dd30a72e7074493152e8ef9c76759218f489985
Conflict:NA
---
 text-utils/column.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/text-utils/column.c b/text-utils/column.c
index 88d46b9..79245cd 100644
--- a/text-utils/column.c
+++ b/text-utils/column.c
@@ -471,37 +471,33 @@ 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, skip = 0, len;
+	wchar_t *sv = NULL, *wcs = wcs0, *all = NULL;
+	size_t n = 0;
 	struct libscols_line *ln = NULL;
 
+
 	if (!ctl->tab)
 		init_table(ctl);
 
-	len = wcslen(wcs0);
+	if (ctl->maxncols) {
+		all = wcsdup(wcs0);
+		if (!all)
+			err(EXIT_FAILURE, _("failed to allocate input line"));
+	}
 
 	do {
 		char *data;
+		wchar_t *wcdata = local_wcstok(ctl, wcs, &sv);
+
+		if (!wcdata)
+			break;
 
 		if (ctl->maxncols && n + 1 == ctl->maxncols) {
-			if (nchars + skip < len)
-				wcdata = wcs0 + (nchars + skip);
-			else
-				wcdata = NULL;
-		} 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;
+			/* Use rest of the string as column data */
+			size_t skip = wcdata - wcs0;
+			wcdata = all + skip;
 		}
 
-		if (!wcdata)
-			break;
 		if (scols_table_get_ncols(ctl->tab) < n + 1) {
 			if (scols_table_is_json(ctl->tab) && !ctl->hide_unnamed)
 				errx(EXIT_FAILURE, _("line %zu: for JSON the name of the "
@@ -517,8 +513,6 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
 				err(EXIT_FAILURE, _("failed to allocate output line"));
 		}
 
-		nchars += wcslen(wcdata) + 1;
-
 		data = wcs_to_mbs(wcdata);
 		if (!data)
 			err(EXIT_FAILURE, _("failed to allocate output data"));
@@ -530,6 +524,7 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
 			break;
 	} while (1);
 
+	free(all);
 	return 0;
 }
 
-- 
2.33.0

