diff options
author | landley <landley@driftwood> | 2006-10-30 10:01:19 -0500 |
---|---|---|
committer | landley <landley@driftwood> | 2006-10-30 10:01:19 -0500 |
commit | 64b2e23ff1f2c2b8eacf6da5ac9a58054c8245cf (patch) | |
tree | d3ac3ebd2804eaef792264efdbaedd2f1d265571 /lib/functions.c | |
parent | 09ea7ac1a269db3c9a3b76840b37a7cb1eccbc24 (diff) | |
download | toybox-64b2e23ff1f2c2b8eacf6da5ac9a58054c8245cf.tar.gz |
Add reread(), readall(), and xread() on the bus ride in to work...
Diffstat (limited to 'lib/functions.c')
-rw-r--r-- | lib/functions.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/lib/functions.c b/lib/functions.c index e01053ba..44f21b55 100644 --- a/lib/functions.c +++ b/lib/functions.c @@ -150,9 +150,35 @@ FILE *xfopen(char *path, char *mode) 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) { |