file.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2020 deesix <deesix@tuta.io>
  3. * This file is part of M2-Planet.
  4. *
  5. * M2-Planet is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * M2-Planet is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // CONSTANT stdin 0
  19. // CONSTANT stdout 1
  20. // CONSTANT stderr 2
  21. // CONSTANT EOF 0xFFFFFFFF
  22. int fgetc(FILE* f)
  23. {
  24. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  25. "PUSH_X0"
  26. "SET_X1_FROM_SP"
  27. "SET_X2_TO_1"
  28. "SET_X8_TO_SYS_READ"
  29. "SYSCALL"
  30. "SET_X1_TO_0"
  31. "CMP_X1_X0"
  32. "POP_X0"
  33. "SKIP_INST_NE"
  34. "SET_X0_TO_MINUS_1");
  35. }
  36. void fputc(char s, FILE* f)
  37. {
  38. asm("SET_X0_FROM_BP" "SUB_X0_8"
  39. "SET_X1_FROM_X0"
  40. "SET_X0_FROM_BP" "SUB_X0_16" "DEREF_X0"
  41. "SET_X2_TO_1"
  42. "SET_X8_TO_SYS_WRITE"
  43. "SYSCALL");
  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("SET_X0_FROM_BP" "SUB_X0_24" "DEREF_X0"
  59. "SET_X3_FROM_X0"
  60. "SET_X0_FROM_BP" "SUB_X0_16" "DEREF_X0"
  61. "SET_X2_FROM_X0"
  62. "SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  63. "SET_X1_FROM_X0"
  64. "SET_X0_TO_FCNTL_H_AT_FDCWD"
  65. "SET_X8_TO_SYS_OPENAT"
  66. "SYSCALL");
  67. }
  68. FILE* fopen(char* filename, char* mode)
  69. {
  70. FILE* f;
  71. if('w' == mode[0])
  72. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  73. f = open(filename, 577 , 384);
  74. }
  75. else
  76. { /* Everything else is a read */
  77. f = open(filename, 0, 0);
  78. }
  79. /* Negative numbers are error codes */
  80. if(0 > f)
  81. {
  82. return 0;
  83. }
  84. return f;
  85. }
  86. int close(int fd)
  87. {
  88. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  89. "SET_X8_TO_SYS_CLOSE"
  90. "SYSCALL");
  91. }
  92. int fclose(FILE* stream)
  93. {
  94. int error = close(stream);
  95. return error;
  96. }
  97. int fflush(FILE *stream){
  98. /* We don't buffer, nothing to flush */
  99. return 0;
  100. }
  101. // CONSTANT SEEK_SET 0
  102. // CONSTANT SEEK_CUR 1
  103. // CONSTANT SEEK_END 2
  104. int fseek(FILE* f, long offset, int whence)
  105. {
  106. asm("SET_X0_TO_MINUS_1"
  107. "SET_X3_FROM_X0"
  108. "SET_X0_FROM_BP" "SUB_X0_24" "DEREF_X0"
  109. "SET_X2_FROM_X0"
  110. "SET_X0_FROM_BP" "SUB_X0_16" "DEREF_X0"
  111. "SET_X1_FROM_X0"
  112. "SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  113. "SET_X8_TO_SYS_LSEEK"
  114. "SYSCALL");
  115. }
  116. void rewind(FILE* f)
  117. {
  118. fseek(f, 0, SEEK_SET);
  119. }