diff mbox

hw/i386/pc: Use fstat in get_file_size()

Message ID 20160605081439.26794-1-fabien.siron@epita.fr
State New
Headers show

Commit Message

Fabien Siron June 5, 2016, 8:14 a.m. UTC
As mentioned in the comment, fstat is quite simpler than playing with
ftell() and fseek()

Signed-off-by: Fabien Siron <fabien.siron@epita.fr>
---
 hw/i386/pc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index e29ccc8..186dfe4 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -802,16 +802,9 @@  static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
 
 static long get_file_size(FILE *f)
 {
-    long where, size;
+    struct stat st;
 
-    /* XXX: on Unix systems, using fstat() probably makes more sense */
-
-    where = ftell(f);
-    fseek(f, 0, SEEK_END);
-    size = ftell(f);
-    fseek(f, where, SEEK_SET);
-
-    return size;
+    return fstat(fileno(f), &st) < 0 ? -1: st.st_size;
 }
 
 static void load_linux(PCMachineState *pcms,
@@ -835,7 +828,7 @@  static void load_linux(PCMachineState *pcms,
 
     /* load the kernel header */
     f = fopen(kernel_filename, "rb");
-    if (!f || !(kernel_size = get_file_size(f)) ||
+    if (!f || (kernel_size = get_file_size(f)) <= 0 ||
         fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
         MIN(ARRAY_SIZE(header), kernel_size)) {
         fprintf(stderr, "qemu: could not load kernel '%s': %s\n",