diff --git a/misc/subr.c b/misc/subr.c index 729d588..e16a7dc 100644 --- a/misc/subr.c +++ b/misc/subr.c @@ -174,3 +174,35 @@ rand_between(long min, long max) { return min + (long)rint((double)random() * (max - min) / RAND_MAX); } + +int +write_fully(int fd, const void *buf, size_t len) +{ + ssize_t nw; + size_t off = 0; + + while (off < len) { + nw = write(fd, buf + off, len - off); + if (nw == -1) + return errno; + off += nw; + } + return 0; +} + +int +read_fully(int fd, void *buf, size_t len) +{ + ssize_t nread; + size_t off = 0; + + while (off < len) { + nread = read(fd, buf + off, len - off); + if (nread == 0) + return ECONNRESET; + else if (nread == -1) + return errno; + off += nread; + } + return 0; +} diff --git a/misc/subr.h b/misc/subr.h index 8ad42c2..0a6223a 100644 --- a/misc/subr.h +++ b/misc/subr.h @@ -24,4 +24,7 @@ int canon_path(const char *path, char **res); long rand_between(long min, long max); +int read_fully(int fd, void *buf, size_t len); +int write_fully(int fd, const void *buf, size_t len); + #endif