diff options
Diffstat (limited to 'src/dt/common.h')
-rw-r--r-- | src/dt/common.h | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/dt/common.h b/src/dt/common.h index 6127bea..c5acd27 100644 --- a/src/dt/common.h +++ b/src/dt/common.h @@ -1,9 +1,14 @@ #ifndef __DT_COMMON_H #define __DT_COMMON_H +#include <fcntl.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> -#include <stdint.h> +#include <unistd.h> + +#include <sys/stat.h> + /** * container_of - cast a member of a structure out to the containing structure @@ -109,6 +114,40 @@ static inline size_t strlcpy(char *dest, const char *src, size_t size) return ret; } +static inline void *read_file(const char *filename, size_t *size) +{ + int fd; + struct stat s; + void *buf = NULL; + int ret; + + if (stat(filename, &s)) + return NULL; + + buf = xzalloc(s.st_size + 1); + + fd = open(filename, O_RDONLY); + if (fd < 0) + goto err_out; + + if (read(fd, buf, s.st_size) < s.st_size) + goto err_out1; + + close(fd); + + if (size) + *size = s.st_size; + + return buf; + +err_out1: + close(fd); +err_out: + free(buf); + + return NULL; +} + #define cpu_to_be32 __cpu_to_be32 #define be32_to_cpu __be32_to_cpu |