summaryrefslogtreecommitdiff
path: root/src/head.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2001-08-18 10:10:56 +0000
committerJim Meyering <jim@meyering.net>2001-08-18 10:10:56 +0000
commit215ba7757ee88470673b32e47d88e1ed369913e3 (patch)
treeaf42cabd3299db4f45c0b5df4d282cc1b5d7dbf6 /src/head.c
parent54041560f5f4eb963a3b4cc62a7d7fef5dd48fbb (diff)
downloadcoreutils-215ba7757ee88470673b32e47d88e1ed369913e3.tar.gz
coreutils-215ba7757ee88470673b32e47d88e1ed369913e3.tar.bz2
coreutils-215ba7757ee88470673b32e47d88e1ed369913e3.zip
(head_bytes): Read no more than the specified number of bytes.
Diffstat (limited to 'src/head.c')
-rw-r--r--src/head.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/head.c b/src/head.c
index 7bc3cad17..a71a83d03 100644
--- a/src/head.c
+++ b/src/head.c
@@ -1,5 +1,5 @@
/* head -- output first part of file(s)
- Copyright (C) 89, 90, 91, 1995-2000 Free Software Foundation, Inc.
+ Copyright (C) 89, 90, 91, 1995-2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -120,13 +120,16 @@ head_bytes (const char *filename, int fd, uintmax_t bytes_to_write)
{
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, uintmax_t bytes_to_write)
}
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;