diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-20 22:12:33 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-20 22:12:33 +0100 |
commit | b03f5162ac239c3743cfac246b3760b0020f4d23 (patch) | |
tree | f8962ca3b8485d4ef787b4b23e8fb5c67d811003 | |
parent | dd40b40ee59e1eae6a70265741657bc55d960cce (diff) | |
download | busybox-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.c | 6 | ||||
-rwxr-xr-x | testsuite/cut.tests | 35 |
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 |
97 | optional FEATURE_CUT_REGEX | 97 | optional FEATURE_CUT_REGEX |
98 | testing "cut -DF" "cut -DF 2,7,5" \ | 98 | testing "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 | "\ |
100 | said and your | ||
101 | are | ||
102 | is demand. supply | ||
103 | forecast : | ||
104 | you you better, | ||
105 | |||
106 | Em: 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. |
101 | Cheerios are donut seeds. | 109 | Cheerios are donut seeds. |
102 | Talk is cheap because supply exceeds demand. | 110 | Talk is cheap because supply exceeds demand. |
@@ -105,6 +113,29 @@ Apple: you can buy better, but you can't pay more. | |||
105 | Subcalifragilisticexpialidocious. | 113 | Subcalifragilisticexpialidocious. |
106 | Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy." | 114 | Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy." |
107 | 115 | ||
116 | # No delimiter found: print entire line regardless of -F RANGES | ||
117 | testing "cut -F1" "cut -d: -F1" \ | ||
118 | "the_only_field\n" "" \ | ||
119 | "the_only_field\n" | ||
120 | testing "cut -F2" "cut -d: -F2" \ | ||
121 | "the_only_field\n" "" \ | ||
122 | "the_only_field\n" | ||
123 | # No delimiter found and -s: skip entire line | ||
124 | testing "cut -sF1" "cut -d: -sF1" \ | ||
125 | "" "" \ | ||
126 | "the_only_field\n" | ||
127 | #^^^ the above is probably mishandled by toybox, it prints the line | ||
128 | testing "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 | ||
132 | testing "cut -DF1" "cut -d: -DF1" \ | ||
133 | "the_only_field\n" "" \ | ||
134 | "the_only_field\n" | ||
135 | testing "cut -DF2" "cut -d: -DF2" \ | ||
136 | "\n" "" \ | ||
137 | "the_only_field\n" | ||
138 | |||
108 | optional FEATURE_CUT_REGEX LONG_OPTS | 139 | optional FEATURE_CUT_REGEX LONG_OPTS |
109 | testing "cut -F preserves intermediate delimiters" \ | 140 | testing "cut -F preserves intermediate delimiters" \ |
110 | "cut --output-delimiter=: -F2,4-6,7" \ | 141 | "cut --output-delimiter=: -F2,4-6,7" \ |