diff options
author | Nikias Bassen | 2019-01-21 18:37:59 +0100 |
---|---|---|
committer | Nikias Bassen | 2019-01-21 18:37:59 +0100 |
commit | 8d389e546563c9bd346e6f5102e0f75d546b08e8 (patch) | |
tree | 0824226dd3fceba6425d79b34a6fca6d8483038a /src/common.c | |
parent | 661efc8f3a724fdd38174f3a886fd3aeb26fde37 (diff) | |
download | idevicerestore-8d389e546563c9bd346e6f5102e0f75d546b08e8.tar.gz idevicerestore-8d389e546563c9bd346e6f5102e0f75d546b08e8.tar.bz2 |
common: Use fstat() instead of fseeko() and ftello()
Diffstat (limited to 'src/common.c')
-rw-r--r-- | src/common.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/common.c b/src/common.c index 7bbe33a..c3d835f 100644 --- a/src/common.c +++ b/src/common.c @@ -31,6 +31,7 @@ #include <errno.h> #include <libgen.h> #include <time.h> +#include <sys/stat.h> #include "common.h" @@ -162,6 +163,8 @@ int read_file(const char* filename, void** data, size_t* size) { size_t length = 0; FILE* file = NULL; char* buffer = NULL; + struct stat fst; + debug("Reading data from %s\n", filename); *size = 0; @@ -169,13 +172,15 @@ int read_file(const char* filename, void** data, size_t* size) { file = fopen(filename, "rb"); if (file == NULL) { - error("read_file: File %s not found\n", filename); + error("read_file: cannot open %s: %s\n", filename, strerror(errno)); return -1; } - fseeko(file, 0, SEEK_END); - length = ftello(file); - rewind(file); + if (fstat(fileno(file), &fst) < 0) { + error("read_file: fstat: %s\n", strerror(errno)); + return -1; + } + length = fst.st_size; buffer = (char*) malloc(length); if (buffer == NULL) { |