logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 77fd38a21cc0f9f4fa13a34fb86c65318254b248
parent df87041119c352b952f386079add7050967c7c4b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  7 Sep 2024 10:24:58 +0200

cmd/cut: fix output of no-delim lines

Diffstat:

Mcmd/cut.13++-
Mcmd/cut.c12+++++++-----
Mtest-cmd/cut.sh8++++----
3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/cmd/cut.1 b/cmd/cut.1 @@ -58,7 +58,8 @@ Cut fields based on .It Fl n Do not split codepoints. (Currently unsupported in this implementation) .It Fl s -Suppress lines with no delimiter characters. +Suppress lines with no delimiter characters, +otherwise whole delimiter-less lines are printed as-is. .El .Sh EXIT STATUS .Ex -std diff --git a/cmd/cut.c b/cmd/cut.c @@ -289,13 +289,15 @@ cut_f(FILE *in, const char *filename) if(line[nread - 1] == '\n') line[--nread] = '\0'; - if(opt_s) + size_t di = 0; + for(; di < (size_t)nread; di++) + if(line[di] == delim) break; + + if(di == (size_t)nread) { - size_t i = 0; - for(; i < (size_t)nread; i++) - if(line[i] == delim) break; + if(!opt_s) puts(line); - if(i == (size_t)nread) continue; + continue; } bool need_sep = false; diff --git a/test-cmd/cut.sh b/test-cmd/cut.sh @@ -46,11 +46,11 @@ t --input='aéb' 'widechar' '-c2' 'é ' fields='1 2 3 4 - +A a b c' t --input="$fields" f2 '-f2' '2 - +A b ' @@ -59,11 +59,11 @@ b ' t --input="$fields" f2- '-f2-' '2 3 4 - +A b c ' t --input="$fields" f-2 '-f-2' '1 2 - +A a b '