aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/cut.c20
-rwxr-xr-xtestsuite/cut.tests9
2 files changed, 24 insertions, 5 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index d129f9b9d..b7fe11126 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -152,11 +152,18 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
152 unsigned uu = 0, start = 0, end = 0, out = 0; 152 unsigned uu = 0, start = 0, end = 0, out = 0;
153 int dcount = 0; 153 int dcount = 0;
154 154
155 /* Blank line? Check -s (later check for -s does not catch empty lines) */
156 if (linelen == 0) {
157 if (option_mask32 & CUT_OPT_SUPPRESS_FLGS)
158 goto next_line;
159 }
160
155 /* Loop through bytes, finding next delimiter */ 161 /* Loop through bytes, finding next delimiter */
156 for (;;) { 162 for (;;) {
157 /* End of current range? */ 163 /* End of current range? */
158 if (end == linelen || dcount > cut_lists[cl_pos].endpos) { 164 if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
159 if (++cl_pos >= nlists) break; 165 if (++cl_pos >= nlists)
166 break;
160 if (option_mask32 & CUT_OPT_NOSORT_FLGS) 167 if (option_mask32 & CUT_OPT_NOSORT_FLGS)
161 start = dcount = uu = 0; 168 start = dcount = uu = 0;
162 end = 0; 169 end = 0;
@@ -175,15 +182,18 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
175 if (shoe) { 182 if (shoe) {
176 regmatch_t rr = {-1, -1}; 183 regmatch_t rr = {-1, -1};
177 184
178 if (!regexec(&reg, line+uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) { 185 if (!regexec(&reg, line + uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
179 end = uu + rr.rm_so; 186 end = uu + rr.rm_so;
180 uu += rr.rm_eo; 187 uu += rr.rm_eo;
181 } else { 188 } else {
182 uu = linelen; 189 uu = linelen;
183 continue; 190 continue;
184 } 191 }
185 } else if (line[end = uu++] != *delim) 192 } else {
186 continue; 193 end = uu++;
194 if (line[end] != *delim)
195 continue;
196 }
187 197
188 /* Got delimiter. Loop if not yet within range. */ 198 /* Got delimiter. Loop if not yet within range. */
189 if (dcount++ < cut_lists[cl_pos].startpos) { 199 if (dcount++ < cut_lists[cl_pos].startpos) {
@@ -192,7 +202,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
192 } 202 }
193 } 203 }
194 if (end != start || !shoe) 204 if (end != start || !shoe)
195 printf("%s%.*s", out++ ? odelim : "", end-start, line + start); 205 printf("%s%.*s", out++ ? odelim : "", end - start, line + start);
196 start = uu; 206 start = uu;
197 if (!dcount) 207 if (!dcount)
198 break; 208 break;
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
65testing "cut with -d -f(a) -s" "cut -da -f3 -s input" "n\nsium:Jim\n\ncion:Ed\n" "$input" "" 65testing "cut with -d -f(a) -s" "cut -da -f3 -s input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
66testing "cut with -d -f(a) -s -n" "cut -da -f3 -s -n input" "n\nsium:Jim\n\ncion:Ed\n" "$input" "" 66testing "cut with -d -f(a) -s -n" "cut -da -f3 -s -n input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
67 67
68input="\
69
70foo bar baz
71
72bing bong boop
73
74"
75testing "cut with -d -s omits blank lines" "cut -d' ' -f2 -s input" "bar\nbong\n" "$input" ""
76
68# substitute for awk 77# substitute for awk
69optional FEATURE_CUT_REGEX 78optional FEATURE_CUT_REGEX
70testing "cut -DF" "cut -DF 2,7,5" \ 79testing "cut -DF" "cut -DF 2,7,5" \