file.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // CONSTANT stdin 0
  18. // CONSTANT stdout 1
  19. // CONSTANT stderr 2
  20. // CONSTANT EOF 0xFFFFFFFF
  21. int fgetc(FILE* f)
  22. {
  23. asm("LOAD_EFFECTIVE_ADDRESS_rdi %8"
  24. "LOAD_INTEGER_rdi"
  25. "LOAD_IMMEDIATE_rax %0"
  26. "PUSH_RAX"
  27. "LOAD_EFFECTIVE_ADDRESS_rsi %0"
  28. "LOAD_IMMEDIATE_rdx %1"
  29. "SYSCALL"
  30. "LOAD_IMMEDIATE_rbx %0"
  31. "CMP"
  32. "POP_RAX"
  33. "JUMP_NE %FUNCTION_fgetc_Done"
  34. "LOAD_IMMEDIATE_rax %-1"
  35. ":FUNCTION_fgetc_Done");
  36. }
  37. void fputc(char s, FILE* f)
  38. {
  39. asm("LOAD_IMMEDIATE_rax %1"
  40. "LOAD_EFFECTIVE_ADDRESS_rdi %8"
  41. "LOAD_INTEGER_rdi"
  42. "LOAD_EFFECTIVE_ADDRESS_rsi %16"
  43. "LOAD_IMMEDIATE_rdx %1"
  44. "SYSCALL");
  45. }
  46. /* Important values needed for open */
  47. // CONSTANT O_RDONLY 0
  48. // CONSTANT O_WRONLY 1
  49. // CONSTANT O_RDWR 2
  50. // CONSTANT O_CREAT 64
  51. // CONSTANT O_TRUNC 512
  52. /* 00700 in octal is 448*/
  53. // CONSTANT S_IRWXU 448
  54. /* 00100 in octal is 64 */
  55. // CONSTANT S_IXUSR 64
  56. /* 00200 in octal is 128 */
  57. // CONSTANT S_IWUSR 128
  58. /* 00400 in octal is 256 */
  59. // CONSTANT S_IRUSR 256
  60. FILE* open(char* name, int flag, int mode)
  61. {
  62. asm("LOAD_EFFECTIVE_ADDRESS_rdi %24"
  63. "LOAD_INTEGER_rdi"
  64. "LOAD_EFFECTIVE_ADDRESS_rsi %16"
  65. "LOAD_INTEGER_rsi"
  66. "LOAD_EFFECTIVE_ADDRESS_rdx %8"
  67. "LOAD_INTEGER_rdx"
  68. "LOAD_IMMEDIATE_rax %2"
  69. "SYSCALL");
  70. }
  71. FILE* fopen(char* filename, char* mode)
  72. {
  73. FILE* f;
  74. if('w' == mode[0])
  75. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  76. f = open(filename, 577 , 384);
  77. }
  78. else
  79. { /* Everything else is a read */
  80. f = open(filename, 0, 0);
  81. }
  82. /* Negative numbers are error codes */
  83. if(0 > f)
  84. {
  85. return 0;
  86. }
  87. return f;
  88. }
  89. int close(int fd)
  90. {
  91. asm("LOAD_EFFECTIVE_ADDRESS_rdi %8"
  92. "LOAD_INTEGER_rdi"
  93. "LOAD_IMMEDIATE_rax %3"
  94. "SYSCALL");
  95. }
  96. int fclose(FILE* stream)
  97. {
  98. int error = close(stream);
  99. return error;
  100. }
  101. int fflush(FILE *stream){
  102. /* We don't buffer, nothing to flush */
  103. return 0;
  104. }
  105. // CONSTANT SEEK_SET 0
  106. // CONSTANT SEEK_CUR 1
  107. // CONSTANT SEEK_END 2
  108. int fseek(FILE* f, long offset, int whence)
  109. {
  110. /* Uses lseek directly */
  111. asm("LOAD_EFFECTIVE_ADDRESS_rdi %24"
  112. "LOAD_INTEGER_rdi"
  113. "LOAD_EFFECTIVE_ADDRESS_rsi %16"
  114. "LOAD_INTEGER_rsi"
  115. "LOAD_EFFECTIVE_ADDRESS_rdx %8"
  116. "LOAD_INTEGER_rdx"
  117. "LOAD_IMMEDIATE_rax %8"
  118. "SYSCALL");
  119. }
  120. void rewind(FILE* f)
  121. {
  122. fseek(f, 0, SEEK_SET);
  123. }