]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: fix -l
authorKarel Zak <kzak@redhat.com>
Wed, 2 Aug 2023 10:57:37 +0000 (12:57 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 2 Aug 2023 10:57:37 +0000 (12:57 +0200)
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>
text-utils/column.c

index ad3d8f3d7f0c416c0b86a3d58d7b1cb7c4c1d768..6e6d13310f12ef0af6c26a0191b17be0ba110961 100644 (file)
@@ -513,37 +513,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 "
@@ -559,8 +555,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"));
@@ -572,6 +566,7 @@ static int add_line_to_table(struct column_control *ctl, wchar_t *wcs0)
                        break;
        } while (1);
 
+       free(all);
        return 0;
 }