oslibcfs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /******************************************************************************
  2. *
  3. * Module Name: oslibcfs - C library OSL for file I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include <stdio.h>
  44. #include <stdarg.h>
  45. #define _COMPONENT ACPI_OS_SERVICES
  46. ACPI_MODULE_NAME("oslibcfs")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_os_open_file
  50. *
  51. * PARAMETERS: path - File path
  52. * modes - File operation type
  53. *
  54. * RETURN: File descriptor.
  55. *
  56. * DESCRIPTION: Open a file for reading (ACPI_FILE_READING) or/and writing
  57. * (ACPI_FILE_WRITING).
  58. *
  59. ******************************************************************************/
  60. ACPI_FILE acpi_os_open_file(const char *path, u8 modes)
  61. {
  62. ACPI_FILE file;
  63. u32 i = 0;
  64. char modes_str[4];
  65. if (modes & ACPI_FILE_READING) {
  66. modes_str[i++] = 'r';
  67. }
  68. if (modes & ACPI_FILE_WRITING) {
  69. modes_str[i++] = 'w';
  70. }
  71. if (modes & ACPI_FILE_BINARY) {
  72. modes_str[i++] = 'b';
  73. }
  74. modes_str[i++] = '\0';
  75. file = fopen(path, modes_str);
  76. if (!file) {
  77. perror("Could not open file");
  78. }
  79. return (file);
  80. }
  81. /*******************************************************************************
  82. *
  83. * FUNCTION: acpi_os_close_file
  84. *
  85. * PARAMETERS: file - An open file descriptor
  86. *
  87. * RETURN: None.
  88. *
  89. * DESCRIPTION: Close a file opened via acpi_os_open_file.
  90. *
  91. ******************************************************************************/
  92. void acpi_os_close_file(ACPI_FILE file)
  93. {
  94. fclose(file);
  95. }
  96. /*******************************************************************************
  97. *
  98. * FUNCTION: acpi_os_read_file
  99. *
  100. * PARAMETERS: file - An open file descriptor
  101. * buffer - Data buffer
  102. * size - Data block size
  103. * count - Number of data blocks
  104. *
  105. * RETURN: Number of bytes actually read.
  106. *
  107. * DESCRIPTION: Read from a file.
  108. *
  109. ******************************************************************************/
  110. int
  111. acpi_os_read_file(ACPI_FILE file, void *buffer, acpi_size size, acpi_size count)
  112. {
  113. int length;
  114. length = fread(buffer, size, count, file);
  115. if (length < 0) {
  116. perror("Error reading file");
  117. }
  118. return (length);
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_os_write_file
  123. *
  124. * PARAMETERS: file - An open file descriptor
  125. * buffer - Data buffer
  126. * size - Data block size
  127. * count - Number of data blocks
  128. *
  129. * RETURN: Number of bytes actually written.
  130. *
  131. * DESCRIPTION: Write to a file.
  132. *
  133. ******************************************************************************/
  134. int
  135. acpi_os_write_file(ACPI_FILE file,
  136. void *buffer, acpi_size size, acpi_size count)
  137. {
  138. int length;
  139. length = fwrite(buffer, size, count, file);
  140. if (length < 0) {
  141. perror("Error writing file");
  142. }
  143. return (length);
  144. }
  145. /*******************************************************************************
  146. *
  147. * FUNCTION: acpi_os_get_file_offset
  148. *
  149. * PARAMETERS: file - An open file descriptor
  150. *
  151. * RETURN: Current file pointer position.
  152. *
  153. * DESCRIPTION: Get current file offset.
  154. *
  155. ******************************************************************************/
  156. long acpi_os_get_file_offset(ACPI_FILE file)
  157. {
  158. long offset;
  159. offset = ftell(file);
  160. return (offset);
  161. }
  162. /*******************************************************************************
  163. *
  164. * FUNCTION: acpi_os_set_file_offset
  165. *
  166. * PARAMETERS: file - An open file descriptor
  167. * offset - New file offset
  168. * from - From begin/end of file
  169. *
  170. * RETURN: Status
  171. *
  172. * DESCRIPTION: Set current file offset.
  173. *
  174. ******************************************************************************/
  175. acpi_status acpi_os_set_file_offset(ACPI_FILE file, long offset, u8 from)
  176. {
  177. int ret = 0;
  178. if (from == ACPI_FILE_BEGIN) {
  179. ret = fseek(file, offset, SEEK_SET);
  180. }
  181. if (from == ACPI_FILE_END) {
  182. ret = fseek(file, offset, SEEK_END);
  183. }
  184. if (ret < 0) {
  185. return (AE_ERROR);
  186. } else {
  187. return (AE_OK);
  188. }
  189. }