file.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. int fgetc(FILE* f)
  18. {
  19. asm("LOAD_IMMEDIATE_eax %3"
  20. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  21. "LOAD_INTEGER_ebx"
  22. "PUSH_ebx"
  23. "COPY_esp_to_ecx"
  24. "LOAD_IMMEDIATE_edx %1"
  25. "INT_80"
  26. "TEST"
  27. "POP_eax"
  28. "JUMP_NE8 !FUNCTION_fgetc_Done"
  29. "LOAD_IMMEDIATE_eax %-1"
  30. ":FUNCTION_fgetc_Done");
  31. }
  32. void fputc(char s, FILE* f)
  33. {
  34. asm("LOAD_IMMEDIATE_eax %4"
  35. "LOAD_EFFECTIVE_ADDRESS_ebx %4"
  36. "LOAD_INTEGER_ebx"
  37. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  38. "LOAD_IMMEDIATE_edx %1"
  39. "INT_80");
  40. }
  41. /* Important values needed for open
  42. * O_RDONLY => 0
  43. * O_WRONLY => 1
  44. * O_RDWR => 2
  45. * O_CREAT => 64
  46. * O_TRUNC => 512
  47. * S_IRWXU => 00700
  48. * S_IXUSR => 00100
  49. * S_IWUSR => 00200
  50. * S_IRUSR => 00400
  51. */
  52. FILE* open(char* name, int flag, int mode)
  53. {
  54. asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
  55. "LOAD_INTEGER_ebx"
  56. "LOAD_EFFECTIVE_ADDRESS_ecx %8"
  57. "LOAD_INTEGER_ecx"
  58. "LOAD_EFFECTIVE_ADDRESS_edx %4"
  59. "LOAD_INTEGER_edx"
  60. "LOAD_IMMEDIATE_eax %5"
  61. "INT_80");
  62. }
  63. FILE* fopen(char* filename, char* mode)
  64. {
  65. FILE* f;
  66. if('w' == mode[0])
  67. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  68. f = open(filename, 577 , 384);
  69. }
  70. else
  71. { /* Everything else is a read */
  72. f = open(filename, 0, 0);
  73. }
  74. /* Negative numbers are error codes */
  75. if(0 > f)
  76. {
  77. return 0;
  78. }
  79. return f;
  80. }
  81. int close(int fd)
  82. {
  83. asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
  84. "LOAD_IMMEDIATE_eax %6"
  85. "INT_80");
  86. }
  87. int fclose(FILE* stream)
  88. {
  89. int error = close(stream);
  90. return error;
  91. }