k-tana-zero/k/fd.c

40 lines
721 B
C
Raw Normal View History

#include "fd.h"
static struct file_entry fd_table[MAX_FD] = { 0 };
static int find_next_fd(void)
{
for (int fd = 0; fd < MAX_FD; ++fd)
{
if (!fd_table[fd].open)
return fd;
}
return -1;
}
2022-04-23 19:37:54 +00:00
int register_fd(u32 lba, u32 file_size)
{
int free_fd = find_next_fd();
if (free_fd < 0)
return -1;
2022-04-23 19:37:54 +00:00
struct file_entry f = { .open = 0, .lba = lba, .file_offset = 0,
.file_size = file_size };
fd_table[free_fd] = f;
return free_fd;
}
int destroy_fd(int fd)
{
if (fd > MAX_FD || !fd_table[fd].open)
return -1;
fd_table[fd].open = 0;
fd_table[fd].lba = 0;
fd_table[fd].file_offset = 0;
2022-04-23 19:37:54 +00:00
fd_table[fd].file_size = 0;
return 0;
}