diff options
author | Jim Meyering <jim@meyering.net> | 2002-02-11 11:00:00 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2002-02-11 11:00:00 +0000 |
commit | e44fbb95ea54af272ad1cf1a6996041e954fcd9c (patch) | |
tree | 1136698ed25e3ab95edaf9ea4a75756e4930ee39 /src | |
parent | 5ec71c5990c7f30a5b9c97fb6d77e8eb340f58fc (diff) | |
download | coreutils-e44fbb95ea54af272ad1cf1a6996041e954fcd9c.tar.xz |
(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.
Diffstat (limited to 'src')
-rw-r--r-- | src/head.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/head.c b/src/head.c index 6f7496fc0..dd39cbd84 100644 --- a/src/head.c +++ b/src/head.c @@ -182,7 +182,20 @@ head_lines (const char *filename, int fd, uintmax_t lines_to_write) break; while (bytes_to_write < bytes_read) if (buffer[bytes_to_write++] == '\n' && --lines_to_write == 0) - break; + { + /* If we have read more data than that on the specified number + of lines, try to seek back to the position we would have + gotten to had we been reading one byte at a time. */ + 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, _("cannot reposition file pointer for %s"), + filename); + } + break; + } if (fwrite (buffer, 1, bytes_to_write, stdout) == 0) error (EXIT_FAILURE, errno, _("write error")); } |