src/kernel/file.c (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include "cedos/file.h"
#include "cedos/fat.h"
#include "cedos/drivers/console.h"
#include "cedos/drivers/keyboard.h"
#ifdef DEBUG
#define PRINT_DBG(...) printk("[" __FILE__ "] " __VA_ARGS__)
#else
#define PRINT_DBG(...) {}
#endif
//const int root_fd = 0x1000;
FILE file_table[256];
int next_free = 0;
int stdin, stdout, fat_root;
int file_init() {
file_table[next_free].type = FILE_STDIO;
file_table[next_free].stdio_id = 0;
file_table[next_free].fat_cluster = 0;
stdin = next_free++;
file_table[next_free].type = FILE_STDIO;
file_table[next_free].stdio_id = 1;
file_table[next_free].fat_cluster = 0;
stdout = next_free++;
// FAT root
file_table[next_free].type = FILE_FAT;
file_table[next_free].stdio_id = 0;
file_table[next_free].fat_cluster = 0;
fat_root = next_free++;
return 0;
}
int file_open(const char *pathname, int flags) {
while (*pathname == '/') {
pathname++;
}
return file_openat(fat_root, pathname, flags);
}
int file_openat(int fd, const char *fname, int flags) {
int new_fd = next_free++;
PRINT_DBG("Given fd: %i\n", fd);
PRINT_DBG("New fd: %i\n", new_fd);
FILE *root = &file_table[fd];
FILE *handle = &file_table[new_fd];
if (root->type == FILE_FAT) {
PRINT_DBG("fd is FAT\n");
FAT_openat(root, handle, fname, flags);
return new_fd;
} else {
PRINT_DBG("fd is NOT FAT (%i)\n", (int)(root->type));
// open other files
return 0;
}
}
int file_read(int fd, char *buffer, uint32_t size) {
FILE *file = &file_table[fd];
if (file->type == FILE_FAT) {
return FAT_read(file, buffer, size);
} else {
uint32_t i = 0;
while (i < size) {
char table[] = {
'^', 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\\', '`', 0x08,
'\t', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', '?', '+', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '?', '?', '#',
0, '<', 'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0, 0, 0, ' ' };
uint32_t scancode = std_kb->read();
char c = scancode <= 60 ? table[scancode] : 0;
if (c == 0) { continue; }
buffer[i++] = c;
}
return size;
}
}
int file_write(int fd, char *buffer, uint32_t size) {
for (uint32_t i = 0; i < size; i++) {
std_con->write_c(buffer[i]);
}
return size;
}
int file_dir_next(int fd, int index, char *fname_buffer) {
if (fd & 0x1000) {
return FAT_dir_next(fd, index, fname_buffer);
} else {
return -1;
}
}