summaryrefslogtreecommitdiff
path: root/lib/safe-read.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2002-12-04 07:50:55 +0000
committerJim Meyering <jim@meyering.net>2002-12-04 07:50:55 +0000
commitc0ff7df8cbd8161e35fd2a39632750abb0424076 (patch)
tree0ee1902138a1cf0b85dac6afd2d618a0e460628f /lib/safe-read.c
parentc5a4e28e9b22a49073a621a5a56e4d66e73aa512 (diff)
downloadcoreutils-c0ff7df8cbd8161e35fd2a39632750abb0424076.tar.xz
Rework so that it may serve to define safe_write, too.
Diffstat (limited to 'lib/safe-read.c')
-rw-r--r--lib/safe-read.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/lib/safe-read.c b/lib/safe-read.c
index e52326eaf..c36f6e824 100644
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -1,4 +1,4 @@
-/* An interface to read that retries after interrupts.
+/* An interface to read and 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
@@ -19,9 +19,6 @@
# include <config.h>
#endif
-/* Specification. */
-#include "safe-read.h"
-
/* Get ssize_t. */
#include <sys/types.h>
#if HAVE_UNISTD_H
@@ -57,25 +54,38 @@ extern int errno;
# define INT_MAX TYPE_MAXIMUM (int)
#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. */
+#ifdef SAFE_WRITE
+# include "safe-write.h"
+# define safe_rw safe_write
+# define rw write
+#else
+# include "safe-read.h"
+# define safe_rw safe_read
+# define rw read
+# undef const
+# define const /* empty */
+#endif
+
+/* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
+ interrupted. Return the actual number of bytes read(written), zero for EOF,
+ or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
size_t
-safe_read (int fd, void *buf, size_t count)
+safe_rw (int fd, void const *buf, size_t count)
{
ssize_t result;
/* POSIX limits COUNT to SSIZE_MAX, but we limit it further, requiring
that COUNT <= INT_MAX, to avoid triggering a bug in Tru64 5.1.
When decreasing COUNT, keep the file pointer block-aligned.
- Note that in any case, read may succeed, yet read fewer than COUNT
- bytes, so the caller must be prepared to handle partial results. */
+ Note that in any case, read(write) may succeed, yet read(write)
+ fewer than COUNT bytes, so the caller must be prepared to handle
+ partial results. */
if (count > INT_MAX)
count = INT_MAX & ~8191;
do
{
- result = read (fd, buf, count);
+ result = rw (fd, buf, count);
}
while (result < 0 && IS_EINTR (errno));