1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 
fprintf(FILE * restrict stream,const char * restrict format,...)7 int fprintf(FILE *restrict stream, const char *restrict format, ...)
8 {
9     const int bufsize = 65536 * 2;
10     char *buf = malloc(bufsize);
11     memset(buf, 0, bufsize);
12     va_list args;
13 
14     va_start(args, format);
15     vsprintf(buf, format, args);
16     va_end(args);
17 
18     int len = strlen(buf);
19     if (len > bufsize - 1)
20     {
21         len = bufsize - 1;
22         buf[bufsize - 1] = 0;
23     }
24     write(stream->fd, buf, len);
25     free(buf);
26 }
27 
getchar(void)28 int getchar(void)
29 {
30     unsigned int c;
31     read(0, &c, 1);
32     return c;
33 }
34 
puts(const char * s)35 int puts(const char *s)
36 {
37     return put_string(s, COLOR_WHITE, COLOR_BLACK);
38 }
39 
putchar(int c)40 int putchar(int c)
41 {
42     return printf("%c", (char)c);
43 }
44 
fflush(FILE * stream)45 int fflush(FILE *stream)
46 {
47     return 0;
48 }
49 
ferror(FILE * stream)50 int ferror(FILE *stream)
51 {
52     return 0;
53 }
54 
fclose(FILE * stream)55 int fclose(FILE *stream)
56 {
57     int retval = close(stream->fd);
58     if (retval)
59         return retval;
60     if (stream->fd >= 3)
61         free(stream);
62 
63     return 0;
64 }
65 
66 // FIXME:
67 // 请注意,这个函数的实现,没有遵照posix,行为也与Linux的不一致,请在将来用Rust重构时改变它,以使得它的行为与Linux的一致。
fopen(const char * restrict pathname,const char * restrict mode)68 FILE *fopen(const char *restrict pathname, const char *restrict mode)
69 {
70     FILE *stream = malloc(sizeof(FILE));
71     memset(stream, 0, sizeof(FILE));
72     int o_flags = 0;
73 
74     if (strcmp(mode, "r") == 0)
75         o_flags = O_RDONLY;
76     else if (strcmp(mode, "r+") == 0)
77         o_flags = O_RDWR;
78     else if (strcmp(mode, "w") == 0)
79         o_flags = O_WRONLY;
80     else if (strcmp(mode, "w+") == 0)
81         o_flags = O_RDWR | O_CREAT;
82     else if (strcmp(mode, "a") == 0)
83         o_flags = O_APPEND | O_CREAT;
84     else if (strcmp(mode, "a+") == 0)
85         o_flags = O_APPEND | O_CREAT;
86 
87     int fd = open(pathname, o_flags);
88     if (fd >= 0)
89         stream->fd = fd;
90     return stream;
91 }
92