]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
head: with --lines=-N (-n-N) reset file pointer on seekable input
authorJim Meyering <meyering@redhat.com>
Tue, 5 Jun 2012 10:24:49 +0000 (12:24 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 5 Jun 2012 17:55:26 +0000 (19:55 +0200)
* src/head.c (elide_tail_lines_seekable): Reset file pointer
after printing up to an end-relative line-counted offset.
Anoop Sharma reported the problem and suggested the fix.
* tests/misc/head-pos: Add coverage via a very similar, existing test.
Also add coverage for a previously untested block of code.
* tests/misc/head-elide-tail ($READ_BUFSIZE): Update to 8192, to
match the value of BUFSIZ I see today on Fedora 17/x86_64 (unrelated
to this fix).
* NEWS (Bug fixes): Mention it.

Improved-by: Pádraig Brady
NEWS
src/head.c
tests/misc/head-elide-tail
tests/misc/head-pos

diff --git a/NEWS b/NEWS
index 7c7c2c30e774663da1521154920850921eec359a..c0e5fdbbba113a3e79672f34552168a0bdb3fc54 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -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]
 
index d7e83b716d38ea8ed14049033b86f10509b17e36..0d5e1b2c663d22e5058ec52ed49f0c98f02661ca 100644 (file)
@@ -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;
             }
         }
index de4896bc5be79411edccb738dc65da18f2c02d97..85f509d26ea8a0f72523e0da63ca2a067fb46f54 100755 (executable)
@@ -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 =
   (
index 3d96261f9c0fab08400669f4755e827b457ad83a..fa3284e90fcff5cc765f3a954ac24a8731ac38de 100755 (executable)
 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