From 01d5814091c7010e395cc3481774753a478b6145 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 3 Dec 2002 12:02:19 +0000 Subject: tweak comment --- lib/safe-read.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'lib/safe-read.c') 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 #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; } -- cgit v1.2.3-54-g00ecf