diff options
author | Jim Meyering <jim@meyering.net> | 2002-01-27 07:53:37 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2002-01-27 07:53:37 +0000 |
commit | 662fff786183c374c1d941373a34c50441e5c319 (patch) | |
tree | 179f987e8a59ad9a7a27744ff7dfead8683a492c /src | |
parent | 2852c1c0a1d75a4715255afde44ffbedeb63a39b (diff) | |
download | coreutils-662fff786183c374c1d941373a34c50441e5c319.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/tail.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/tail.c b/src/tail.c index 2131c1d4b..684cf1397 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); } |