oslibcfs.c revision 281075
1/****************************************************************************** 2 * 3 * Module Name: oslibcfs - C library OSL for file I/O 4 * 5 *****************************************************************************/ 6 7/* 8 * Copyright (C) 2000 - 2015, Intel Corp. 9 * All rights reserved. 10 * 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. 25 * 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. 29 * 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. 42 */ 43 44#include <contrib/dev/acpica/include/acpi.h> 45#include <stdio.h> 46#include <stdarg.h> 47 48#define _COMPONENT ACPI_OS_SERVICES 49 ACPI_MODULE_NAME ("oslibcfs") 50 51 52/******************************************************************************* 53 * 54 * FUNCTION: AcpiOsOpenFile 55 * 56 * PARAMETERS: Path - File path 57 * Modes - File operation type 58 * 59 * RETURN: File descriptor. 60 * 61 * DESCRIPTION: Open a file for reading (ACPI_FILE_READING) or/and writing 62 * (ACPI_FILE_WRITING). 63 * 64 ******************************************************************************/ 65 66ACPI_FILE 67AcpiOsOpenFile ( 68 const char *Path, 69 UINT8 Modes) 70{ 71 ACPI_FILE File; 72 UINT32 i = 0; 73 char ModesStr[4]; 74 75 76 if (Modes & ACPI_FILE_READING) 77 { 78 ModesStr[i++] = 'r'; 79 } 80 if (Modes & ACPI_FILE_WRITING) 81 { 82 ModesStr[i++] = 'w'; 83 } 84 if (Modes & ACPI_FILE_BINARY) 85 { 86 ModesStr[i++] = 'b'; 87 } 88 89 ModesStr[i++] = '\0'; 90 91 File = fopen (Path, ModesStr); 92 if (!File) 93 { 94 perror ("Could not open file"); 95 } 96 97 return (File); 98} 99 100 101/******************************************************************************* 102 * 103 * FUNCTION: AcpiOsCloseFile 104 * 105 * PARAMETERS: File - An open file descriptor 106 * 107 * RETURN: None. 108 * 109 * DESCRIPTION: Close a file opened via AcpiOsOpenFile. 110 * 111 ******************************************************************************/ 112 113void 114AcpiOsCloseFile ( 115 ACPI_FILE File) 116{ 117 fclose (File); 118} 119 120 121/******************************************************************************* 122 * 123 * FUNCTION: AcpiOsReadFile 124 * 125 * PARAMETERS: File - An open file descriptor 126 * Buffer - Data buffer 127 * Size - Data block size 128 * Count - Number of data blocks 129 * 130 * RETURN: Number of bytes actually read. 131 * 132 * DESCRIPTION: Read from a file. 133 * 134 ******************************************************************************/ 135 136int 137AcpiOsReadFile ( 138 ACPI_FILE File, 139 void *Buffer, 140 ACPI_SIZE Size, 141 ACPI_SIZE Count) 142{ 143 int Length; 144 145 146 Length = fread (Buffer, Size, Count, File); 147 if (Length < 0) 148 { 149 perror ("Error reading file"); 150 } 151 152 return (Length); 153} 154 155 156/******************************************************************************* 157 * 158 * FUNCTION: AcpiOsWriteFile 159 * 160 * PARAMETERS: File - An open file descriptor 161 * Buffer - Data buffer 162 * Size - Data block size 163 * Count - Number of data blocks 164 * 165 * RETURN: Number of bytes actually written. 166 * 167 * DESCRIPTION: Write to a file. 168 * 169 ******************************************************************************/ 170 171int 172AcpiOsWriteFile ( 173 ACPI_FILE File, 174 void *Buffer, 175 ACPI_SIZE Size, 176 ACPI_SIZE Count) 177{ 178 int Length; 179 180 181 Length = fwrite (Buffer, Size, Count, File); 182 if (Length < 0) 183 { 184 perror ("Error writing file"); 185 } 186 187 return (Length); 188} 189 190 191/******************************************************************************* 192 * 193 * FUNCTION: AcpiOsGetFileOffset 194 * 195 * PARAMETERS: File - An open file descriptor 196 * 197 * RETURN: Current file pointer position. 198 * 199 * DESCRIPTION: Get current file offset. 200 * 201 ******************************************************************************/ 202 203long 204AcpiOsGetFileOffset ( 205 ACPI_FILE File) 206{ 207 long Offset; 208 209 210 Offset = ftell (File); 211 return (Offset); 212} 213 214 215/******************************************************************************* 216 * 217 * FUNCTION: AcpiOsSetFileOffset 218 * 219 * PARAMETERS: File - An open file descriptor 220 * Offset - New file offset 221 * From - From begin/end of file 222 * 223 * RETURN: Status 224 * 225 * DESCRIPTION: Set current file offset. 226 * 227 ******************************************************************************/ 228 229ACPI_STATUS 230AcpiOsSetFileOffset ( 231 ACPI_FILE File, 232 long Offset, 233 UINT8 From) 234{ 235 int Ret = 0; 236 237 238 if (From == ACPI_FILE_BEGIN) 239 { 240 Ret = fseek (File, Offset, SEEK_SET); 241 } 242 if (From == ACPI_FILE_END) 243 { 244 Ret = fseek (File, Offset, SEEK_END); 245 } 246 247 if (Ret < 0) 248 { 249 return (AE_ERROR); 250 } 251 else 252 { 253 return (AE_OK); 254 } 255} 256