make run: Use -vga std
[snowy-minesweeper.git] / stdio.c
blob9f1ba485d1a2f3f8c758688ce6f9906ace26dc03
1 #include <console.h>
2 #include <multiboot.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
9 struct FILE {
10 struct multiboot_module *mod;
11 long loc;
15 extern struct multiboot_module *mboot_modules;
16 extern int mboot_module_count;
19 FILE *fopen(const char *path, const char *mode)
21 for (int i = 0; i < mboot_module_count; i++) {
22 if (!strcmp(path, mboot_modules[i].cmdline)) {
23 FILE *fp = malloc(sizeof(*fp));
24 fp->mod = &mboot_modules[i];
25 fp->loc = 0;
26 return fp;
30 return NULL;
34 int fclose(FILE *stream)
36 free(stream);
37 return 0;
41 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
43 size_t byte_size = size * nmemb;
44 size_t mod_len = stream->mod->mod_end - stream->mod->mod_start;
46 if (stream->loc > mod_len) {
47 return 0;
50 if (byte_size > mod_len - stream->loc) {
51 byte_size = mod_len - stream->loc;
53 nmemb = byte_size / size;
54 byte_size = nmemb * size;
56 memcpy(ptr, (uint8_t *)stream->mod->mod_start + stream->loc, byte_size);
57 stream->loc += byte_size;
59 return nmemb;
63 long ftell(FILE *fp)
65 return fp->loc;
69 int fseek(FILE *stream, long offset, int whence)
71 switch (whence) {
72 case SEEK_SET:
73 stream->loc = offset;
74 break;
76 case SEEK_CUR:
77 stream->loc += offset;
78 break;
80 case SEEK_END:
81 stream->loc = stream->mod->mod_end - stream->mod->mod_start;
82 stream->loc += offset;
83 break;
86 if (stream->loc < 0) {
87 stream->loc = 0;
90 return 0;