changeset 50:63c168b65fa6

Add rewrite(), writeall(),and xwrite() to match the read versions.
author Rob Landley <rob@landley.net>
date Mon, 08 Jan 2007 02:49:39 -0500
parents bb4c38853a7d
children 63edd4de94dd
files lib/functions.c lib/lib.h
diffstat 2 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/lib/functions.c	Sun Jan 07 22:51:12 2007 -0500
+++ b/lib/functions.c	Mon Jan 08 02:49:39 2007 -0500
@@ -165,13 +165,21 @@
 // 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 @@
 	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);
--- a/lib/lib.h	Sun Jan 07 22:51:12 2007 -0500
+++ b/lib/lib.h	Mon Jan 08 02:49:39 2007 -0500
@@ -42,8 +42,11 @@
 int xopen(char *path, int flags);
 FILE *xfopen(char *path, char *mode);
 ssize_t reread(int fd, void *buf, size_t count);
+ssize_t rewrite(int fd, void *buf, size_t count);
 ssize_t readall(int fd, void *buf, size_t count);
+ssize_t writeall(int fd, void *buf, size_t count);
 void xread(int fd, char *buf, size_t count);
+void xwrite(int fd, char *buf, size_t count);
 char *xgetcwd(void);
 char *xabspath(char *path);
 struct string_list *find_in_path(char *path, char *filename);