fcntl.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __FCNTL_C
  18. #define __FCNTL_C
  19. #define O_RDONLY 0
  20. #define O_WRONLY 1
  21. #define O_RDWR 2
  22. #define O_CREAT 00100
  23. #define O_EXCL 00200
  24. #define O_TRUNC 001000
  25. #define O_APPEND 002000
  26. #define S_IXUSR 00100
  27. #define S_IWUSR 00200
  28. #define S_IRUSR 00400
  29. #define S_IRWXU 00700
  30. #include <uefi/uefi.c>
  31. void free(void* l);
  32. int __open(struct efi_file_protocol* _rootdir, char* name, long mode, long attributes)
  33. {
  34. struct efi_file_protocol* new_handle;
  35. char* wide_name = _posix_path_to_uefi(name);
  36. unsigned rval = __uefi_5(_rootdir, &new_handle, wide_name, mode, attributes, _rootdir->open);
  37. free(wide_name);
  38. if(rval != EFI_SUCCESS)
  39. {
  40. return -1;
  41. }
  42. return new_handle;
  43. }
  44. void _set_file_size(struct efi_file_protocol* f, unsigned new_size)
  45. {
  46. /* Preallocate some extra space for file_name */
  47. size_t file_info_size = sizeof(struct efi_file_info);
  48. struct efi_file_info* file_info = calloc(1, file_info_size);
  49. unsigned r = __uefi_4(f, &EFI_FILE_INFO_GUID, &file_info_size, file_info, f->get_info);
  50. if(r != EFI_SUCCESS)
  51. {
  52. free(file_info);
  53. return;
  54. }
  55. file_info->file_size = new_size;
  56. __uefi_4(f, &EFI_FILE_INFO_GUID, file_info_size, file_info, f->set_info);
  57. free(file_info);
  58. }
  59. int _open(char* name, int flag, int mode)
  60. {
  61. long mode = 0;
  62. long attributes = 0;
  63. if ((flag == (O_WRONLY | O_CREAT | O_TRUNC)) || (flag == (O_RDWR | O_CREAT | O_EXCL)))
  64. {
  65. mode = EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ;
  66. }
  67. else
  68. { /* Everything else is a read */
  69. mode = EFI_FILE_MODE_READ;
  70. attributes = EFI_FILE_READ_ONLY;
  71. }
  72. int handle = __open(_rootdir, name, mode, attributes);
  73. if (flag & O_TRUNC)
  74. {
  75. _set_file_size(handle, 0);
  76. }
  77. return handle;
  78. }
  79. #define STDIN_FILENO 0
  80. #define STDOUT_FILENO 1
  81. #define STDERR_FILENO 2
  82. #endif