Blob Blame History Raw
diff --git a/util.c b/util.c
index 46bc89d..34fd789 100644
--- a/util.c
+++ b/util.c
@@ -139,9 +139,29 @@ read_buffer (fd, buf, cnt)
      voidp buf;
      unsigned int cnt;
 {
+  int len;
+#ifdef INT_MAX
   if (INT_MAX < cnt)
     cnt = INT_MAX;
-  return read (fd, buf, cnt);
+#endif
+  len = read (fd, buf, cnt);
+
+#if defined F_SETFL && O_NONBLOCK && defined EAGAIN
+  /* Iput files are opened O_NONBLOCK for security reasons. On some
+     file systems this can cause read to fail with errno == EAGAIN.  */
+  if (len < 0 && errno == EAGAIN)
+    {
+      int flags = fcntl (fd, F_GETFL);
+      if (0 <= flags)
+        {
+          if (! (flags & O_NONBLOCK))
+            errno = EAGAIN;
+          else if (fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
+            len = read (fd, buf, cnt);
+        }
+    }
+#endif
+  return len;
 }
 
 /* Likewise for 'write'.  */