aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-12-20 22:12:33 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2024-12-20 22:12:33 +0100
commitb03f5162ac239c3743cfac246b3760b0020f4d23 (patch)
treef8962ca3b8485d4ef787b4b23e8fb5c67d811003
parentdd40b40ee59e1eae6a70265741657bc55d960cce (diff)
downloadbusybox-w32-b03f5162ac239c3743cfac246b3760b0020f4d23.tar.gz
busybox-w32-b03f5162ac239c3743cfac246b3760b0020f4d23.tar.bz2
busybox-w32-b03f5162ac239c3743cfac246b3760b0020f4d23.zip
cut: fix up -D/-s behavior with -F
function old new delta cut_main 1388 1402 +14 packed_usage 34934 34933 -1 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 14/-1) Total: 13 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/cut.c6
-rwxr-xr-xtestsuite/cut.tests35
2 files changed, 36 insertions, 5 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index a766db40f..65e0e5c30 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -43,7 +43,7 @@
43//usage: "\n -F LIST Print only these fields (-d is regex)" 43//usage: "\n -F LIST Print only these fields (-d is regex)"
44//usage: ) 44//usage: )
45//usage: "\n -s Drop lines with no delimiter (else print them in full)" 45//usage: "\n -s Drop lines with no delimiter (else print them in full)"
46//usage: "\n -D Don't sort/collate sections or match -f"IF_FEATURE_CUT_REGEX("F")" lines without delimeter" 46//usage: "\n -D Don't sort ranges; line without delimiters has one field"
47//usage: IF_LONG_OPTS( 47//usage: IF_LONG_OPTS(
48//usage: "\n --output-delimiter SEP Output field delimeter" 48//usage: "\n --output-delimiter SEP Output field delimeter"
49//usage: ) IF_NOT_LONG_OPTS( 49//usage: ) IF_NOT_LONG_OPTS(
@@ -202,8 +202,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
202 /* End of current line? */ 202 /* End of current line? */
203 if (next == linelen) { 203 if (next == linelen) {
204 end = linelen; /* print up to end */ 204 end = linelen; /* print up to end */
205 /* If we've seen no delimiters, check -s */ 205 /* If we've seen no delimiters, and no -D, check -s */
206 if (cl_pos == 0 && dcount == 0 && !opt_REGEX) { 206 if (!(option_mask32 & OPT_NOSORT) && cl_pos == 0 && dcount == 0) {
207 if (option_mask32 & OPT_SUPPRESS) 207 if (option_mask32 & OPT_SUPPRESS)
208 goto next_line; 208 goto next_line;
209 /* else: will print entire line */ 209 /* else: will print entire line */
diff --git a/testsuite/cut.tests b/testsuite/cut.tests
index e57b028ac..21cfea809 100755
--- a/testsuite/cut.tests
+++ b/testsuite/cut.tests
@@ -95,8 +95,16 @@ testing "cut with -d -s omits blank lines" "cut -d' ' -f2 -s input" "bar\nbong\n
95 95
96# substitute for awk 96# substitute for awk
97optional FEATURE_CUT_REGEX 97optional FEATURE_CUT_REGEX
98testing "cut -DF" "cut -DF 2,7,5" \ 98testing "cut -DF unordered" "cut -DF 2,7,5" \
99 "said and your\nare\nis demand. supply\nforecast :\nyou you better,\n\nEm: Took hate\n" "" \ 99 "\
100said and your
101are
102is demand. supply
103forecast :
104you you better,
105
106Em: Took hate
107" "" \
100"Bother, said Pooh. It's your husband, and he has a gun. 108"Bother, said Pooh. It's your husband, and he has a gun.
101Cheerios are donut seeds. 109Cheerios are donut seeds.
102Talk is cheap because supply exceeds demand. 110Talk is cheap because supply exceeds demand.
@@ -105,6 +113,29 @@ Apple: you can buy better, but you can't pay more.
105Subcalifragilisticexpialidocious. 113Subcalifragilisticexpialidocious.
106Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy." 114Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
107 115
116# No delimiter found: print entire line regardless of -F RANGES
117testing "cut -F1" "cut -d: -F1" \
118 "the_only_field\n" "" \
119 "the_only_field\n"
120testing "cut -F2" "cut -d: -F2" \
121 "the_only_field\n" "" \
122 "the_only_field\n"
123# No delimiter found and -s: skip entire line
124testing "cut -sF1" "cut -d: -sF1" \
125 "" "" \
126 "the_only_field\n"
127#^^^ the above is probably mishandled by toybox, it prints the line
128testing "cut -sF2" "cut -d: -sF2" \
129 "" "" \
130 "the_only_field\n"
131# -D disables special handling of lines with no delimiters, the line is treated as the 1st field
132testing "cut -DF1" "cut -d: -DF1" \
133 "the_only_field\n" "" \
134 "the_only_field\n"
135testing "cut -DF2" "cut -d: -DF2" \
136 "\n" "" \
137 "the_only_field\n"
138
108optional FEATURE_CUT_REGEX LONG_OPTS 139optional FEATURE_CUT_REGEX LONG_OPTS
109testing "cut -F preserves intermediate delimiters" \ 140testing "cut -F preserves intermediate delimiters" \
110 "cut --output-delimiter=: -F2,4-6,7" \ 141 "cut --output-delimiter=: -F2,4-6,7" \