aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util.c24
-rw-r--r--src/util/io.h12
2 files changed, 32 insertions, 4 deletions
diff --git a/src/util.c b/src/util.c
index 4654789..299dee8 100644
--- a/src/util.c
+++ b/src/util.c
@@ -112,18 +112,34 @@ int
writeall(int fd, void *buf, size_t n)
{
char *buffer = buf;
- size_t ptr = 0;
ssize_t r;
- while (ptr < n) {
+ while (n) {
r = write(fd, buffer, n);
if (r < 0)
return -1;
- buffer += (size_t)ptr;
- n -= (size_t)ptr;
+ buffer += (size_t)r;
+ n -= (size_t)r;
}
return 0;
}
+ssize_t
+readall(int fd, void *buf, size_t n)
+{
+ char *buffer = buf;
+ size_t ptr = 0;
+ ssize_t r;
+ for (;;) {
+ r = read(fd, buffer + ptr, n - ptr);
+ if (r < 0)
+ return -1;
+ if (r == 0)
+ break;
+ r += (size_t)r;
+ }
+ return r;
+}
+
static inline pid_t
enfork(int status)
diff --git a/src/util/io.h b/src/util/io.h
index 25f360c..ac326c8 100644
--- a/src/util/io.h
+++ b/src/util/io.h
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#define ewriteall(...) enwriteall(1, __VA_ARGS__)
+#define ereadall(...) enreadall(1, __VA_ARGS__)
int writeall(int fd, void *buf, size_t n);
@@ -10,3 +11,14 @@ enwriteall(int status, int fd, void *buf, size_t n, const char *fname)
if (writeall(fd, buf, n))
enprintf(status, "write %s:", fname);
}
+
+ssize_t readall(int fd, void *buf, size_t n);
+
+static inline size_t
+enreadall(int status, int fd, void *buf, size_t n, const char *fname)
+{
+ ssize_t r = readall(fd, buf, n);
+ if (r < 0)
+ enprintf(status, "read %s:", fname);
+ return (size_t)r;
+}