stdio.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef __STDIO_H
  2. #define __STDIO_H
  3. #include "asmc.h"
  4. #include "stdlib.h"
  5. #include "stdarg.h"
  6. #include "assert.h"
  7. #include "fcntl.h"
  8. #include "unistd.h"
  9. #define EOF (-1)
  10. int fputc(int c, FILE *s) {
  11. unsigned char uc = (unsigned char) c;
  12. if (s->fd == 0) {
  13. return EOF;
  14. } else if (s->fd == 1 || s->fd == 2) {
  15. __handles->write(uc);
  16. } else {
  17. __handles->vfs_write(uc, s->fd);
  18. }
  19. return uc;
  20. }
  21. #define putc fputc
  22. int putchar(int c) {
  23. return fputc(c, stdout);
  24. }
  25. int fputs(const char *s, FILE *stream) {
  26. while (*s != 0) {
  27. if (fputc(*s, stream) == EOF) {
  28. return EOF;
  29. }
  30. s = s + 1;
  31. }
  32. return 0;
  33. }
  34. int puts(const char *s) {
  35. if (fputs(s, stdout) != EOF) {
  36. if (fputc('\n', stdout) != EOF) {
  37. return 0;
  38. }
  39. }
  40. return EOF;
  41. }
  42. int getc(FILE *stream) {
  43. if (stream->ungetted) {
  44. stream->ungetted = 0;
  45. return stream->ungetbuf;
  46. }
  47. unsigned char buf;
  48. ssize_t res = read(stream->fd, &buf, 1);
  49. if (res) {
  50. return buf;
  51. } else {
  52. return EOF;
  53. }
  54. }
  55. int fgetc(FILE *stream) {
  56. return getc(stream);
  57. }
  58. int ungetc(int c, FILE *stream) {
  59. if (stream->ungetted) {
  60. return EOF;
  61. }
  62. stream->ungetted = 1;
  63. stream->ungetbuf = c;
  64. return c;
  65. }
  66. char *itoa(unsigned int x) {
  67. return __handles->itoa(x);
  68. }
  69. int fflush(FILE *stream) {
  70. // Our FILE has no buffer, so there is nothing to flush
  71. return 0;
  72. }
  73. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  74. const char *buf = ptr;
  75. size_t written;
  76. for (written = 0; written < nmemb; written++) {
  77. size_t size2 = size;
  78. while (size2--) {
  79. if (fputc(*buf++, stream) == EOF) {
  80. return written;
  81. }
  82. }
  83. }
  84. return written;
  85. }
  86. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  87. char *buf = ptr;
  88. size_t read;
  89. for (read = 0; read < nmemb; read++) {
  90. size_t size2 = size;
  91. while (size2--) {
  92. int c = fgetc(stream);
  93. if (c == EOF) {
  94. return read;
  95. } else {
  96. *buf++ = (char) c;
  97. }
  98. }
  99. }
  100. return read;
  101. }
  102. int _open_mode(const char *mode) {
  103. int oflag = 0;
  104. if (*mode == 'r') oflag = O_RDONLY;
  105. if (*mode == 'w') oflag = O_WRONLY | O_CREAT | O_TRUNC;
  106. if (!oflag) return 0;
  107. mode++;
  108. if (*mode == '\0') return oflag;
  109. if (*mode != 'b') return 0;
  110. mode++;
  111. if (*mode == '\0') return oflag;
  112. return 0;
  113. }
  114. FILE *fdopen(int fildes, const char *mode) {
  115. if (_open_mode(mode)) {
  116. FILE *ret = malloc(sizeof(FILE));
  117. ret->fd = fildes;
  118. ret->ungetbuf = 0;
  119. ret->ungetted = 0;
  120. return ret;
  121. } else {
  122. _force_assert(!"unknown file mode");
  123. }
  124. }
  125. FILE *fopen(const char *filename, const char *mode) {
  126. int oflag = _open_mode(mode);
  127. if (oflag) {
  128. int fildes = open(filename, oflag);
  129. FILE *ret = fdopen(fildes, mode);
  130. return ret;
  131. } else {
  132. _force_assert(!"unknown file mode");
  133. }
  134. }
  135. int fclose(FILE *stream) {
  136. if (stream == stdout || stream == stderr) {
  137. // Nothing to do here...
  138. } else {
  139. __handles->vfs_close(stream->fd);
  140. free(stream);
  141. }
  142. }
  143. #include "_printf.h"
  144. #include "_scanf.h"
  145. #endif