diff options
Diffstat (limited to 'src/cut.c')
-rw-r--r-- | src/cut.c | 35 |
1 files changed, 22 insertions, 13 deletions
@@ -526,10 +526,16 @@ cut_fields (FILE *stream) unsigned int field_idx; int found_any_selected_field; int buffer_first_field; + int empty_input; found_any_selected_field = 0; field_idx = 1; + c = getc (stream); + empty_input = (c == EOF); + if (c != EOF) + ungetc (c, stream); + /* To support the semantics of the -s flag, we may have to buffer all of the first field to determine whether it is `delimited.' But that is unnecessary if all non-delimited lines must be printed @@ -577,22 +583,25 @@ cut_fields (FILE *stream) ++field_idx; } - if (print_kth (field_idx)) + if (c != EOF) { - if (found_any_selected_field) - putchar (delim); - found_any_selected_field = 1; - - while ((c = getc (stream)) != delim && c != '\n' && c != EOF) + if (print_kth (field_idx)) { - putchar (c); + if (found_any_selected_field) + putchar (delim); + found_any_selected_field = 1; + + while ((c = getc (stream)) != delim && c != '\n' && c != EOF) + { + putchar (c); + } } - } - else - { - while ((c = getc (stream)) != delim && c != '\n' && c != EOF) + else { - /* Empty. */ + while ((c = getc (stream)) != delim && c != '\n' && c != EOF) + { + /* Empty. */ + } } } @@ -611,7 +620,7 @@ cut_fields (FILE *stream) else if (c == '\n' || c == EOF) { if (found_any_selected_field - || !(suppress_non_delimited && field_idx == 1)) + || (!empty_input && !(suppress_non_delimited && field_idx == 1))) putchar ('\n'); if (c == EOF) break; |