diff --git a/src/dt/common.h b/src/dt/common.h index c5acd27..94189ad 100644 --- a/src/dt/common.h +++ b/src/dt/common.h @@ -114,6 +114,32 @@ return ret; } +/* + * read_full - read from filedescriptor + * + * Like read, but this function only returns less bytes than + * requested when the end of file is reached. + */ +static inline int read_full(int fd, void *buf, size_t size) +{ + size_t insize = size; + int now; + int total = 0; + + while (size) { + now = read(fd, buf, size); + if (now == 0) + return total; + if (now < 0) + return now; + total += now; + size -= now; + buf += now; + } + + return insize; +} + static inline void *read_file(const char *filename, size_t *size) { int fd; @@ -130,7 +156,7 @@ if (fd < 0) goto err_out; - if (read(fd, buf, s.st_size) < s.st_size) + if (read_full(fd, buf, s.st_size) < s.st_size) goto err_out1; close(fd);