Cleaned and slightly refactored stdio.h and file I/O syscalls

This commit is contained in:
2024-08-16 11:48:54 +02:00
parent 823cd1761f
commit a40490d6a8
7 changed files with 49 additions and 42 deletions
+32 -3
View File
@@ -1,12 +1,14 @@
#ifndef SYSCALLNUMS_H
#define SYSCALLNUMS_H
#ifndef SYSCALL_H
#define SYSCALL_H
#include <stddef.h>
#define MAX_SYSCALLS 9
typedef enum {
SYS_RESERVED,
SYS_SLEEP,
SYS_GETS,
SYS_READ,
SYS_WRITE,
SYS_OPEN,
SYS_CLOSE,
@@ -25,4 +27,31 @@ typedef enum {
O_BIN = 0x40, // Read bytes, not text
} OFLAGS;
int syscall(int nr, int arg0, int arg1, int arg2) {
int ret = 0;
__asm__ volatile ("int $0x80" : "=a"(ret) : "a"(nr), "b"(arg0), "c"(arg1), "d"(arg2));
return ret;
}
/* Syscall wrappers */
int read(int fd, void* ptr, size_t count) {
// Returns: int fd
return syscall(SYS_READ, fd, (int)ptr, count);
}
int write(int fd, const void* ptr, size_t count) {
// Returns: int count
return syscall(SYS_WRITE, fd, (int)ptr, count);
}
int open(const char* path, int oflag) {
// Returns: int fd
return syscall(SYS_OPEN, (int)path, oflag, 0);
}
int close(int fd) {
// Returns: int status
return syscall(SYS_CLOSE, fd, 0, 0);
}
#endif