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