diff options
-rw-r--r-- | lib/functions.c | 57 | ||||
-rw-r--r-- | lib/lib.h | 12 | ||||
-rw-r--r-- | lib/portability.h | 6 | ||||
-rw-r--r-- | toys.h | 3 | ||||
-rw-r--r-- | toys/catv.c | 2 |
5 files changed, 35 insertions, 45 deletions
diff --git a/lib/functions.c b/lib/functions.c index 98a344a2..ce3edccc 100644 --- a/lib/functions.c +++ b/lib/functions.c @@ -174,30 +174,12 @@ FILE *xfopen(char *path, char *mode) return f; } -// Read from file handle, retrying if interrupted. -ssize_t reread(int fd, void *buf, size_t count) -{ - for (;;) { - 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) +ssize_t readall(int fd, void *buf, size_t len) { - size_t len = 0; - while (len<count) { - int i = reread(fd, buf, count); + size_t count = 0; + while (count<len) { + int i = read(fd, buf+count, len-count); if (!i) return len; if (i<0) return i; count += i; @@ -207,13 +189,12 @@ ssize_t readall(int fd, void *buf, size_t count) } // Keep writing until done or EOF -ssize_t writeall(int fd, void *buf, size_t count) +ssize_t writeall(int fd, void *buf, size_t len) { - size_t len = 0; - while (len<count) { - int i = rewrite(fd, buf, count); - if (!i) return len; - if (i<0) return i; + size_t count = 0; + while (count<len) { + int i = write(fd, buf+count, len-count); + if (i<1) return i; count += i; } @@ -221,22 +202,26 @@ ssize_t writeall(int fd, void *buf, size_t count) } // Die if there's an error other than EOF. -size_t xread(int fd, void *buf, size_t count) +size_t xread(int fd, void *buf, size_t len) { - count = reread(fd, buf, count); - if (count < 0) perror_exit("xread"); + len = read(fd, buf, len); + if (len < 0) perror_exit("xread"); - return count; + return len; } -void xreadall(int fd, void *buf, size_t count) +void xreadall(int fd, void *buf, size_t len) { - if (count != readall(fd, buf, count)) perror_exit("xreadall"); + if (len != readall(fd, buf, len)) perror_exit("xreadall"); } -void xwrite(int fd, void *buf, size_t count) +// There's no xwriteall(), just xwrite(). When we read, there may or may not +// be more data waiting. When we write, there is data and it had better go +// somewhere. + +void xwrite(int fd, void *buf, size_t len) { - if (count != writeall(fd, buf, count)) perror_exit("xwrite"); + if (len != writeall(fd, buf, len)) perror_exit("xwrite"); } char *xgetcwd(void) @@ -43,13 +43,11 @@ void xaccess(char *path, int flags); int xcreate(char *path, int flags, int mode); 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); -size_t xread(int fd, void *buf, size_t count); -void xreadall(int fd, void *buf, size_t count); -void xwrite(int fd, void *buf, size_t count); +ssize_t readall(int fd, void *buf, size_t len); +ssize_t writeall(int fd, void *buf, size_t len); +size_t xread(int fd, void *buf, size_t len); +void xreadall(int fd, void *buf, size_t len); +void xwrite(int fd, void *buf, size_t len); char *xgetcwd(void); char *xabspath(char *path); struct string_list *find_in_path(char *path, char *filename); diff --git a/lib/portability.h b/lib/portability.h index dca0ddea..5aa4a0bf 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -1,3 +1,9 @@ + +// Humor glibc to get dprintf, then #define it to something more portable. +#define _GNU_SOURCE +#include <stdio.h> +#define fdprintf(...) dprintf(__VA_ARGS__) + #include <endian.h> #if __BYTE_ORDER == __BIG_ENDIAN @@ -6,6 +6,8 @@ * Licensed under GPL version 2, see file LICENSE in this tarball for details. */ +#include "lib/portability.h" + #include <ctype.h> #include <errno.h> #include <fcntl.h> @@ -26,7 +28,6 @@ #include <unistd.h> #include "lib/lib.h" -#include "lib/portability.h" #include "gen_config.h" #include "toys/toylist.h" diff --git a/toys/catv.c b/toys/catv.c index e37f307b..9097c065 100644 --- a/toys/catv.c +++ b/toys/catv.c @@ -27,7 +27,7 @@ int catv_main(void) else for(;;) { int i, res; - res = reread(fd, toybuf, sizeof(toybuf)); + res = read(fd, toybuf, sizeof(toybuf)); if (res < 0) retval = EXIT_FAILURE; if (res < 1) break; for (i=0; i<res; i++) { |