summaryrefslogtreecommitdiffstats
path: root/tools/power/acpi/common/cmfsize.c
blob: 68b9ea86b86c30eb43afd3f8497b6511d4deba3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/******************************************************************************
 *
 * Module Name: cmfsize - Common get file size function
 *
 * Copyright (C) 2000 - 2023, Intel Corp.
 *
 *****************************************************************************/

#include <acpi/acpi.h>
#include "accommon.h"
#include "acapps.h"

#define _COMPONENT          ACPI_TOOLS
ACPI_MODULE_NAME("cmfsize")

/*******************************************************************************
 *
 * FUNCTION:    cm_get_file_size
 *
 * PARAMETERS:  file                    - Open file descriptor
 *
 * RETURN:      File Size. On error, -1 (ACPI_UINT32_MAX)
 *
 * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open.
 *              Does not disturb the current file pointer.
 *
 ******************************************************************************/
u32 cm_get_file_size(ACPI_FILE file)
{
	long file_size;
	long current_offset;
	acpi_status status;

	/* Save the current file pointer, seek to EOF to obtain file size */

	current_offset = ftell(file);
	if (current_offset < 0) {
		goto offset_error;
	}

	status = fseek(file, 0, SEEK_END);
	if (ACPI_FAILURE(status)) {
		goto seek_error;
	}

	file_size = ftell(file);
	if (file_size < 0) {
		goto offset_error;
	}

	/* Restore original file pointer */

	status = fseek(file, current_offset, SEEK_SET);
	if (ACPI_FAILURE(status)) {
		goto seek_error;
	}

	return ((u32)file_size);

offset_error:
	fprintf(stderr, "Could not get file offset\n");
	return (ACPI_UINT32_MAX);

seek_error:
	fprintf(stderr, "Could not set file offset\n");
	return (ACPI_UINT32_MAX);
}