diff options
author | Rob Landley <rob@landley.net> | 2007-01-18 21:54:08 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2007-01-18 21:54:08 -0500 |
commit | 901637760b4206e968e73dd5ff7430c107c27b57 (patch) | |
tree | 61923e296b7728cb32261bd64eb1ad2261f5ad75 /lib | |
parent | 813840c9d199bf9e26d23291cf23e22a68e8ad82 (diff) | |
download | toybox-901637760b4206e968e73dd5ff7430c107c27b57.tar.gz |
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
shouldn't be a problem if we register signal handlers with sigaction(SA_RESTART)
Straighten out count and len (I generally consistently use "count" for the
current progress and "len" for the total, but this time I got them backwards
for some reason and don't want to confuse myself in future.)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/functions.c | 57 | ||||
-rw-r--r-- | lib/lib.h | 12 | ||||
-rw-r--r-- | lib/portability.h | 6 |
3 files changed, 32 insertions, 43 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 |