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
++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. */
+ }
}
}
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;