aboutsummaryrefslogtreecommitdiff
path: root/lib/functions.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-01-08 02:49:39 -0500
committerRob Landley <rob@landley.net>2007-01-08 02:49:39 -0500
commitf3e452a35ab5fd1808be7a50652b54dc3222ba6e (patch)
tree6e5e62925a73f3c66285c2adf510d0b50be34294 /lib/functions.c
parent1322beb384ea43a15c17f8229e7db070949dd331 (diff)
downloadtoybox-f3e452a35ab5fd1808be7a50652b54dc3222ba6e.tar.gz
Add rewrite(), writeall(),and xwrite() to match the read versions.
Diffstat (limited to 'lib/functions.c')
-rw-r--r--lib/functions.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/lib/functions.c b/lib/functions.c
index 990c34fa..e37887ed 100644
--- a/lib/functions.c
+++ b/lib/functions.c
@@ -165,13 +165,21 @@ FILE *xfopen(char *path, char *mode)
// Read from file handle, retrying if interrupted.
ssize_t reread(int fd, void *buf, size_t count)
{
- ssize_t len;
for (;;) {
- len = read(fd, buf, count);
+ ssize_t len = read(fd, buf, count);
if (len >= 0 || errno != EINTR) return len;
}
}
+// Write to file handle, retrying if interrupted.
+ssize_t rewrite(int fd, void *buf, size_t count)
+{
+ for (;;) {
+ ssize_t len = write(fd, buf, count);
+ if (len >= 0 || errno != EINTR) return len;
+ }
+}
+
// Keep reading until full or EOF
ssize_t readall(int fd, void *buf, size_t count)
{
@@ -186,12 +194,31 @@ ssize_t readall(int fd, void *buf, size_t count)
return count;
}
+// Keep writing until done or EOF
+ssize_t writeall(int fd, void *buf, size_t count)
+{
+ size_t len = 0;
+ while (len<count) {
+ int i = rewrite(fd, buf, count);
+ if (!i) return len;
+ if (i<0) return i;
+ count += i;
+ }
+
+ return count;
+}
+
// Die if we can't fill a buffer
void xread(int fd, char *buf, size_t count)
{
if (count != readall(fd, buf, count)) perror_exit("xread");
}
+void xwrite(int fd, char *buf, size_t count)
+{
+ if (count != writeall(fd, buf, count)) perror_exit("xwrite");
+}
+
char *xgetcwd(void)
{
char *buf = getcwd(NULL, 0);