From 476ce37019df5b1cb917c5b50e71f9bce5911401 Mon Sep 17 00:00:00 2001 From: Алексей Шилин Date: Wed, 29 Jan 2014 01:23:46 +0000 Subject: head: fix --lines=-0 outputting nothing if no newline at EOF * src/head.c (elide_tail_lines_pipe): Just output all input in this case to avoid the issue and also avoid redundant '\n' processing. (elide_tail_lines_seekable): Likewise. * tests/misc/head-elide-tail.pl: Add tests for no '\n' at EOF. * NEWS: Mention the fix. Fixes http://bugs.gnu.org/16329 --- src/head.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src/head.c') diff --git a/src/head.c b/src/head.c index ddaa9906b..ef368d7cc 100644 --- a/src/head.c +++ b/src/head.c @@ -501,6 +501,13 @@ elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide) n_read = safe_read (fd, tmp->buffer, BUFSIZ); if (n_read == 0 || n_read == SAFE_READ_ERROR) break; + + if (! n_elide) + { + fwrite (tmp->buffer, 1, n_read, stdout); + continue; + } + tmp->nbytes = n_read; tmp->nlines = 0; tmp->next = NULL; @@ -636,8 +643,11 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, return false; } + /* n_lines == 0 case needs special treatment. */ + const bool all_lines = !n_lines; + /* Count the incomplete line on files that don't end with a newline. */ - if (bytes_read && buffer[bytes_read - 1] != '\n') + if (n_lines && bytes_read && buffer[bytes_read - 1] != '\n') --n_lines; while (1) @@ -647,11 +657,16 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, size_t n = bytes_read; while (n) { - char const *nl; - nl = memrchr (buffer, '\n', n); - if (nl == NULL) - break; - n = nl - buffer; + if (all_lines) + n -= 1; + else + { + char const *nl; + nl = memrchr (buffer, '\n', n); + if (nl == NULL) + break; + n = nl - buffer; + } if (n_lines-- == 0) { /* Found it. */ -- cgit v1.2.3-54-g00ecf