file.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * O_RDONLY => 0
  47. * O_WRONLY => 1
  48. * O_RDWR => 2
  49. * O_CREAT => 64
  50. * O_TRUNC => 512
  51. * S_IRWXU => 00700
  52. * S_IXUSR => 00100
  53. * S_IWUSR => 00200
  54. * S_IRUSR => 00400
  55. */
  56. FILE* open(char* name, int flag, int mode)
  57. {
  58. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  59. "LOAD_INTEGER_ebx"
  60. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  61. "LOAD_INTEGER_ecx"
  62. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  63. "LOAD_INTEGER_edx"
  64. "LOAD_IMMEDIATE_eax %5"
  65. "INT_80");
  66. }
  67. FILE* fopen(char* filename, char* mode)
  68. {
  69. FILE* f;
  70. if('w' == mode[0])
  71. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  72. f = open(filename, 577 , 384);
  73. }
  74. else
  75. { /* Everything else is a read */
  76. f = open(filename, 0, 0);
  77. }
  78. /* Negative numbers are error codes */
  79. if(0 > f)
  80. {
  81. return 0;
  82. }
  83. return f;
  84. }
  85. int close(int fd)
  86. {
  87. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  88. "LOAD_IMMEDIATE_eax %6"
  89. "INT_80");
  90. }
  91. int fclose(FILE* stream)
  92. {
  93. int error = close(stream);
  94. return error;
  95. }