cmfsize.c revision 263851
1263851Sjkim/****************************************************************************** 2263851Sjkim * 3263851Sjkim * Module Name: cfsize - Common get file size function 4263851Sjkim * 5263851Sjkim *****************************************************************************/ 6263851Sjkim 7263851Sjkim/* 8263851Sjkim * Copyright (C) 2000 - 2014, Intel Corp. 9263851Sjkim * All rights reserved. 10263851Sjkim * 11263851Sjkim * Redistribution and use in source and binary forms, with or without 12263851Sjkim * modification, are permitted provided that the following conditions 13263851Sjkim * are met: 14263851Sjkim * 1. Redistributions of source code must retain the above copyright 15263851Sjkim * notice, this list of conditions, and the following disclaimer, 16263851Sjkim * without modification. 17263851Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18263851Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 19263851Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 20263851Sjkim * including a substantially similar Disclaimer requirement for further 21263851Sjkim * binary redistribution. 22263851Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 23263851Sjkim * of any contributors may be used to endorse or promote products derived 24263851Sjkim * from this software without specific prior written permission. 25263851Sjkim * 26263851Sjkim * Alternatively, this software may be distributed under the terms of the 27263851Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 28263851Sjkim * Software Foundation. 29263851Sjkim * 30263851Sjkim * NO WARRANTY 31263851Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32263851Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33263851Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34263851Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35263851Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36263851Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37263851Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38263851Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39263851Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40263851Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41263851Sjkim * POSSIBILITY OF SUCH DAMAGES. 42263851Sjkim */ 43263851Sjkim 44263851Sjkim#include "acpi.h" 45263851Sjkim#include "accommon.h" 46263851Sjkim#include "acapps.h" 47263851Sjkim#include <stdio.h> 48263851Sjkim 49263851Sjkim#define _COMPONENT ACPI_TOOLS 50263851Sjkim ACPI_MODULE_NAME ("cmfsize") 51263851Sjkim 52263851Sjkim 53263851Sjkim/******************************************************************************* 54263851Sjkim * 55263851Sjkim * FUNCTION: CmGetFileSize 56263851Sjkim * 57263851Sjkim * PARAMETERS: File - Open file descriptor 58263851Sjkim * 59263851Sjkim * RETURN: File Size. On error, -1 (ACPI_UINT32_MAX) 60263851Sjkim * 61263851Sjkim * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open. 62263851Sjkim * Does not disturb the current file pointer. Uses perror for 63263851Sjkim * error messages. 64263851Sjkim * 65263851Sjkim ******************************************************************************/ 66263851Sjkim 67263851SjkimUINT32 68263851SjkimCmGetFileSize ( 69263851Sjkim FILE *File) 70263851Sjkim{ 71263851Sjkim long FileSize; 72263851Sjkim long CurrentOffset; 73263851Sjkim 74263851Sjkim 75263851Sjkim /* Save the current file pointer, seek to EOF to obtain file size */ 76263851Sjkim 77263851Sjkim CurrentOffset = ftell (File); 78263851Sjkim if (CurrentOffset < 0) 79263851Sjkim { 80263851Sjkim goto OffsetError; 81263851Sjkim } 82263851Sjkim 83263851Sjkim if (fseek (File, 0, SEEK_END)) 84263851Sjkim { 85263851Sjkim goto SeekError; 86263851Sjkim } 87263851Sjkim 88263851Sjkim FileSize = ftell (File); 89263851Sjkim if (FileSize < 0) 90263851Sjkim { 91263851Sjkim goto OffsetError; 92263851Sjkim } 93263851Sjkim 94263851Sjkim /* Restore original file pointer */ 95263851Sjkim 96263851Sjkim if (fseek (File, CurrentOffset, SEEK_SET)) 97263851Sjkim { 98263851Sjkim goto SeekError; 99263851Sjkim } 100263851Sjkim 101263851Sjkim return ((UINT32) FileSize); 102263851Sjkim 103263851Sjkim 104263851SjkimOffsetError: 105263851Sjkim perror ("Could not get file offset"); 106263851Sjkim return (ACPI_UINT32_MAX); 107263851Sjkim 108263851SjkimSeekError: 109263851Sjkim perror ("Could not seek file"); 110263851Sjkim return (ACPI_UINT32_MAX); 111263851Sjkim} 112