From ac1f3857366ae2273a428f05a7c4a0ad7816509c Mon Sep 17 00:00:00 2001 From: Karel Zak 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=utf8 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 --- 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 f17d1a9752..ba2acd1c45 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -443,7 +443,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) @@ -455,13 +455,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.47.2