From 199606e960942c29fd8085be812edd3d3697825c Mon Sep 17 00:00:00 2001
From: Colin McAllister <colinmca242@gmail.com>
Date: Wed, 17 Jul 2024 07:58:52 -0500
Subject: [PATCH 1/1] cut: Fix "-s" flag to omit blank lines

Using cut with the delimiter flag ("-d") with the "-s" flag to only
output lines containing the delimiter will print blank lines. This is
deviant behavior from cut provided by GNU Coreutils. Blank lines should
be omitted if "-s" is used with "-d".

This change introduces a somewhat naiive, yet efficient solution, where
line length is checked before looping though bytes. If line length is
zero and the "-s" flag is used, the code will jump to parsing the next
line to avoid printing a newline character.

In addition, a test to cut.tests has been added to ensure that this
regression is fixed and will not happen again in the future.

Upstream-Status: Submitted [http://lists.busybox.net/pipermail/busybox/2024-July/090834.html]

Signed-off-by: Colin McAllister <colinmca242@gmail.com>
---
 coreutils/cut.c     | 6 ++++++
 testsuite/cut.tests | 9 +++++++++
 2 files changed, 15 insertions(+)

diff --git a/coreutils/cut.c b/coreutils/cut.c
index 55bdd9386..b7f986f26 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -152,6 +152,12 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
 			unsigned uu = 0, start = 0, end = 0, out = 0;
 			int dcount = 0;
 
+			/* Blank line? */
+			if (!linelen) {
+				if (option_mask32 & CUT_OPT_SUPPRESS_FLGS)
+					goto next_line;
+			}
+
 			/* Loop through bytes, finding next delimiter */
 			for (;;) {
 				/* End of current range? */
diff --git a/testsuite/cut.tests b/testsuite/cut.tests
index 2458c019c..0b401bc00 100755
--- a/testsuite/cut.tests
+++ b/testsuite/cut.tests
@@ -65,6 +65,15 @@ testing "cut with -d -f( ) -s" "cut -d' ' -f3 -s input && echo yes" "yes\n" "$in
 testing "cut with -d -f(a) -s" "cut -da -f3 -s input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
 testing "cut with -d -f(a) -s -n" "cut -da -f3 -s -n input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
 
+input="\
+
+foo bar baz
+
+bing bong boop
+
+"
+testing "cut with -d -s omits blank lines" "cut -d' ' -f2 -s input" "bar\nbong\n" "$input" ""
+
 # substitute for awk
 optional FEATURE_CUT_REGEX
 testing "cut -DF" "cut -DF 2,7,5" \
-- 
2.43.0

