From: Jim Meyering Date: Sun, 27 Jan 2002 07:53:37 +0000 (+0000) Subject: tail -n +2 would perform an extra read after encountering EOF X-Git-Tag: TEXTUTILS-2_0_21~139 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=662fff786183c374c1d941373a34c50441e5c319;p=thirdparty%2Fcoreutils.git tail -n +2 would perform an extra read after encountering EOF (start_lines): Detect EOF, inform caller. (tail_lines): Upon EOF in start_lines, return immediately. --- diff --git a/src/tail.c b/src/tail.c index 2131c1d4b5..684cf1397e 100644 --- a/src/tail.c +++ b/src/tail.c @@ -747,7 +747,7 @@ start_bytes (const char *pretty_filename, int fd, off_t n_bytes) /* Skip N_LINES lines at the start of file or pipe FD, and print any extra characters that were read beyond that. - Return 1 on error, 0 if ok. */ + Return 1 on error, 0 if ok, -1 if EOF. */ static int start_lines (const char *pretty_filename, int fd, long int n_lines) @@ -763,16 +763,24 @@ start_lines (const char *pretty_filename, int fd, long int n_lines) if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0) break; } + + /* EOF */ + if (bytes_read == 0) + return -1; + + /* error */ if (bytes_read == -1) { error (0, errno, "%s", pretty_filename); return 1; } - else if (bytes_to_skip < bytes_read) + + if (bytes_to_skip < bytes_read) { xwrite (STDOUT_FILENO, &buffer[bytes_to_skip], bytes_read - bytes_to_skip); } + return 0; } @@ -1130,7 +1138,10 @@ tail_lines (const char *pretty_filename, int fd, long int n_lines) if (from_start) { - if (start_lines (pretty_filename, fd, n_lines)) + int t; + if ((t = start_lines (pretty_filename, fd, n_lines)) < 0) + return 0; + if (t) return 1; dump_remainder (pretty_filename, fd, COPY_TO_EOF); }