forked from PoliEcho-holding-org/kern
Cleaned and slightly refactored stdio.h and file I/O syscalls
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#ifndef STDDEF_H
|
||||
#define STDDEF_H
|
||||
|
||||
#define NULL ((void*)0)
|
||||
|
||||
typedef unsigned int size_t;
|
||||
typedef signed int ssize_t;
|
||||
|
||||
#endif
|
||||
+2
-3
@@ -1,7 +1,7 @@
|
||||
#ifndef STDINT_H
|
||||
#define STDINT_H
|
||||
|
||||
#define NULL ((void*)0)
|
||||
#include <stddef.h>
|
||||
|
||||
// Signed ints
|
||||
typedef signed char int8_t;
|
||||
@@ -15,7 +15,6 @@ typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
// Other
|
||||
typedef unsigned int size_t;
|
||||
// TODO: floats
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,31 +17,6 @@ FILE* stdin;
|
||||
FILE* stdout;
|
||||
FILE* stderr;
|
||||
|
||||
/* --- Syscall Wrappers --- */
|
||||
int write(int fd, const void* ptr, size_t count) {
|
||||
int result = 0;
|
||||
__asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_WRITE), "b"(fd), "c"(ptr), "d"(count));
|
||||
return result;
|
||||
}
|
||||
|
||||
int read(int fd, void* ptr, size_t count) {
|
||||
int result = 0;
|
||||
__asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_GETS), "b"(fd), "c"(ptr), "d"(count));
|
||||
return result;
|
||||
}
|
||||
|
||||
int open(const char* path, int oflag) {
|
||||
int fd = -1;
|
||||
__asm__ volatile ("int $0x80" : "=a"(fd) : "a"(SYS_OPEN), "b"(path), "c"(oflag));
|
||||
return fd;
|
||||
}
|
||||
|
||||
int close(int fd) {
|
||||
int result = -1;
|
||||
__asm__ volatile ("int $0x80" : "=a"(result) : "a"(SYS_CLOSE), "b"(fd));
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --- file I/O --- */
|
||||
FILE* fopen(const char* filename, const char* mode) {
|
||||
FILE* stream = malloc(sizeof(FILE));
|
||||
@@ -90,7 +65,6 @@ size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
|
||||
}
|
||||
|
||||
return writtenEl;
|
||||
|
||||
}
|
||||
|
||||
int fseek(FILE* stream, long int origin, int offset) {
|
||||
|
||||
+2
-6
@@ -5,15 +5,11 @@
|
||||
#include <syscall.h>
|
||||
|
||||
void* malloc(const uint32_t size) {
|
||||
void* ptr = 0;
|
||||
__asm__ volatile ("int $0x80" : : "a"(SYS_MALLOC), "b"(size));
|
||||
__asm__ volatile ("movl %%eax, %0" : "=r"(ptr));
|
||||
|
||||
return ptr;
|
||||
return (void*)syscall(SYS_MALLOC, size, 0, 0);
|
||||
}
|
||||
|
||||
void free(const void* ptr) {
|
||||
__asm__ volatile ("int $0x80" : : "a"(SYS_FREE), "b"(ptr));
|
||||
syscall(SYS_FREE, (int)ptr, 0, 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+32
-3
@@ -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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef SYSCALL_H
|
||||
#define SYSCALL_H
|
||||
#ifndef INTERRUPT_SYSCALLS_H
|
||||
#define INTERRUPT_SYSCALLS_H
|
||||
|
||||
#include <interrupt/idt.h>
|
||||
#include <terminal/terminal.h>
|
||||
@@ -47,7 +47,7 @@ void sys_write() {
|
||||
|
||||
// TODO: Use a Read() sycall and read from stdin
|
||||
// Args: ebx - char* buffer
|
||||
void sys_gets() {
|
||||
void sys_read() {
|
||||
char* s;
|
||||
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
|
||||
reads(s);
|
||||
@@ -102,7 +102,7 @@ void sys_seek() {}
|
||||
void (*syscalls[MAX_SYSCALLS])(void) = {
|
||||
[SYS_SLEEP] = sys_sleep,
|
||||
[SYS_WRITE] = sys_write,
|
||||
[SYS_GETS] = sys_gets,
|
||||
[SYS_READ] = sys_read,
|
||||
[SYS_MALLOC] = sys_malloc,
|
||||
[SYS_FREE] = sys_free,
|
||||
[SYS_OPEN] = sys_open,
|
||||
|
||||
Reference in New Issue
Block a user