aboutsummaryrefslogtreecommitdiff
path: root/util/cbfstool/common.c
diff options
context:
space:
mode:
authorPatrick Georgi <patrick@georgi-clan.de>2014-08-11 09:27:18 +0200
committerPatrick Georgi <patrick@georgi-clan.de>2014-08-12 11:53:24 +0200
commit32eeff4b6eaf5e0bf1979cc9d08ac60d8d011354 (patch)
tree056545cb95de31f837822dabfbdbb3c7f4dc9f16 /util/cbfstool/common.c
parent77b182a31a17e237da2350b0290301f5ce51d9d8 (diff)
util: replace fseek/ftell/rewind with fstat
It's a more direct approach to get the file size. Change-Id: If49df26bf4996bd556c675f3a673d0003b4adf89 Signed-off-by: Patrick Georgi <patrick@georgi-clan.de> Reviewed-on: http://review.coreboot.org/6594 Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich <rminnich@gmail.com>
Diffstat (limited to 'util/cbfstool/common.c')
-rw-r--r--util/cbfstool/common.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index b746c863d1..6778eb9401 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -22,6 +22,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include <libgen.h>
#include "common.h"
#include "cbfs.h"
@@ -40,6 +43,14 @@ int is_big_endian(void)
return 0;
}
+static off_t get_file_size(FILE *f)
+{
+ struct stat s;
+ int fd = fileno(f);
+ if (fd == -1) return -1;
+ if (fstat(fd, &s) == -1) return -1;
+ return s.st_size;
+}
/* Buffer and file I/O */
int buffer_create(struct buffer *buffer, size_t size, const char *name) {
@@ -59,10 +70,13 @@ int buffer_from_file(struct buffer *buffer, const char *filename) {
perror(filename);
return -1;
}
- fseek(fp, 0, SEEK_END);
- buffer->size = ftell(fp);
+ buffer->size = get_file_size(fp);
+ if (buffer->size == -1) {
+ fprintf(stderr, "could not determine size of %s\n", filename);
+ fclose(fp);
+ return -1;
+ }
buffer->name = strdup(filename);
- rewind(fp);
buffer->data = (char *)malloc(buffer->size);
assert(buffer->data);
if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {