summaryrefslogtreecommitdiff
path: root/lib/safe-read.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2002-12-03 12:02:19 +0000
committerJim Meyering <jim@meyering.net>2002-12-03 12:02:19 +0000
commit01d5814091c7010e395cc3481774753a478b6145 (patch)
tree61422f8d266e4e431638580aa21b0ab63f90a8cc /lib/safe-read.c
parent90723c47a01b871e73b78589b1f0d8e7f703b3b3 (diff)
downloadcoreutils-01d5814091c7010e395cc3481774753a478b6145.tar.xz
tweak comment
Diffstat (limited to 'lib/safe-read.c')
-rw-r--r--lib/safe-read.c35
1 files changed, 15 insertions, 20 deletions
diff --git a/lib/safe-read.c b/lib/safe-read.c
index 527653c5c..e52326eaf 100644
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -33,6 +33,12 @@
extern int errno;
#endif
+#ifdef EINTR
+# define IS_EINTR(x) ((x) == EINTR)
+#else
+# define IS_EINTR(x) 0
+#endif
+
#include <limits.h>
#ifndef CHAR_BIT
@@ -51,38 +57,27 @@ extern int errno;
# define INT_MAX TYPE_MAXIMUM (int)
#endif
-/* We don't pass an nbytes count > SSIZE_MAX to read() - POSIX says the
- effect would be implementation-defined. Also we don't pass an nbytes
- count > INT_MAX but <= SSIZE_MAX to read() - this triggers a bug in
- Tru64 5.1. */
-#define MAX_BYTES_TO_READ INT_MAX
-
-#ifndef EINTR
-/* If a system doesn't have support for EINTR, define it
- to be a value to which errno will never be set. */
-# define EINTR (INT_MAX - 10)
-#endif
-
/* Read up to COUNT bytes at BUF from descriptor FD, retrying if interrupted.
Return the actual number of bytes read, zero for EOF, or SAFE_READ_ERROR
upon error. */
size_t
safe_read (int fd, void *buf, size_t count)
{
- size_t nbytes_to_read = count;
ssize_t result;
- /* Limit the number of bytes to read, to avoid running
- into unspecified behaviour. But keep the file pointer block
- aligned when doing so. */
- if (nbytes_to_read > MAX_BYTES_TO_READ)
- nbytes_to_read = MAX_BYTES_TO_READ & ~8191;
+ /* POSIX limits COUNT to SSIZE_MAX, but we limit it further, requiring
+ that COUNT <= INT_MAX, to avoid triggering a bug in Tru64 5.1.
+ When decreasing COUNT, keep the file pointer block-aligned.
+ Note that in any case, read may succeed, yet read fewer than COUNT
+ bytes, so the caller must be prepared to handle partial results. */
+ if (count > INT_MAX)
+ count = INT_MAX & ~8191;
do
{
- result = read (fd, buf, nbytes_to_read);
+ result = read (fd, buf, count);
}
- while (result < 0 && errno == EINTR);
+ while (result < 0 && IS_EINTR (errno));
return (size_t) result;
}