1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /****************************************************************************** 3 * 4 * Module Name: cfsize - Common get file size function 5 * 6 * Copyright (C) 2000 - 2019, Intel Corp. 7 * 8 *****************************************************************************/ 9 10 #include <acpi/acpi.h> 11 #include "accommon.h" 12 #include "acapps.h" 13 14 #define _COMPONENT ACPI_TOOLS 15 ACPI_MODULE_NAME("cmfsize") 16 17 /******************************************************************************* 18 * 19 * FUNCTION: cm_get_file_size 20 * 21 * PARAMETERS: file - Open file descriptor 22 * 23 * RETURN: File Size. On error, -1 (ACPI_UINT32_MAX) 24 * 25 * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open. 26 * Does not disturb the current file pointer. 27 * 28 ******************************************************************************/ 29 u32 cm_get_file_size(ACPI_FILE file) 30 { 31 long file_size; 32 long current_offset; 33 acpi_status status; 34 35 /* Save the current file pointer, seek to EOF to obtain file size */ 36 37 current_offset = ftell(file); 38 if (current_offset < 0) { 39 goto offset_error; 40 } 41 42 status = fseek(file, 0, SEEK_END); 43 if (ACPI_FAILURE(status)) { 44 goto seek_error; 45 } 46 47 file_size = ftell(file); 48 if (file_size < 0) { 49 goto offset_error; 50 } 51 52 /* Restore original file pointer */ 53 54 status = fseek(file, current_offset, SEEK_SET); 55 if (ACPI_FAILURE(status)) { 56 goto seek_error; 57 } 58 59 return ((u32)file_size); 60 61 offset_error: 62 fprintf(stderr, "Could not get file offset\n"); 63 return (ACPI_UINT32_MAX); 64 65 seek_error: 66 fprintf(stderr, "Could not set file offset\n"); 67 return (ACPI_UINT32_MAX); 68 } 69