aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/cut.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 72a6f2b80..0fbeff8ea 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -152,7 +152,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
152 puts(line); 152 puts(line);
153 goto next_line; 153 goto next_line;
154 } else { /* cut by fields */ 154 } else { /* cut by fields */
155 unsigned uu = 0, start = 0, end = 0, out = 0; 155 unsigned next = 0, start = 0, end = 0;
156 int first_print = 1;
156 int dcount = 0; 157 int dcount = 0;
157 158
158 /* Blank line? Check -s (later check for -s does not catch empty lines) */ 159 /* Blank line? Check -s (later check for -s does not catch empty lines) */
@@ -168,11 +169,11 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
168 if (++cl_pos >= nlists) 169 if (++cl_pos >= nlists)
169 break; 170 break;
170 if (option_mask32 & OPT_NOSORT) 171 if (option_mask32 & OPT_NOSORT)
171 start = dcount = uu = 0; 172 start = dcount = next = 0;
172 end = 0; 173 end = 0;
173 } 174 }
174 /* End of current line? */ 175 /* End of current line? */
175 if (uu == linelen) { 176 if (next == linelen) {
176 /* If we've seen no delimiters, check -s */ 177 /* If we've seen no delimiters, check -s */
177 if (cl_pos == 0 && dcount == 0 && !opt_REGEX) { 178 if (cl_pos == 0 && dcount == 0 && !opt_REGEX) {
178 if (option_mask32 & OPT_SUPPRESS) 179 if (option_mask32 & OPT_SUPPRESS)
@@ -185,31 +186,36 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
185 if (opt_REGEX) { 186 if (opt_REGEX) {
186 regmatch_t rr = {-1, -1}; 187 regmatch_t rr = {-1, -1};
187 188
188 if (!regexec(&reg, line + uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) { 189 if (!regexec(&reg, line + next, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
189 end = uu + rr.rm_so; 190 end = next + rr.rm_so;
190 uu += rr.rm_eo; 191 next += rr.rm_eo;
191 } else { 192 } else {
192 uu = linelen; 193 next = linelen;
193 continue; 194 continue;
194 } 195 }
195 } else { 196 } else {
196 end = uu++; 197 end = next++;
197 if (line[end] != *delim) 198 if (line[end] != *delim)
198 continue; 199 continue;
199 } 200 }
200 201
201 /* Got delimiter. Loop if not yet within range. */ 202 /* Got delimiter. Loop if not yet within range. */
202 if (dcount++ < cut_lists[cl_pos].startpos) { 203 if (dcount++ < cut_lists[cl_pos].startpos) {
203 start = uu; 204 start = next;
204 continue; 205 continue;
205 } 206 }
206 } 207 }
207 if (end != start || !opt_REGEX) 208 if (end != start || !opt_REGEX) {
208 printf("%s%.*s", out++ ? odelim : "", end - start, line + start); 209 if (first_print) {
209 start = uu; 210 first_print = 0;
211 printf("%.*s", end - start, line + start);
212 } else
213 printf("%s%.*s", odelim, end - start, line + start);
214 }
215 start = next;
210 if (dcount == 0) 216 if (dcount == 0)
211 break; 217 break;
212 } 218 } /* byte loop */
213 } 219 }
214 /* if we printed anything, finish with newline */ 220 /* if we printed anything, finish with newline */
215 putchar('\n'); 221 putchar('\n');