From: Karel Zak Date: Thu, 4 Aug 2022 08:13:49 +0000 (+0200) Subject: column: fix greedy mode on -l X-Git-Tag: v2.38.1~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac1f3857366ae2273a428f05a7c4a0ad7816509c;p=thirdparty%2Futil-linux.git column: fix greedy mode on -l 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 --- 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) {