file.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_IMMEDIATE_eax %3"
  24. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  25. "LOAD_INTEGER_ebx"
  26. "PUSH_ebx"
  27. "COPY_esp_to_ecx"
  28. "LOAD_IMMEDIATE_edx %1"
  29. "INT_80"
  30. "TEST"
  31. "POP_eax"
  32. "JUMP_NE8 !FUNCTION_fgetc_Done"
  33. "LOAD_IMMEDIATE_eax %-1"
  34. ":FUNCTION_fgetc_Done");
  35. }
  36. void fputc(char s, FILE* f)
  37. {
  38. asm("LOAD_IMMEDIATE_eax %4"
  39. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  40. "LOAD_INTEGER_ebx"
  41. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  42. "LOAD_IMMEDIATE_edx %1"
  43. "INT_80");
  44. }
  45. /* Important values needed for open */
  46. // CONSTANT O_RDONLY 0
  47. // CONSTANT O_WRONLY 1
  48. // CONSTANT O_RDWR 2
  49. // CONSTANT O_CREAT 64
  50. // CONSTANT O_TRUNC 512
  51. /* 00700 in octal is 448*/
  52. // CONSTANT S_IRWXU 448
  53. /* 00100 in octal is 64 */
  54. // CONSTANT S_IXUSR 64
  55. /* 00200 in octal is 128 */
  56. // CONSTANT S_IWUSR 128
  57. /* 00400 in octal is 256 */
  58. // CONSTANT S_IRUSR 256
  59. FILE* open(char* name, int flag, int mode)
  60. {
  61. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  62. "LOAD_INTEGER_ebx"
  63. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  64. "LOAD_INTEGER_ecx"
  65. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  66. "LOAD_INTEGER_edx"
  67. "LOAD_IMMEDIATE_eax %5"
  68. "INT_80");
  69. }
  70. FILE* fopen(char* filename, char* mode)
  71. {
  72. FILE* f;
  73. if('w' == mode[0])
  74. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  75. f = open(filename, 577 , 384);
  76. }
  77. else
  78. { /* Everything else is a read */
  79. f = open(filename, 0, 0);
  80. }
  81. /* Negative numbers are error codes */
  82. if(0 > f)
  83. {
  84. return 0;
  85. }
  86. return f;
  87. }
  88. int close(int fd)
  89. {
  90. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  91. "LOAD_INTEGER_ebx"
  92. "LOAD_IMMEDIATE_eax %6"
  93. "INT_80");
  94. }
  95. int fclose(FILE* stream)
  96. {
  97. int error = close(stream);
  98. return error;
  99. }
  100. int fflush(FILE *stream){
  101. /* We don't buffer, nothing to flush */
  102. return 0;
  103. }
  104. // CONSTANT SEEK_SET 0
  105. // CONSTANT SEEK_CUR 1
  106. // CONSTANT SEEK_END 2
  107. /* We just use lseek directly */
  108. int fseek(FILE* f, long offset, int whence)
  109. {
  110. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  111. "LOAD_INTEGER_ebx"
  112. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  113. "LOAD_INTEGER_ecx"
  114. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  115. "LOAD_INTEGER_edx"
  116. "LOAD_IMMEDIATE_eax %19"
  117. "INT_80");
  118. }
  119. void rewind(FILE* f)
  120. {
  121. fseek(f, 0, SEEK_SET);
  122. }