Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
authorBruno Haible <bruno@clisp.org>
Fri, 22 Nov 2002 16:25:53 +0000 (16:25 +0000)
committerBruno Haible <bruno@clisp.org>
Fri, 22 Nov 2002 16:25:53 +0000 (16:25 +0000)
correctly.

lib/ChangeLog
lib/full-read.c [new file with mode: 0644]
lib/full-read.h [new file with mode: 0644]
lib/full-write.c
lib/full-write.h
lib/safe-read.c
lib/safe-read.h
lib/safe-write.c [new file with mode: 0644]
lib/safe-write.h [new file with mode: 0644]

index 90bd018..f85e717 100644 (file)
@@ -1,3 +1,18 @@
+2002-11-22  Bruno Haible  <bruno@clisp.org>
+
+       * safe-read.h: Assume C89. Add comments.
+       (safe_read): Change return type to size_t.
+       * safe-read.c (safe_read): Change return type to size_t. Handle byte
+       counts > SSIZE_MAX correctly.
+       * safe-write.h: New file.
+       * safe-write.c: New file.
+       * full-read.h: New file.
+       * full-read.c: New file.
+       * full-write.h: Assume C89. Add comments.
+       * full-write.c: Include safe-write.h.
+       (full_write): Rewritten to use safe_write.
+       Suggested by Jim Meyering and Paul Eggert.
+
 2002-11-21  Bruno Haible  <bruno@clisp.org>
 
        Remove case insensitive option matching.
diff --git a/lib/full-read.c b/lib/full-read.c
new file mode 100644 (file)
index 0000000..a328588
--- /dev/null
@@ -0,0 +1,65 @@
+/* An interface to read() that reads all it is asked to read.
+
+   Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
+   Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, read to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include "full-read.h"
+
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#include "safe-read.h"
+
+/* Read COUNT bytes at BUF to descriptor FD, retrying if interrupted
+   or if partial reads occur.  Return the number of bytes successfully
+   read, setting errno if that is less than COUNT.  errno = 0 means EOF.  */
+size_t
+full_read (int fd, void *buf, size_t count)
+{
+  size_t total_read = 0;
+
+  if (count > 0)
+    {
+      char *ptr = buf;
+
+      do
+       {
+         size_t nread = safe_read (fd, ptr, count);
+         if (nread == (size_t)-1)
+           break;
+         if (nread == 0)
+           {
+             /* EOF.  */
+             errno = 0;
+             break;
+           }
+         total_read += nread;
+         ptr += nread;
+         count -= nread;
+       }
+      while (count > 0);
+    }
+
+  return total_read;
+}
diff --git a/lib/full-read.h b/lib/full-read.h
new file mode 100644 (file)
index 0000000..71f19a3
--- /dev/null
@@ -0,0 +1,24 @@
+/* An interface to read() that reads all it is asked to read.
+
+   Copyright (C) 2002 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, read to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#include <stddef.h>
+
+/* Read COUNT bytes at BUF to descriptor FD, retrying if interrupted
+   or if partial reads occur.  Return the number of bytes successfully
+   read, setting errno if that is less than COUNT.  errno = 0 means EOF.  */
+extern size_t full_read (int fd, void *buf, size_t count);
index 4e566f6..f21e479 100644 (file)
@@ -1,6 +1,6 @@
-/* full-write.c -- an interface to write that retries after interrupts
+/* An interface to write() that writes all it is asked to write.
 
-   Copyright 1993, 1994, 1997, 1998, 1999, 2000, 2001 Free Software
+   Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
    Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-   Written by Paul Eggert.  */
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #if HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#include <sys/types.h>
-
+/* Specification.  */
 #include "full-write.h"
 
-#if HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #include <errno.h>
 #ifndef errno
 extern int errno;
 #endif
 
-/* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted
-   or if partial writes occur.  Return the number of bytes successfully
-   written, setting errno if that is less than LEN.  */
+#include "safe-write.h"
 
+/* Write COUNT bytes at BUF to descriptor FD, retrying if interrupted
+   or if partial writes occur.  Return the number of bytes successfully
+   written, setting errno if that is less than COUNT.  */
 size_t
-full_write (int desc, const char *ptr, size_t len)
+full_write (int fd, const void *buf, size_t count)
 {
   size_t total_written = 0;
 
-  while (len > 0)
+  if (count > 0)
     {
-      ssize_t written = write (desc, ptr, len);
-      if (written <= 0)
+      const char *ptr = buf;
+
+      do
        {
-         /* Some buggy drivers return 0 when you fall off a device's end.  */
+         size_t written = safe_write (fd, ptr, count);
+         if (written == (size_t)-1)
+           break;
          if (written == 0)
-           errno = ENOSPC;
-#ifdef EINTR
-         if (errno == EINTR)
-           continue;
-#endif
-         break;
+           {
+             /* Some buggy drivers return 0 when you fall off a device's
+                end.  (Example: Linux 1.2.13 on /dev/fd0.)  */
+             errno = ENOSPC;
+             break;
+           }
+         total_written += written;
+         ptr += written;
+         count -= written;
        }
-      total_written += written;
-      ptr += written;
-      len -= written;
+      while (count > 0);
     }
+
   return total_written;
 }
