Mercurial > hg > toybox
changeset 8:04f66da2bdbf
Add reread(), readall(), and xread() on the bus ride in to work...
author | landley@driftwood |
---|---|
date | Mon, 30 Oct 2006 10:01:19 -0500 |
parents | fc9c0503d5e2 |
children | 8f8a8ac59c14 |
files | lib/functions.c lib/lib.h |
diffstat | 2 files changed, 32 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/functions.c Mon Oct 30 01:38:00 2006 -0500 +++ b/lib/functions.c Mon Oct 30 10:01:19 2006 -0500 @@ -150,9 +150,35 @@ return f; } -// int xread(int fd, char *buf, int len) // Die if can't fill buffer -// int readall(int fd, char *buf, int len) // Keep reading until full or EOF -// int toy_read(int fd, char *buf, int len) // retry if interrupted +// 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); + if (len >= 0 || errno != EINTR) return len; + } +} + +// Keep reading until full or EOF +ssize_t readall(int fd, void *buf, size_t count) +{ + size_t len = 0; + while (len<count) { + int i = reread(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"); +} char *xgetcwd(void) {
--- a/lib/lib.h Mon Oct 30 01:38:00 2006 -0500 +++ b/lib/lib.h Mon Oct 30 10:01:19 2006 -0500 @@ -19,6 +19,9 @@ void xexec(char **argv); int xopen(char *path, int flags, int mode); FILE *xfopen(char *path, char *mode); +ssize_t reread(int fd, void *buf, size_t count); +ssize_t readall(int fd, void *buf, size_t count); +void xread(int fd, char *buf, size_t count); char *xgetcwd(void); char *find_in_path(char *path, char *filename); void utoa_to_buf(unsigned n, char *buf, unsigned buflen);