file.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 R1 R14 0"
  24. "FGETC");
  25. }
  26. void fputc(char s, FILE* f)
  27. {
  28. asm("LOAD R0 R14 0"
  29. "LOAD R1 R14 4"
  30. "FPUTC");
  31. }
  32. /* Important values needed for open */
  33. // CONSTANT O_RDONLY 0
  34. // CONSTANT O_WRONLY 1
  35. // CONSTANT O_RDWR 2
  36. // CONSTANT O_CREAT 64
  37. // CONSTANT O_TRUNC 512
  38. /* 00700 in octal is 448*/
  39. // CONSTANT S_IRWXU 448
  40. /* 00100 in octal is 64 */
  41. // CONSTANT S_IXUSR 64
  42. /* 00200 in octal is 128 */
  43. // CONSTANT S_IWUSR 128
  44. /* 00400 in octal is 256 */
  45. // CONSTANT S_IRUSR 256
  46. FILE* open(char* filename, int flag, int mode)
  47. {
  48. asm("LOAD R0 R14 0"
  49. "LOAD R1 R14 4"
  50. "LOAD R2 R14 8"
  51. "FOPEN"
  52. "FALSE R2");
  53. }
  54. FILE* fopen(char* filename, char* mode)
  55. {
  56. FILE* f;
  57. if('w' == mode[0])
  58. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  59. f = open(filename, 577, 384);
  60. }
  61. else
  62. { /* Everything else is a read */
  63. f = open(filename, 0, 0);
  64. }
  65. /* Negative numbers are error codes */
  66. if(0 > f)
  67. {
  68. return 0;
  69. }
  70. return f;
  71. }
  72. int fclose(FILE* stream)
  73. {
  74. asm("LOAD R0 R14 0"
  75. "FCLOSE");
  76. }
  77. int fflush(FILE *stream){
  78. /* We don't buffer, nothing to flush */
  79. return 0;
  80. }
  81. // CONSTANT SEEK_SET 0
  82. // CONSTANT SEEK_CUR 1
  83. // CONSTANT SEEK_END 2
  84. int fseek(FILE* f, long offset, int whence)
  85. {
  86. asm("LOAD R0 R14 0"
  87. "LOAD R1 R14 4"
  88. "LOAD R2 R14 8"
  89. "FSEEK"
  90. "FALSE R2");
  91. }
  92. void rewind(FILE* f)
  93. {
  94. fseek(f, 0, SEEK_SET);
  95. }