Subject: Re: bug#11631: closed (Re: bug#11631: Head command does not position file pointer correctly for negative line count)

From: Jim Meyering <[email-masked]>
Date: Wed, 06 Jun 2012 10:02:18 +0200

Anoop Sharma wrote:
> 1. The comment in code - "Don't bother testing for failure for such a
> small amount. Any failure will be detected upon close." may be
> re-looked too, since we are now lseeking after it.
>
> What if we change plain fwrite to:
>       if (fwrite (buffer, 1, n + 1, stdout) < (n + 1))
>             error (EXIT_FAILURE, errno, _("write error"));
>
> 2. Maybe using lseek with SEEK_CUR in place of SEEK_SET would reflect
> logic better.

Regarding #1, thanks for the suggestion.  You're right.

I've written a complete patch in your name, below, adding a commit
log and removing the unnecessary parentheses around "n + 1".
In general, I much prefer a patch to a suggestion like that.
The advantage to you is that if you describe precisely enough
(usually requires a patch), then you're listed as the author
of the commit.  If you also take the time to write a commit
log entry like I did below, then that's even better.
See the file, HACKING, for instructions on how to do that
as well as general contribution guidelines.

Regarding #2, please be precise and write/post a patch.

>From 7fcfa754f1ff92d4fed1495a05574b8e47c3b4fb Mon Sep 17 00:00:00 2001
From: Anoop Sharma <[email-masked]>
Date: Wed, 6 Jun 2012 09:42:09 +0200
Subject: [PATCH] head: diagnose write failure immediately, given new lseek
 use

Inserting the lseek call (commit v8.17-13-g295ee52) rendered
inaccurate the comment/code that refrained from diagnosing the
failure of the just-preceding fwrite.
* src/head.c (elide_tail_lines_seekable): Remove now-erroneous
comment, and diagnose fwrite failure.
---
 src/head.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/head.c b/src/head.c
index c13c064..ffe7c74 100644
--- a/src/head.c
+++ b/src/head.c
@@ -663,10 +663,9 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd,
                 }

               /* Output the initial portion of the buffer
-                 in which we found the desired newline byte.
-                 Don't bother testing for failure for such a small amount.
-                 Any failure will be detected upon close.  */
-              fwrite (buffer, 1, n + 1, stdout);
+                 in which we found the desired newline byte.  */
+              if (fwrite (buffer, 1, n + 1, stdout) < n + 1)
+                error (EXIT_FAILURE, errno, _("write error"));

               /* Set file pointer to the byte after what we've output.  */
               if (lseek (fd, pos + n + 1, SEEK_SET) < 0)
--
1.7.11.rc1