syscalls: done with open, write, close syscalls
This commit is contained in:
+57
-5
@@ -1,13 +1,50 @@
|
||||
#include "../include/syscalls.h"
|
||||
#include <stddef.h>
|
||||
#include "../include/stdio.h"
|
||||
#include "../include/liballoc.h"
|
||||
|
||||
#define STDIN_FILENO 0
|
||||
#define STDOUT_FILENO 1
|
||||
#define STDERR_FILENO 2
|
||||
|
||||
#define EBADF 9
|
||||
typedef unsigned short umode_t
|
||||
|
||||
const char* flags_to_mode_str(int flags) {
|
||||
static char mode[4] = {0};
|
||||
int accmode = flags & 3;
|
||||
|
||||
if (accmode == 0) { // O_RDONLY
|
||||
mode[0] = 'r';
|
||||
mode[1] = '\0';
|
||||
} else if (accmode == 1) { // O_WRONLY
|
||||
if (flags & 0x400) { // O_APPEND
|
||||
mode[0] = 'a';
|
||||
mode[1] = '\0';
|
||||
} else {
|
||||
mode[0] = 'w';
|
||||
mode[1] = '\0';
|
||||
}
|
||||
} else if (accmode == 2) { // O_RDWR
|
||||
if (flags & 0x400) { // O_APPEND
|
||||
mode[0] = 'a';
|
||||
mode[1] = '+';
|
||||
mode[2] = '\0';
|
||||
} else if (flags & 0x200) { // O_TRUNC
|
||||
mode[0] = 'w';
|
||||
mode[1] = '+';
|
||||
mode[2] = '\0';
|
||||
} else {
|
||||
mode[0] = 'r';
|
||||
mode[1] = '+';
|
||||
mode[2] = '\0';
|
||||
}
|
||||
} else {
|
||||
mode[0] = 'r';
|
||||
mode[1] = '\0';
|
||||
}
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
int sys_write(TrapFrame *tf)
|
||||
{
|
||||
@@ -32,18 +69,30 @@ int sys_write(TrapFrame *tf)
|
||||
|
||||
int sys_open(TrapFrame *tf)
|
||||
{
|
||||
const char* filename = tf->ebx;
|
||||
const char* filename = (const char*)tf->ebx;
|
||||
int flags = tf->ecx;
|
||||
umode_t mode = tf->edx;
|
||||
|
||||
int fd = -1;
|
||||
const char* mode_str = flags_to_mode_str(flags);
|
||||
|
||||
debug_log("\n====filename: %s\n", filename);
|
||||
debug_log("\n====mode_str: %s\n", mode_str);
|
||||
|
||||
for(int i = 3; i < MAX_OPEN_FILES; i++)
|
||||
{
|
||||
debug_log("\n====i: %X\n", i);
|
||||
if(current->file_descriptors[i] == NULL)
|
||||
{
|
||||
fat_fopen(current->file_descriptors[i], filename, (char)mode);
|
||||
current->file_descriptors[i] = malloc(sizeof(file_t)); // Kernel malloc
|
||||
debug_log("\n====FILE_DESCRIPTOR: %X\n", i);
|
||||
int result = fat_fopen(current->file_descriptors[i], filename, mode_str);
|
||||
if (result < 0) {
|
||||
free(current->file_descriptors[i]); // Free on failure
|
||||
current->file_descriptors[i] = NULL;
|
||||
return result;
|
||||
}
|
||||
fd = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +104,10 @@ int sys_close(TrapFrame *tf)
|
||||
int fd = tf->ebx;
|
||||
if(current->file_descriptors[fd] != NULL)
|
||||
{
|
||||
return fat_fclose(current->file_descriptors[fd]);
|
||||
int result = fat_fclose(current->file_descriptors[fd]);
|
||||
free(current->file_descriptors[fd]);
|
||||
current->file_descriptors[fd] = NULL;
|
||||
return result;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user