cmfsize.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acapps.h"
  12. #define _COMPONENT ACPI_TOOLS
  13. ACPI_MODULE_NAME("cmfsize")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: cm_get_file_size
  17. *
  18. * PARAMETERS: file - Open file descriptor
  19. *
  20. * RETURN: File Size. On error, -1 (ACPI_UINT32_MAX)
  21. *
  22. * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open.
  23. * Does not disturb the current file pointer.
  24. *
  25. ******************************************************************************/
  26. u32 cm_get_file_size(ACPI_FILE file)
  27. {
  28. long file_size;
  29. long current_offset;
  30. acpi_status status;
  31. /* Save the current file pointer, seek to EOF to obtain file size */
  32. current_offset = ftell(file);
  33. if (current_offset < 0) {
  34. goto offset_error;
  35. }
  36. status = fseek(file, 0, SEEK_END);
  37. if (ACPI_FAILURE(status)) {
  38. goto seek_error;
  39. }
  40. file_size = ftell(file);
  41. if (file_size < 0) {
  42. goto offset_error;
  43. }
  44. /* Restore original file pointer */
  45. status = fseek(file, current_offset, SEEK_SET);
  46. if (ACPI_FAILURE(status)) {
  47. goto seek_error;
  48. }
  49. return ((u32)file_size);
  50. offset_error:
  51. fprintf(stderr, "Could not get file offset\n");
  52. return (ACPI_UINT32_MAX);
  53. seek_error:
  54. fprintf(stderr, "Could not set file offset\n");
  55. return (ACPI_UINT32_MAX);
  56. }