changeset 4047:576523b572d3

(EINTR): Define. (safe_read): Rewrite to iterate IFF the read fails with EINTR.
author Jim Meyering <jim@meyering.net>
date Mon, 02 Dec 2002 17:31:49 +0000
parents c66afe793700
children 983c7437c09f
files lib/safe-read.c
diffstat 1 files changed, 18 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -57,41 +57,32 @@
    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 total_read = 0;
-  char *ptr = buf;
+  size_t nbytes_to_read = count;
+  ssize_t result;
 
-  while (count > 0)
-    {
-      size_t nbytes_to_read = count;
-      ssize_t result;
-
-      /* Limit the number of bytes to read in one round, 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;
+  /* 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;
 
-      result = read (fd, ptr, nbytes_to_read);
-      if (result == 0)
-	break;
-      if (result < 0)
-	{
-#ifdef EINTR
-	  if (errno == EINTR)
-	    continue;
-#endif
-	  return SAFE_READ_ERROR;
-	}
-      total_read += result;
-      ptr += result;
-      count -= result;
+  do
+    {
+      result = read (fd, buf, nbytes_to_read);
     }
+  while (result < 0 && errno == EINTR);
 
-  return total_read;
+  return (size_t) result;
 }