summaryrefslogtreecommitdiff
path: root/src/cut.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1997-06-14 17:31:40 +0000
committerJim Meyering <jim@meyering.net>1997-06-14 17:31:40 +0000
commit2f1768f67675dc38f54b20c0122ec1f2944a994d (patch)
treefad6361177f7898a38630909c1a73139bd36b290 /src/cut.c
parent4f0bef5377ff8b9f0d664ff2f4f8347e5ab00584 (diff)
downloadcoreutils-2f1768f67675dc38f54b20c0122ec1f2944a994d.tar.xz
(cut_fields): Detect when the input is empty and handle
that special case. Before `cut -f1 </dev/null' would improperly output a single newline. Reported by Phil Richards.
Diffstat (limited to 'src/cut.c')
-rw-r--r--src/cut.c35
1 files changed, 22 insertions, 13 deletions
diff --git a/src/cut.c b/src/cut.c
index 549a0e342..a9cd23154 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -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;