1
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include <unistd.h>
7 #include <errno.h>
8
9 #include <syscall.h>
10
11 static FILE __stdin = { 1, STDIN_FILENO, 0 };
12 static FILE __stdout = { 1, STDOUT_FILENO, 0 };
13 static FILE __stderr = { 1, STDERR_FILENO, 0 };
14
15 FILE *stdin = &__stdin;
16 FILE *stdout = &__stdout;
17 FILE *stderr = &__stderr;
18
19 static FILE fds[FOPEN_MAX] = {
20 { 0, 0, 0 },
21 { 0, 0, 0 },
22 { 0, 0, 0 },
23 { 0, 0, 0 },
24 { 0, 0, 0 },
25 { 0, 0, 0 },
26 { 0, 0, 0 },
27 { 0, 0, 0 },
28 { 0, 0, 0 },
29 { 0, 0, 0 },
30 { 0, 0, 0 },
31 { 0, 0, 0 },
32 { 0, 0, 0 },
33 { 0, 0, 0 },
34 { 0, 0, 0 },
35 { 0, 0, 0 },
36 };
37
38 FILE *
_alloc_file()39 _alloc_file()
40 {
41 int i;
42
43 for (i = 0; i < FOPEN_MAX; i++) {
44 if (fds[i].in_use == 0) {
45 fds[i].in_use = 1;
46 return &fds[i];
47 }
48 }
49
50 // XXX: malloc
51
52 return NULL;
53 }
54
55 void
_free_file(FILE * fh)56 _free_file(FILE *fh)
57 {
58 fh->fd = 0;
59 fh->offset = 0;
60 fh->in_use = 0;
61
62 // XXX: free
63 }
64
65 FILE *
fopen(const char * path,const char * mode)66 fopen(const char *path, const char *mode)
67 {
68 uint64_t fd;
69 FILE *fh;
70 // XXX: handle mode
71
72 fd = OSOpen(path, 0);
73 if (fd == 0)
74 return NULL;
75
76 fh = _alloc_file();
77 fh->fd = fd;
78 fh->offset = 0;
79
80 // XXX: handle append
81
82 return fh;
83 }
84
85 int
fclose(FILE * fh)86 fclose(FILE *fh)
87 {
88 int status = OSClose(fh->fd);
89
90 _free_file(fh);
91
92 return status;
93 }
94
95 int
feof(FILE * fh)96 feof(FILE *fh)
97 {
98 errno = ENOSYS;
99 return -1;
100 }
101
102 int
fflush(FILE * fh)103 fflush(FILE *fh)
104 {
105 errno = ENOSYS;
106 return -1;
107 }
108
109 size_t
fread(void * buf,size_t size,size_t nmemb,FILE * fh)110 fread(void *buf, size_t size, size_t nmemb, FILE *fh)
111 {
112 return OSRead(fh->fd, buf, 0, size * nmemb);
113 // set errno
114 }
115
116 size_t
fwrite(const void * buf,size_t size,size_t nmemb,FILE * fh)117 fwrite(const void *buf, size_t size, size_t nmemb, FILE *fh)
118 {
119 return OSWrite(fh->fd, buf, 0, size * nmemb);
120 // set errno
121 }
122
123 int
fputc(int ch,FILE * fh)124 fputc(int ch, FILE *fh)
125 {
126 if (fwrite(&ch, 1, 1, fh) == 1)
127 return ch;
128 return EOF;
129 }
130
131 int
fputs(const char * str,FILE * fh)132 fputs(const char *str, FILE *fh)
133 {
134 int status = fwrite(str, strlen(str), 1, fh);
135 if (status > 0)
136 return status;
137 // XXX: error handling
138 return EOF;
139 }
140
141 int
puts(const char * str)142 puts(const char *str)
143 {
144 int status, status2;
145 status = fputs(str, stdout);
146 if (status < 0)
147 return EOF;
148
149 status2 = fputc('\n', stdout);
150 if (status2 < 0)
151 return EOF;
152
153 return status;
154 }
155
156 int
fgetc(FILE * fh)157 fgetc(FILE *fh)
158 {
159 char ch;
160 if (fread(&ch, 1, 1, fh) == 1)
161 return ch;
162 return EOF;
163 }
164
165 char *
fgets(char * str,int size,FILE * fh)166 fgets(char *str, int size, FILE *fh)
167 {
168 int i;
169
170 for (i = 0; i < (size - 1); i++) {
171 int ch = fgetc(fh);
172 if (ch == EOF)
173 return NULL;
174 if (ch == '\b') {
175 if (i > 0)
176 i -= 1;
177 i -= 1;
178 continue;
179 }
180 str[i] = (char)ch;
181 if (ch == '\n') {
182 str[i + 1] = '\0';
183 return str;
184 }
185 }
186
187 str[size - 1] = '\0';
188 return str;
189 }
190
191