xref: /illumos-gate/usr/src/cmd/acpi/common/cmfsize.c (revision bc36eafd)
17b1019a6SJerry Jelinek /******************************************************************************
27b1019a6SJerry Jelinek  *
3*bc36eafdSMike Gerdts  * Module Name: cfsize - Common get file size function
47b1019a6SJerry Jelinek  *
57b1019a6SJerry Jelinek  *****************************************************************************/
67b1019a6SJerry Jelinek 
77b1019a6SJerry Jelinek /*
87b1019a6SJerry Jelinek  * Copyright (C) 2000 - 2016, Intel Corp.
97b1019a6SJerry Jelinek  * All rights reserved.
107b1019a6SJerry Jelinek  *
117b1019a6SJerry Jelinek  * Redistribution and use in source and binary forms, with or without
127b1019a6SJerry Jelinek  * modification, are permitted provided that the following conditions
137b1019a6SJerry Jelinek  * are met:
147b1019a6SJerry Jelinek  * 1. Redistributions of source code must retain the above copyright
157b1019a6SJerry Jelinek  *    notice, this list of conditions, and the following disclaimer,
167b1019a6SJerry Jelinek  *    without modification.
177b1019a6SJerry Jelinek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
187b1019a6SJerry Jelinek  *    substantially similar to the "NO WARRANTY" disclaimer below
197b1019a6SJerry Jelinek  *    ("Disclaimer") and any redistribution must be conditioned upon
207b1019a6SJerry Jelinek  *    including a substantially similar Disclaimer requirement for further
217b1019a6SJerry Jelinek  *    binary redistribution.
227b1019a6SJerry Jelinek  * 3. Neither the names of the above-listed copyright holders nor the names
237b1019a6SJerry Jelinek  *    of any contributors may be used to endorse or promote products derived
247b1019a6SJerry Jelinek  *    from this software without specific prior written permission.
257b1019a6SJerry Jelinek  *
267b1019a6SJerry Jelinek  * Alternatively, this software may be distributed under the terms of the
277b1019a6SJerry Jelinek  * GNU General Public License ("GPL") version 2 as published by the Free
287b1019a6SJerry Jelinek  * Software Foundation.
297b1019a6SJerry Jelinek  *
307b1019a6SJerry Jelinek  * NO WARRANTY
317b1019a6SJerry Jelinek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
327b1019a6SJerry Jelinek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
337b1019a6SJerry Jelinek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
347b1019a6SJerry Jelinek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
357b1019a6SJerry Jelinek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
367b1019a6SJerry Jelinek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
377b1019a6SJerry Jelinek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
387b1019a6SJerry Jelinek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
397b1019a6SJerry Jelinek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
407b1019a6SJerry Jelinek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
417b1019a6SJerry Jelinek  * POSSIBILITY OF SUCH DAMAGES.
427b1019a6SJerry Jelinek  */
437b1019a6SJerry Jelinek 
447b1019a6SJerry Jelinek #include "acpi.h"
457b1019a6SJerry Jelinek #include "accommon.h"
46*bc36eafdSMike Gerdts #include "acapps.h"
47*bc36eafdSMike Gerdts #include <stdio.h>
487b1019a6SJerry Jelinek 
49*bc36eafdSMike Gerdts #define _COMPONENT          ACPI_TOOLS
50*bc36eafdSMike Gerdts         ACPI_MODULE_NAME    ("cmfsize")
517b1019a6SJerry Jelinek 
527b1019a6SJerry Jelinek 
537b1019a6SJerry Jelinek /*******************************************************************************
547b1019a6SJerry Jelinek  *
55*bc36eafdSMike Gerdts  * FUNCTION:    CmGetFileSize
567b1019a6SJerry Jelinek  *
57*bc36eafdSMike Gerdts  * PARAMETERS:  File                    - Open file descriptor
587b1019a6SJerry Jelinek  *
59*bc36eafdSMike Gerdts  * RETURN:      File Size. On error, -1 (ACPI_UINT32_MAX)
607b1019a6SJerry Jelinek  *
61*bc36eafdSMike Gerdts  * DESCRIPTION: Get the size of a file. Uses seek-to-EOF. File must be open.
62*bc36eafdSMike Gerdts  *              Does not disturb the current file pointer.
637b1019a6SJerry Jelinek  *
647b1019a6SJerry Jelinek  ******************************************************************************/
657b1019a6SJerry Jelinek 
66*bc36eafdSMike Gerdts UINT32
67*bc36eafdSMike Gerdts CmGetFileSize (
68*bc36eafdSMike Gerdts     ACPI_FILE               File)
697b1019a6SJerry Jelinek {
70*bc36eafdSMike Gerdts     long                    FileSize;
71*bc36eafdSMike Gerdts     long                    CurrentOffset;
72*bc36eafdSMike Gerdts     ACPI_STATUS             Status;
737b1019a6SJerry Jelinek 
747b1019a6SJerry Jelinek 
75*bc36eafdSMike Gerdts     /* Save the current file pointer, seek to EOF to obtain file size */
767b1019a6SJerry Jelinek 
77*bc36eafdSMike Gerdts     CurrentOffset = AcpiOsGetFileOffset (File);
78*bc36eafdSMike Gerdts     if (CurrentOffset < 0)
79*bc36eafdSMike Gerdts     {
80*bc36eafdSMike Gerdts         goto OffsetError;
81*bc36eafdSMike Gerdts     }
827b1019a6SJerry Jelinek 
83*bc36eafdSMike Gerdts     Status = AcpiOsSetFileOffset (File, 0, ACPI_FILE_END);
84*bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
85*bc36eafdSMike Gerdts     {
86*bc36eafdSMike Gerdts         goto SeekError;
87*bc36eafdSMike Gerdts     }
887b1019a6SJerry Jelinek 
89*bc36eafdSMike Gerdts     FileSize = AcpiOsGetFileOffset (File);
90*bc36eafdSMike Gerdts     if (FileSize < 0)
917b1019a6SJerry Jelinek     {
92*bc36eafdSMike Gerdts         goto OffsetError;
937b1019a6SJerry Jelinek     }
947b1019a6SJerry Jelinek 
95*bc36eafdSMike Gerdts     /* Restore original file pointer */
96*bc36eafdSMike Gerdts 
97*bc36eafdSMike Gerdts     Status = AcpiOsSetFileOffset (File, CurrentOffset, ACPI_FILE_BEGIN);
98*bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
997b1019a6SJerry Jelinek     {
100*bc36eafdSMike Gerdts         goto SeekError;
1017b1019a6SJerry Jelinek     }
1027b1019a6SJerry Jelinek 
103*bc36eafdSMike Gerdts     return ((UINT32) FileSize);
104*bc36eafdSMike Gerdts 
105*bc36eafdSMike Gerdts 
106*bc36eafdSMike Gerdts OffsetError:
107*bc36eafdSMike Gerdts     AcpiLogError ("Could not get file offset");
108*bc36eafdSMike Gerdts     return (ACPI_UINT32_MAX);
109*bc36eafdSMike Gerdts 
110*bc36eafdSMike Gerdts SeekError:
111*bc36eafdSMike Gerdts     AcpiLogError ("Could not set file offset");
112*bc36eafdSMike Gerdts     return (ACPI_UINT32_MAX);
1137b1019a6SJerry Jelinek }
114