From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47176) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9UgE-0008Hn-FK for qemu-devel@nongnu.org; Sun, 05 Jun 2016 05:49:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9UgB-0006lm-9D for qemu-devel@nongnu.org; Sun, 05 Jun 2016 05:49:14 -0400 Received: from mail-db3on0068.outbound.protection.outlook.com ([157.55.234.68]:18804 helo=emea01-db3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9UgA-0006lE-No for qemu-devel@nongnu.org; Sun, 05 Jun 2016 05:49:11 -0400 From: Fabien Siron Date: Sun, 5 Jun 2016 08:14:39 +0000 Message-ID: <20160605081439.26794-1-fabien.siron@epita.fr> MIME-Version: 1.0 Content-Type: text/plain Subject: [Qemu-devel] [PATCH] hw/i386/pc: Use fstat in get_file_size() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-trivial@nongnu.org Cc: qemu-devel@nongnu.org, Fabien Siron As mentioned in the comment, fstat is quite simpler than playing with ftell() and fseek() Signed-off-by: Fabien Siron --- hw/i386/pc.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) 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", -- 2.8.3