index f23bccb..8cd2e81 100644 (file)
@@ -1,9 +1,24 @@
-#ifndef PARAMS
-# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
-#  define PARAMS(Args) Args
-# else
-#  define PARAMS(Args) ()
-# endif
-#endif
-
-size_t full_write PARAMS ((int, const char *, size_t));
+/* An interface to write() that writes all it is asked to write.
+
+   Copyright (C) 2002 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#include <stddef.h>
+
+/* Write COUNT bytes at BUF to descriptor FD, retrying if interrupted
+   or if partial writes occur.  Return the number of bytes successfully
+   written, setting errno if that is less than COUNT.  */
+extern size_t full_write (int fd, const void *buf, size_t count);
index e404586..79d8541 100644 (file)
@@ -1,5 +1,5 @@
-/* safe-read.c -- an interface to read that retries after interrupts
-   Copyright (C) 1993, 1994, 1998 Free Software Foundation, Inc.
+/* An interface to read() that retries after interrupts.
+   Copyright (C) 1993, 1994, 1998, 2002 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-   */
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #if HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#include <sys/types.h>
+/* Specification.  */
+#include "safe-read.h"
 
+/* Get ssize_t.  */
+#include <sys/types.h>
 #if HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 extern int errno;
 #endif
 
-#include "safe-read.h"
+#include <limits.h>
 
-/* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
-   Return the actual number of bytes read, zero for EOF, or negative
-   for an error.  */
+/* 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
 
-ssize_t
-safe_read (int desc, void *ptr, size_t len)
+/* Read up to COUNT bytes at BUF from descriptor FD, retrying if interrupted.
+   Return the actual number of bytes read, zero for EOF, or (size_t) -1
+   for an error.  */
+size_t
+safe_read (int fd, void *buf, size_t count)
 {
-  ssize_t n_chars;
+  size_t total_read = 0;
 
-  if (len <= 0)
-    return len;
+  if (count > 0)
+    {
+      char *ptr = (char *) buf;
+      do
+       {
+         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;
 
+         result = read (fd, ptr, nbytes_to_read);
+         if (result < 0)
+           {
 #ifdef EINTR
-  do
-    {
-      n_chars = read (desc, ptr, len);
-    }
-  while (n_chars < 0 && errno == EINTR);
-#else
-  n_chars = read (desc, ptr, len);
+             if (errno == EINTR)
+               continue;
 #endif
+             return result;
+           }
+         total_read += result;
+         ptr += result;
+         count -= result;
+       }
+      while (count > 0);
+    }
 
-  return n_chars;
+  return total_read;
 }
index 8e4b165..c4445ac 100644 (file)
@@ -1,10 +1,23 @@
-#ifndef PARAMS
-# if defined PROTOTYPES || (defined __STDC__ && __STDC__)
-#  define PARAMS(Args) Args
-# else
-#  define PARAMS(Args) ()
-# endif
-#endif
-
-ssize_t
-safe_read PARAMS ((int desc, void *ptr, size_t len));
+/* An interface to read() that retries after interrupts.
+   Copyright (C) 2002 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#include <stddef.h>
+
+/* Read up to COUNT bytes at BUF from descriptor FD, retrying if interrupted.
+   Return the actual number of bytes read, zero for EOF, or (size_t) -1
+   for an error.  */
+extern size_t safe_read (int fd, void *buf, size_t count);
diff --git a/lib/safe-write.c b/lib/safe-write.c
new file mode 100644 (file)
index 0000000..5596203
--- /dev/null
@@ -0,0 +1,83 @@
+/* An interface to write() that retries after interrupts.
+   Copyright (C) 1993, 1994, 1998, 2002 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include "safe-write.h"
+
+/* Get ssize_t.  */
+#include <sys/types.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#include <limits.h>
+
+/* We don't pass an nbytes count > SSIZE_MAX to write() - POSIX says the
+   effect would be implementation-defined.  Also we don't pass an nbytes
+   count > INT_MAX but <= SSIZE_MAX to write() - this triggers a bug in
+   Tru64 5.1.  */
+#define MAX_BYTES_TO_READ INT_MAX
+
+/* Write up to COUNT bytes at BUF to descriptor FD, retrying if interrupted.
+   Return the actual number of bytes written, zero for EOF, or (size_t) -1
+   for an error.  */
+size_t
+safe_write (int fd, const void *buf, size_t count)
+{
+  size_t total_written = 0;
+
+  if (count > 0)
+    {
+      const char *ptr = (const char *) buf;
+      do
+       {
+         size_t nbytes_to_write = count;
+         ssize_t result;
+
+         /* Limit the number of bytes to write in one round, to avoid running
+            into unspecified behaviour.  But keep the file pointer block
+            aligned when doing so.  */
+         if (nbytes_to_write > MAX_BYTES_TO_READ)
+           nbytes_to_write = MAX_BYTES_TO_READ & ~8191;
+
+         result = write (fd, ptr, nbytes_to_write);
+         if (result < 0)
+           {
+#ifdef EINTR
+             if (errno == EINTR)
+               continue;
+#endif
+             return result;
+           }
+         total_written += result;
+         ptr += result;
+         count -= result;
+       }
+      while (count > 0);
+    }
+
+  return total_written;
+}
diff --git a/lib/safe-write.h b/lib/safe-write.h
new file mode 100644 (file)
index 0000000..25d1a4d
--- /dev/null
@@ -0,0 +1,23 @@
+/* An interface to write() that retries after interrupts.
+   Copyright (C) 2002 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#include <stddef.h>
+
+/* Write up to COUNT bytes at BUF to descriptor FD, retrying if interrupted.
+   Return the actual number of bytes written, zero for EOF, or (size_t) -1
+   for an error.  */
+extern size_t safe_write (int fd, const void *buf, size_t count);