bug-textutils
[Top][All Lists]
Advanced

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

GNU 'head' bug with respect to file position after 'head' exits


From: Paul Eggert
Subject: GNU 'head' bug with respect to file position after 'head' exits
Date: Fri, 8 Feb 2002 14:49:23 -0800 (PST)

> Date: Fri, 8 Feb 2002 10:37:55 -0800
> From: Jim Hill <address@hidden>

> I'll note that neither GNU's head nor FreeBSD's will work properly with 
> David Korn's example: GNU head uses `read(fd,buff,BUFSIZE)` and doesn't 
> lseek at the end; and FreeBSD's uses a getc-putc loop and doesn't 
> fflush() at the end.

Here's a patch to GNU head to fix that bug.

Note to textutils maintainer: David Korn's example:
`(head -n 5 >/dev/null;cat) < file'
would successfully behead the first 5 lines of file.  See:

http://www.opengroup.org/sophocles/show_mail.tpl?source=L&listname=austin-group-l&id=3609


2002-02-08  Paul Eggert  <address@hidden>

        * src/head.c (head_lines): If we have read too much data, try
        to seek back to the position we would have gotten to had we
        been reading one byte at a time.  POSIX currently doesn't
        require this, but it's easy to do and some software relies on
        it.

--- src/head.c-orig     Sat Dec  1 09:29:26 2001
+++ src/head.c  Fri Feb  8 14:29:20 2002
@@ -175,7 +175,16 @@ head_lines (const char *filename, int fd
        break;
       while (bytes_to_write < bytes_read)
        if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0)
-         break;
+         {
+           if (lseek (fd, bytes_to_write - bytes_read, SEEK_CUR) < 0)
+             {
+               int e = errno;
+               struct stat st;
+               if (fstat (fd, &st) != 0 || S_ISREG (st.st_mode))
+                 error (0, e, _("lseek error"));
+             }
+           break;
+         }
       if (fwrite (buffer, 1, bytes_to_write, stdout) == 0)
        error (EXIT_FAILURE, errno, _("write error"));
     }



reply via email to

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