oslibcfs.c revision 306536
1/****************************************************************************** 2 * 3 * Module Name: oslibcfs - C library OSL for file I/O 4 * 5 *****************************************************************************/ 6 7/* 8 * Copyright (C) 2000 - 2016, 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 85 if (Modes & ACPI_FILE_BINARY) 86 { 87 ModesStr[i++] = 'b'; 88 } 89 90 ModesStr[i++] = '\0'; 91 92 File = fopen (Path, ModesStr); 93 if (!File) 94 { 95 perror ("Could not open file"); 96 } 97 98 return (File); 99} 100 101 102/******************************************************************************* 103 * 104 * FUNCTION: AcpiOsCloseFile 105 * 106 * PARAMETERS: File - An open file descriptor 107 * 108 * RETURN: None. 109 * 110 * DESCRIPTION: Close a file opened via AcpiOsOpenFile. 111 * 112 ******************************************************************************/ 113 114void 115AcpiOsCloseFile ( 116 ACPI_FILE File) 117{ 118 119 fclose (File); 120} 121 122 123/******************************************************************************* 124 * 125 * FUNCTION: AcpiOsReadFile 126 * 127 * PARAMETERS: File - An open file descriptor 128 * Buffer - Data buffer 129 * Size - Data block size 130 * Count - Number of data blocks 131 * 132 * RETURN: Number of bytes actually read. 133 * 134 * DESCRIPTION: Read from a file. 135 * 136 ******************************************************************************/ 137 138int 139AcpiOsReadFile ( 140 ACPI_FILE File, 141 void *Buffer, 142 ACPI_SIZE Size, 143 ACPI_SIZE Count) 144{ 145 int Length; 146 147 148 Length = fread (Buffer, Size, Count, File); 149 if (Length < 0) 150 { 151 perror ("Error reading file"); 152 } 153 154 return (Length); 155} 156 157 158/******************************************************************************* 159 * 160 * FUNCTION: AcpiOsWriteFile 161 * 162 * PARAMETERS: File - An open file descriptor 163 * Buffer - Data buffer 164 * Size - Data block size 165 * Count - Number of data blocks 166 * 167 * RETURN: Number of bytes actually written. 168 * 169 * DESCRIPTION: Write to a file. 170 * 171 ******************************************************************************/ 172 173int 174AcpiOsWriteFile ( 175 ACPI_FILE File, 176 void *Buffer, 177 ACPI_SIZE Size, 178 ACPI_SIZE Count) 179{ 180 int Length; 181 182 183 Length = fwrite (Buffer, Size, Count, File); 184 if (Length < 0) 185 { 186 perror ("Error writing file"); 187 } 188 189 return (Length); 190} 191 192 193/******************************************************************************* 194 * 195 * FUNCTION: AcpiOsGetFileOffset 196 * 197 * PARAMETERS: File - An open file descriptor 198 * 199 * RETURN: Current file pointer position. 200 * 201 * DESCRIPTION: Get current file offset. 202 * 203 ******************************************************************************/ 204 205long 206AcpiOsGetFileOffset ( 207 ACPI_FILE File) 208{ 209 long Offset; 210 211 212 Offset = ftell (File); 213 return (Offset); 214} 215 216 217/******************************************************************************* 218 * 219 * FUNCTION: AcpiOsSetFileOffset 220 * 221 * PARAMETERS: File - An open file descriptor 222 * Offset - New file offset 223 * From - From begin/end of file 224 * 225 * RETURN: Status 226 * 227 * DESCRIPTION: Set current file offset. 228 * 229 ******************************************************************************/ 230 231ACPI_STATUS 232AcpiOsSetFileOffset ( 233 ACPI_FILE File, 234 long Offset, 235 UINT8 From) 236{ 237 int Ret = 0; 238 239 240 if (From == ACPI_FILE_BEGIN) 241 { 242 Ret = fseek (File, Offset, SEEK_SET); 243 } 244 245 if (From == ACPI_FILE_END) 246 { 247 Ret = fseek (File, Offset, SEEK_END); 248 } 249 250 if (Ret < 0) 251 { 252 return (AE_ERROR); 253 } 254 else 255 { 256 return (AE_OK); 257 } 258} 259