summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Kleine-Budde <mkl@pengutronix.de>2015-04-27 16:02:04 +0200
committerMarc Kleine-Budde <mkl@pengutronix.de>2015-04-28 14:20:04 +0200
commit954e383388f26682325080e8532b5ea8056dae16 (patch)
tree2dda0dbe90d7a9a616c45580452209a57ba069eb
parent5c8d221da14421f9d2f5ae6c48510f017de2eef4 (diff)
downloaddt-utils-954e383388f26682325080e8532b5ea8056dae16.tar.gz
dt-utils-954e383388f26682325080e8532b5ea8056dae16.tar.xz
libdt: read_file(): use read_full() instead of read() to read a file
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r--src/dt/common.h28
1 files changed, 27 insertions, 1 deletions
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 @@ static inline size_t strlcpy(char *dest, const char *src, size_t size)
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 @@ static inline void *read_file(const char *filename, size_t *size)
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);