summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-04-11 17:59:28 +0000
committerJim Meyering <jim@meyering.net>1999-04-11 17:59:28 +0000
commitf3df8336aed453931dc358bdc67b22a923f844a9 (patch)
treeca75693af4ebea9c3122516718f152b97e6b14f9
parenta66cc3009b701b91af4de6b646d0bdfabaa174db (diff)
downloadcoreutils-f3df8336aed453931dc358bdc67b22a923f844a9.tar.xz
Fix the problem whereby `yes > k & tail -1 k' would infloop.
(dump_remainder): Move this function to precede the new use in file_lines. (tail_lines): Don't call dump_remainder here. (file_lines): Call dump_remainder here instead.
-rw-r--r--src/tail.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/src/tail.c b/src/tail.c
index 0e0d0e8b5..d5399803a 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -261,6 +261,28 @@ write_header (const char *pretty_filename, const char *comment)
first_file = 0;
}
+/* Display file PRETTY_FILENAME from the current position in FD to the end.
+ Return the number of bytes read from the file. */
+
+static long
+dump_remainder (const char *pretty_filename, int fd)
+{
+ char buffer[BUFSIZ];
+ int bytes_read;
+ long total;
+
+ total = 0;
+ while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
+ {
+ xwrite (STDOUT_FILENO, buffer, bytes_read);
+ total += bytes_read;
+ }
+ if (bytes_read == -1)
+ error (EXIT_FAILURE, errno, "%s", pretty_filename);
+
+ return total;
+}
+
/* Print the last N_LINES lines from the end of file FD.
Go backward through the file, reading `BUFSIZ' bytes at a time (except
probably the first), until we hit the start of the file or have
@@ -321,6 +343,7 @@ file_lines (const char *pretty_filename, int fd, long int n_lines, off_t pos)
/* Not enough lines in the file; print the entire file. */
/* FIXME: check lseek return value */
lseek (fd, (off_t) 0, SEEK_SET);
+ dump_remainder (pretty_filename, fd);
return 0;
}
pos -= BUFSIZ;
@@ -603,28 +626,6 @@ start_lines (const char *pretty_filename, int fd, long int n_lines)
return 0;
}
-/* Display file PRETTY_FILENAME from the current position in FD to the end.
- Return the number of bytes read from the file. */
-
-static long
-dump_remainder (const char *pretty_filename, int fd)
-{
- char buffer[BUFSIZ];
- int bytes_read;
- long total;
-
- total = 0;
- while ((bytes_read = safe_read (fd, buffer, BUFSIZ)) > 0)
- {
- xwrite (STDOUT_FILENO, buffer, bytes_read);
- total += bytes_read;
- }
- if (bytes_read == -1)
- error (EXIT_FAILURE, errno, "%s", pretty_filename);
-
- return total;
-}
-
/* FIXME: describe */
static void
@@ -962,7 +963,6 @@ tail_lines (const char *pretty_filename, int fd, long int n_lines)
length = lseek (fd, (off_t) 0, SEEK_END);
if (length != 0 && file_lines (pretty_filename, fd, n_lines, length))
return 1;
- dump_remainder (pretty_filename, fd);
}
else
return pipe_lines (pretty_filename, fd, n_lines);