aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/cut.c23
-rwxr-xr-xtestsuite/cut.tests11
2 files changed, 27 insertions, 7 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 1eb4968d9..2511befc8 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -114,7 +114,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
114 } 114 }
115 } 115 }
116 free(printed); 116 free(printed);
117 } else if (!opt_REGEX && *delim == '\n') { /* cut by lines */ 117 /* Cut by lines */
118 } else if (!opt_REGEX && *delim == '\n') {
118 int spos = cut_list[cl_pos].startpos; 119 int spos = cut_list[cl_pos].startpos;
119 120
120 /* get out if we have no more lists to process or if the lines 121 /* get out if we have no more lists to process or if the lines
@@ -144,7 +145,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
144 * looking for, so print it */ 145 * looking for, so print it */
145 puts(line); 146 puts(line);
146 goto next_line; 147 goto next_line;
147 } else { /* cut by fields */ 148 /* Cut by fields */
149 } else {
148 unsigned next = 0, start = 0, end = 0; 150 unsigned next = 0, start = 0, end = 0;
149 int dcount = 0; /* Nth delimiter we saw (0 - didn't see any yet) */ 151 int dcount = 0; /* Nth delimiter we saw (0 - didn't see any yet) */
150 int first_print = 1; 152 int first_print = 1;
@@ -159,22 +161,33 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
159 for (;;) { 161 for (;;) {
160 /* End of current range? */ 162 /* End of current range? */
161 if (end == linelen || dcount > cut_list[cl_pos].endpos) { 163 if (end == linelen || dcount > cut_list[cl_pos].endpos) {
164 end_of_range:
162 if (++cl_pos >= nlists) 165 if (++cl_pos >= nlists)
163 break; 166 break;
164 if (option_mask32 & OPT_NOSORT) 167 if (option_mask32 & OPT_NOSORT)
165 start = dcount = next = 0; 168 start = dcount = next = 0;
166 end = 0; /* (why?) */ 169 end = 0; /* (why?) */
170 //bb_error_msg("End of current range");
167 } 171 }
168 /* End of current line? */ 172 /* End of current line? */
169 if (next == linelen) { 173 if (next == linelen) {
174 end = linelen; /* print up to end */
170 /* If we've seen no delimiters, check -s */ 175 /* If we've seen no delimiters, check -s */
171 if (cl_pos == 0 && dcount == 0 && !opt_REGEX) { 176 if (cl_pos == 0 && dcount == 0 && !opt_REGEX) {
172 if (option_mask32 & OPT_SUPPRESS) 177 if (option_mask32 & OPT_SUPPRESS)
173 goto next_line; 178 goto next_line;
174 /* else: will print entire line */ 179 /* else: will print entire line */
175 } else if (dcount < cut_list[cl_pos].startpos) 180 } else if (dcount < cut_list[cl_pos].startpos) {
176 start = linelen; /* do not print */ 181 /* echo 1.2 | cut -d. -f1,3: prints "1", not "1." */
177 end = linelen; /* print up to end */ 182 //break;
183 /* ^^^ this fails a case with -D:
184 * echo 1 2 | cut -DF 1,3,2:
185 * do not end line processing when didn't find field#3
186 */
187 //if (option_mask32 & OPT_NOSORT) - no, just do it always
188 goto end_of_range;
189 }
190 //bb_error_msg("End of current line: s:%d e:%d", start, end);
178 } else { 191 } else {
179 /* Find next delimiter */ 192 /* Find next delimiter */
180#if ENABLE_FEATURE_CUT_REGEX 193#if ENABLE_FEATURE_CUT_REGEX
diff --git a/testsuite/cut.tests b/testsuite/cut.tests
index c335f824b..46ef545d7 100755
--- a/testsuite/cut.tests
+++ b/testsuite/cut.tests
@@ -90,7 +90,14 @@ Subcalifragilisticexpialidocious.
90Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy." 90Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
91SKIP= 91SKIP=
92 92
93testing "cut empty field" "cut -d ':' -f 1-3" "a::b\n" "" "a::b\n" 93testing "cut empty field" "cut -d ':' -f 1-3" \
94testing "cut empty field 2" "cut -d ':' -f 3-5" "b::c\n" "" "a::b::c:d\n" 94 "a::b\n" \
95 "" "a::b\n"
96testing "cut empty field 2" "cut -d ':' -f 3-5" \
97 "b::c\n" \
98 "" "a::b::c:d\n"
99testing "cut non-existing field" "cut -d ':' -f1,3" \
100 "1\n" \
101 "" "1:\n"
95 102
96exit $FAILCOUNT 103exit $FAILCOUNT