bug-textutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH]: bad interaction between head(1) and pipes


From: Jim Meyering
Subject: Re: [PATCH]: bad interaction between head(1) and pipes
Date: Sat, 18 Aug 2001 11:34:35 +0200
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.0.105

address@hidden wrote:
> I claim that the following two commands should produce the same
> output. Unfortunately, as of textutils-2.0.14, they don't:
>
> $ echo abcdefgh | head -c6                # expected behaviour
> abcdef
>
> $ echo abcdefgh | (head -c3 ; head -c3)   # unexpected behaviour
> abc

Thanks a lot for the report and patch!
I've applied it with a minor change; diff below.

BTW, it's not just with pipes:

  $ echo abc > x
  $ (./head -c1; ./head -c1) < x
  ab$

The above is with the fixed version.
Without your fix, the output would be just `a'.


Index: head.c
===================================================================
RCS file: /fetish/textutils/src/head.c,v
retrieving revision 1.61
diff -u -p -r1.61 head.c
--- head.c      2000/08/07 13:27:24     1.61
+++ head.c      2001/08/18 09:30:16
@@ -120,13 +120,16 @@ head_bytes (const char *filename, int fd
 {
   char buffer[BUFSIZE];
   int bytes_read;
+  size_t bytes_to_read = BUFSIZE;
 
   /* Need BINARY I/O for the byte counts to be accurate.  */
   SET_BINARY2 (fd, fileno (stdout));
 
   while (bytes_to_write)
     {
-      bytes_read = safe_read (fd, buffer, BUFSIZE);
+      if (bytes_to_write < bytes_to_read)
+       bytes_to_read = bytes_to_write;
+      bytes_read = safe_read (fd, buffer, bytes_to_read);
       if (bytes_read < 0)
        {
          error (0, errno, "%s", filename);
@@ -134,8 +137,6 @@ head_bytes (const char *filename, int fd
        }
       if (bytes_read == 0)
        break;
-      if (bytes_read > bytes_to_write)
-       bytes_read = bytes_to_write;
       if (fwrite (buffer, 1, bytes_read, stdout) == 0)
        error (EXIT_FAILURE, errno, _("write error"));
       bytes_to_write -= bytes_read;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]