Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: cmfsize - Common get file size function
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include <acpi/acpi.h>
0011 #include "accommon.h"
0012 #include "acapps.h"
0013 
0014 #define _COMPONENT          ACPI_TOOLS
0015 ACPI_MODULE_NAME("cmfsize")
0016 
0017 /*******************************************************************************
0018  *
0019  * FUNCTION:    cm_get_file_size
0020  *
0021  * PARAMETERS:  file                    - Open file descriptor
0022  *
0023  * RETURN:      File Size. On error, -1 (ACPI_UINT32_MAX)
0024  *
0025  * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open.
0026  *              Does not disturb the current file pointer.
0027  *
0028  ******************************************************************************/
0029 u32 cm_get_file_size(ACPI_FILE file)
0030 {
0031     long file_size;
0032     long current_offset;
0033     acpi_status status;
0034 
0035     /* Save the current file pointer, seek to EOF to obtain file size */
0036 
0037     current_offset = ftell(file);
0038     if (current_offset < 0) {
0039         goto offset_error;
0040     }
0041 
0042     status = fseek(file, 0, SEEK_END);
0043     if (ACPI_FAILURE(status)) {
0044         goto seek_error;
0045     }
0046 
0047     file_size = ftell(file);
0048     if (file_size < 0) {
0049         goto offset_error;
0050     }
0051 
0052     /* Restore original file pointer */
0053 
0054     status = fseek(file, current_offset, SEEK_SET);
0055     if (ACPI_FAILURE(status)) {
0056         goto seek_error;
0057     }
0058 
0059     return ((u32)file_size);
0060 
0061 offset_error:
0062     fprintf(stderr, "Could not get file offset\n");
0063     return (ACPI_UINT32_MAX);
0064 
0065 seek_error:
0066     fprintf(stderr, "Could not set file offset\n");
0067     return (ACPI_UINT32_MAX);
0068 }