stdio.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <sys/types.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <Kernel/Syscall.h>
  10. #include <AK/printf.cpp>
  11. extern "C" {
  12. int fileno(FILE* stream)
  13. {
  14. assert(stream);
  15. return stream->fd;
  16. }
  17. int feof(FILE* stream)
  18. {
  19. assert(stream);
  20. return stream->eof;
  21. }
  22. int fflush(FILE* stream)
  23. {
  24. // FIXME: Implement buffered streams, duh.
  25. if (!stream)
  26. return -EBADF;
  27. return 0;
  28. }
  29. char* fgets(char* buffer, int size, FILE* stream)
  30. {
  31. assert(stream);
  32. ssize_t nread = 0;
  33. for (;;) {
  34. if (nread >= size)
  35. break;
  36. char ch = fgetc(stream);
  37. if (feof(stream))
  38. break;
  39. buffer[nread++] = ch;
  40. if (!ch || ch == '\n')
  41. break;
  42. }
  43. if (nread < size)
  44. buffer[nread] = '\0';
  45. return buffer;
  46. }
  47. int fgetc(FILE* stream)
  48. {
  49. assert(stream);
  50. char ch;
  51. fread(&ch, sizeof(char), 1, stream);
  52. return ch;
  53. }
  54. int getc(FILE* stream)
  55. {
  56. return fgetc(stream);
  57. }
  58. int getchar()
  59. {
  60. return getc(stdin);
  61. }
  62. int fputc(int ch, FILE* stream)
  63. {
  64. assert(stream);
  65. write(stream->fd, &ch, 1);
  66. return (byte)ch;
  67. }
  68. int putc(int ch, FILE* stream)
  69. {
  70. return fputc(ch, stream);
  71. }
  72. int putchar(int ch)
  73. {
  74. return putc(ch, stdout);
  75. }
  76. void clearerr(FILE* stream)
  77. {
  78. assert(stream);
  79. stream->eof = false;
  80. }
  81. size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
  82. {
  83. assert(stream);
  84. ssize_t nread = read(stream->fd, ptr, nmemb * size);
  85. if (nread < 0)
  86. return 0;
  87. if (nread == 0)
  88. stream->eof = true;
  89. return nread;
  90. }
  91. size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
  92. {
  93. assert(stream);
  94. ssize_t nwritten = write(stream->fd, ptr, nmemb * size);
  95. if (nwritten < 0)
  96. return 0;
  97. return nwritten;
  98. }
  99. int fseek(FILE* stream, long offset, int whence)
  100. {
  101. assert(stream);
  102. off_t off = lseek(stream->fd, offset, whence);
  103. if (off < 0)
  104. return off;
  105. return 0;
  106. }
  107. long ftell(FILE* stream)
  108. {
  109. assert(stream);
  110. return lseek(stream->fd, 0, SEEK_CUR);
  111. }
  112. void rewind(FILE* stream)
  113. {
  114. fseek(stream, 0, SEEK_SET);
  115. }
  116. static void sys_putch(char*&, char ch)
  117. {
  118. putchar(ch);
  119. }
  120. static FILE* __current_stream = nullptr;
  121. static void stream_putch(char*&, char ch)
  122. {
  123. write(__current_stream->fd, &ch, 1);
  124. }
  125. int fprintf(FILE* fp, const char* fmt, ...)
  126. {
  127. __current_stream = fp;
  128. va_list ap;
  129. va_start(ap, fmt);
  130. int ret = printfInternal(stream_putch, nullptr, fmt, ap);
  131. va_end(ap);
  132. return ret;
  133. }
  134. int printf(const char* fmt, ...)
  135. {
  136. va_list ap;
  137. va_start(ap, fmt);
  138. int ret = printfInternal(sys_putch, nullptr, fmt, ap);
  139. va_end(ap);
  140. return ret;
  141. }
  142. static void buffer_putch(char*& bufptr, char ch)
  143. {
  144. *bufptr++ = ch;
  145. }
  146. int sprintf(char* buffer, const char* fmt, ...)
  147. {
  148. va_list ap;
  149. va_start(ap, fmt);
  150. int ret = printfInternal(buffer_putch, buffer, fmt, ap);
  151. buffer[ret] = '\0';
  152. va_end(ap);
  153. return ret;
  154. }
  155. void perror(const char* s)
  156. {
  157. fprintf(stderr, "%s: %s\n", s, strerror(errno));
  158. }
  159. FILE* fopen(const char* pathname, const char* mode)
  160. {
  161. assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
  162. int fd = open(pathname, O_RDONLY);
  163. if (fd < 0)
  164. return nullptr;
  165. auto* fp = (FILE*)malloc(sizeof(FILE));
  166. fp->fd = fd;
  167. fp->eof = false;
  168. return fp;
  169. }
  170. int fclose(FILE* stream)
  171. {
  172. int rc = close(stream->fd);
  173. free(stream);
  174. return rc;
  175. }
  176. }