diff options
-rw-r--r-- | NEWS | 7 | ||||
-rw-r--r-- | src/head.c | 8 | ||||
-rwxr-xr-x | tests/misc/head-elide-tail | 2 | ||||
-rwxr-xr-x | tests/misc/head-pos | 26 |
4 files changed, 35 insertions, 8 deletions
@@ -8,6 +8,13 @@ GNU coreutils NEWS -*- outline -*- processes will not intersperse their output. [the bug dates back to the initial implementation] + head --lines=-N (-n-N) now resets the read pointer of a seekable input file. + This means that "head -n-3" no longer consumes all of its input, and lines + not output by head may be processed by other programs. For example, this + command now prints the final line, 2, while before it would print nothing: + seq 2 > k; (head -n-1 > /dev/null; cat) < k + [This bug was present in "the beginning".] + ls --color would mis-color relative-named symlinks in / [bug introduced in coreutils-8.17] diff --git a/src/head.c b/src/head.c index d7e83b716..0d5e1b2c6 100644 --- a/src/head.c +++ b/src/head.c @@ -667,6 +667,14 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, Don't bother testing for failure for such a small amount. Any failure will be detected upon close. */ fwrite (buffer, 1, n + 1, stdout); + + /* Set file pointer to the byte after what we've output. */ + if (lseek (fd, pos + n + 1, SEEK_SET) < 0) + { + error (0, errno, "%s: failed to reset file pointer", + quote (pretty_filename)); + return false; + } return true; } } diff --git a/tests/misc/head-elide-tail b/tests/misc/head-elide-tail index de4896bc5..85f509d26 100755 --- a/tests/misc/head-elide-tail +++ b/tests/misc/head-elide-tail @@ -26,7 +26,7 @@ $ENV{PROG} = 'head'; @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; # This should match the definition in head.c. -my $READ_BUFSIZE = 4096; +my $READ_BUFSIZE = 8192; my @Tests = ( diff --git a/tests/misc/head-pos b/tests/misc/head-pos index 3d96261f9..fa3284e90 100755 --- a/tests/misc/head-pos +++ b/tests/misc/head-pos @@ -21,12 +21,24 @@ print_ver_ head (echo a; echo b) > in || framework_failure_ - -(head -n 1 >/dev/null; cat) < in > out || fail=1 -cat <<EOF > exp -b -EOF - -compare exp out || fail=1 +echo b > exp || framework_failure_ + +for i in -1 1; do + (head -n $i >/dev/null; cat) < in > out || fail=1 + compare exp out || fail=1 +done + +# Exercise the (start_pos < pos) block in elide_tail_lines_seekable. +# So far, this is the only test to do that. +# Do that by creating a file larger than BUFSIZ (I've seen 128K) and +# elide a suffix of it (by line count) that is also larger than BUFSIZ. +# 50000 lines times 6 bytes per line gives us enough leeway even on a +# system with a BUFSIZ of 256K. +n_lines=50000 +seq 70000 > in2 || framework_failure_ +echo $n_lines > exp-n || framework_failure_ + +(head -n-$n_lines>/dev/null; wc -l) < in2 > n +compare exp-n n || fail=1 Exit $fail |