From 8d389e546563c9bd346e6f5102e0f75d546b08e8 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 21 Jan 2019 18:37:59 +0100 Subject: common: Use fstat() instead of fseeko() and ftello() --- src/common.c | 13 +++++++++---- 1 file 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 #include #include +#include #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) { -- cgit v1.1-32-gdbae