From: Jim Meyering Date: Sun, 11 Apr 1999 17:59:28 +0000 (+0000) Subject: Fix the problem whereby `yes > k & tail -1 k' would infloop. X-Git-Tag: TEXTUTILS-1_22i~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f3df8336aed453931dc358bdc67b22a923f844a9;p=platform%2Fupstream%2Fcoreutils.git 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. --- 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);