summaryrefslogtreecommitdiff
path: root/lib/safe-read.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-01-08 19:33:34 +0000
committerJim Meyering <jim@meyering.net>1995-01-08 19:33:34 +0000
commit96dcb4e9329ab0b64d747ce196651a38e182fa5c (patch)
treed0801f29aaf8ae7b5abb275b9e8ea954227ada39 /lib/safe-read.c
parenta151c6859f932b56bc29aca70406db46fa015efc (diff)
downloadgnulib-96dcb4e9329ab0b64d747ce196651a38e182fa5c.tar.gz
See log in textutils/lib/.
Diffstat (limited to 'lib/safe-read.c')
-rw-r--r--lib/safe-read.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/lib/safe-read.c b/lib/safe-read.c
index 7fd4415bb2..93d35e4fa6 100644
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -27,39 +27,34 @@
#endif
#include <errno.h>
-#ifndef STDC_HEADERS
+#ifndef errno
extern int errno;
#endif
-/* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
- retrying if necessary. Return a negative value if an error occurs,
- otherwise return the actual number of bytes read,
- which must be LEN unless end-of-file was reached. */
+/* 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. */
- The canonical source for this function is in the gcc distribution
- in file cccp.c. So don't make any changes here. */
-
-static int
+int
safe_read (desc, ptr, len)
int desc;
char *ptr;
int len;
{
- int left = len;
- while (left > 0) {
- int nchars = read (desc, ptr, left);
- if (nchars < 0)
- {
+ int n_chars;
+
+ if (len <= 0)
+ return len;
+
#ifdef EINTR
- if (errno == EINTR)
- continue;
+ do
+ {
+ n_chars = read (desc, ptr, len);
+ }
+ while (n_chars < 0 && errno == EINTR);
+#else
+ n_chars = read (desc, ptr, len);
#endif
- return nchars;
- }
- if (nchars == 0)
- break;
- ptr += nchars;
- left -= nchars;
- }
- return len - left;
+
+ return n_chars;
}