Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / tools / acpidump / apfiles.c
blob5c4b0dce4dc599cc53328049f6c6c6240719c3de
1 /******************************************************************************
3 * Module Name: apfiles - File-related functions for acpidump utility
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "acpidump.h"
45 #include "acapps.h"
48 /******************************************************************************
50 * FUNCTION: ApOpenOutputFile
52 * PARAMETERS: Pathname - Output filename
54 * RETURN: Open file handle
56 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
57 * exists.
59 ******************************************************************************/
61 int
62 ApOpenOutputFile (
63 char *Pathname)
65 struct stat StatInfo;
66 FILE *File;
69 /* If file exists, prompt for overwrite */
71 if (!stat (Pathname, &StatInfo))
73 fprintf (stderr, "Target path already exists, overwrite? [y|n] ");
75 if (getchar () != 'y')
77 return (-1);
81 /* Point stdout to the file */
83 File = freopen (Pathname, "w", stdout);
84 if (!File)
86 perror ("Could not open output file");
87 return (-1);
90 /* Save the file and path */
92 Gbl_OutputFile = File;
93 Gbl_OutputFilename = Pathname;
94 return (0);
98 /******************************************************************************
100 * FUNCTION: ApWriteToBinaryFile
102 * PARAMETERS: Table - ACPI table to be written
103 * Instance - ACPI table instance no. to be written
105 * RETURN: Status
107 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
108 * filename from the table signature.
110 ******************************************************************************/
113 ApWriteToBinaryFile (
114 ACPI_TABLE_HEADER *Table,
115 UINT32 Instance)
117 char Filename[ACPI_NAME_SIZE + 16];
118 char InstanceStr [16];
119 FILE *File;
120 size_t Actual;
121 UINT32 TableLength;
124 /* Obtain table length */
126 TableLength = ApGetTableLength (Table);
128 /* Construct lower-case filename from the table local signature */
130 if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
132 ACPI_MOVE_NAME (Filename, AP_DUMP_SIG_RSDP);
134 else
136 ACPI_MOVE_NAME (Filename, Table->Signature);
138 Filename[0] = (char) ACPI_TOLOWER (Filename[0]);
139 Filename[1] = (char) ACPI_TOLOWER (Filename[1]);
140 Filename[2] = (char) ACPI_TOLOWER (Filename[2]);
141 Filename[3] = (char) ACPI_TOLOWER (Filename[3]);
142 Filename[ACPI_NAME_SIZE] = 0;
144 /* Handle multiple SSDTs - create different filenames for each */
146 if (Instance > 0)
148 sprintf (InstanceStr, "%u", Instance);
149 strcat (Filename, InstanceStr);
152 strcat (Filename, ACPI_TABLE_FILE_SUFFIX);
154 if (Gbl_VerboseMode)
156 fprintf (stderr,
157 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
158 Table->Signature, Filename, Table->Length, Table->Length);
161 /* Open the file and dump the entire table in binary mode */
163 File = fopen (Filename, "wb");
164 if (!File)
166 perror ("Could not open output file");
167 return (-1);
170 Actual = fwrite (Table, 1, TableLength, File);
171 if (Actual != TableLength)
173 perror ("Error writing binary output file");
174 fclose (File);
175 return (-1);
178 fclose (File);
179 return (0);
183 /******************************************************************************
185 * FUNCTION: ApGetTableFromFile
187 * PARAMETERS: Pathname - File containing the binary ACPI table
188 * OutFileSize - Where the file size is returned
190 * RETURN: Buffer containing the ACPI table. NULL on error.
192 * DESCRIPTION: Open a file and read it entirely into a new buffer
194 ******************************************************************************/
196 ACPI_TABLE_HEADER *
197 ApGetTableFromFile (
198 char *Pathname,
199 UINT32 *OutFileSize)
201 ACPI_TABLE_HEADER *Buffer = NULL;
202 FILE *File;
203 UINT32 FileSize;
204 size_t Actual;
207 /* Must use binary mode */
209 File = fopen (Pathname, "rb");
210 if (!File)
212 perror ("Could not open input file");
213 return (NULL);
216 /* Need file size to allocate a buffer */
218 FileSize = ApGetFileSize (File);
219 if (!FileSize)
221 fprintf (stderr,
222 "Could not get input file size: %s\n", Pathname);
223 goto Cleanup;
226 /* Allocate a buffer for the entire file */
228 Buffer = calloc (1, FileSize);
229 if (!Buffer)
231 fprintf (stderr,
232 "Could not allocate file buffer of size: %u\n", FileSize);
233 goto Cleanup;
236 /* Read the entire file */
238 Actual = fread (Buffer, 1, FileSize, File);
239 if (Actual != FileSize)
241 fprintf (stderr,
242 "Could not read input file: %s\n", Pathname);
243 free (Buffer);
244 Buffer = NULL;
245 goto Cleanup;
248 *OutFileSize = FileSize;
250 Cleanup:
251 fclose (File);
252 return (Buffer);
256 /******************************************************************************
258 * FUNCTION: ApGetFileSize
260 * PARAMETERS: File - Open file descriptor
262 * RETURN: File size in bytes
264 * DESCRIPTION: Get the size of an open file
266 ******************************************************************************/
268 UINT32
269 ApGetFileSize (
270 FILE *File)
272 UINT32 FileSize;
273 long Offset;
276 Offset = ftell (File);
277 if (fseek (File, 0, SEEK_END))
279 return (0);
282 /* Get size and restore file pointer */
284 FileSize = (UINT32) ftell (File);
285 if (fseek (File, Offset, SEEK_SET))
287 return (0);
290 return (FileSize);