mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
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
|
#ifndef STDINT_H
|
||||||
#define STDINT_H
|
#define STDINT_H
|
||||||
|
|
||||||
#define NULL ((void*)0)
|
#include <stddef.h>
|
||||||
|
|
||||||
// Signed ints
|
// Signed ints
|
||||||
typedef signed char int8_t;
|
typedef signed char int8_t;
|
||||||
@@ -15,7 +15,6 @@ typedef unsigned short uint16_t;
|
|||||||
typedef unsigned int uint32_t;
|
typedef unsigned int uint32_t;
|
||||||
typedef unsigned long long uint64_t;
|
typedef unsigned long long uint64_t;
|
||||||
|
|
||||||
// Other
|
// TODO: floats
|
||||||
typedef unsigned int size_t;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,31 +17,6 @@ FILE* stdin;
|
|||||||
FILE* stdout;
|
FILE* stdout;
|
||||||
FILE* stderr;
|
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 I/O --- */
|
||||||
FILE* fopen(const char* filename, const char* mode) {
|
FILE* fopen(const char* filename, const char* mode) {
|
||||||
FILE* stream = malloc(sizeof(FILE));
|
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;
|
return writtenEl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int fseek(FILE* stream, long int origin, int offset) {
|
int fseek(FILE* stream, long int origin, int offset) {
|
||||||
|
|||||||
+2
-6
@@ -5,15 +5,11 @@
|
|||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
|
|
||||||
void* malloc(const uint32_t size) {
|
void* malloc(const uint32_t size) {
|
||||||
void* ptr = 0;
|
return (void*)syscall(SYS_MALLOC, size, 0, 0);
|
||||||
__asm__ volatile ("int $0x80" : : "a"(SYS_MALLOC), "b"(size));
|
|
||||||
__asm__ volatile ("movl %%eax, %0" : "=r"(ptr));
|
|
||||||
|
|
||||||
return ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(const void* ptr) {
|
void free(const void* ptr) {
|
||||||
__asm__ volatile ("int $0x80" : : "a"(SYS_FREE), "b"(ptr));
|
syscall(SYS_FREE, (int)ptr, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+32
-3
@@ -1,12 +1,14 @@
|
|||||||
#ifndef SYSCALLNUMS_H
|
#ifndef SYSCALL_H
|
||||||
#define SYSCALLNUMS_H
|
#define SYSCALL_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#define MAX_SYSCALLS 9
|
#define MAX_SYSCALLS 9
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SYS_RESERVED,
|
SYS_RESERVED,
|
||||||
SYS_SLEEP,
|
SYS_SLEEP,
|
||||||
SYS_GETS,
|
SYS_READ,
|
||||||
SYS_WRITE,
|
SYS_WRITE,
|
||||||
SYS_OPEN,
|
SYS_OPEN,
|
||||||
SYS_CLOSE,
|
SYS_CLOSE,
|
||||||
@@ -25,4 +27,31 @@ typedef enum {
|
|||||||
O_BIN = 0x40, // Read bytes, not text
|
O_BIN = 0x40, // Read bytes, not text
|
||||||
} OFLAGS;
|
} 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
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef SYSCALL_H
|
#ifndef INTERRUPT_SYSCALLS_H
|
||||||
#define SYSCALL_H
|
#define INTERRUPT_SYSCALLS_H
|
||||||
|
|
||||||
#include <interrupt/idt.h>
|
#include <interrupt/idt.h>
|
||||||
#include <terminal/terminal.h>
|
#include <terminal/terminal.h>
|
||||||
@@ -47,7 +47,7 @@ void sys_write() {
|
|||||||
|
|
||||||
// TODO: Use a Read() sycall and read from stdin
|
// TODO: Use a Read() sycall and read from stdin
|
||||||
// Args: ebx - char* buffer
|
// Args: ebx - char* buffer
|
||||||
void sys_gets() {
|
void sys_read() {
|
||||||
char* s;
|
char* s;
|
||||||
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
|
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
|
||||||
reads(s);
|
reads(s);
|
||||||
@@ -102,7 +102,7 @@ void sys_seek() {}
|
|||||||
void (*syscalls[MAX_SYSCALLS])(void) = {
|
void (*syscalls[MAX_SYSCALLS])(void) = {
|
||||||
[SYS_SLEEP] = sys_sleep,
|
[SYS_SLEEP] = sys_sleep,
|
||||||
[SYS_WRITE] = sys_write,
|
[SYS_WRITE] = sys_write,
|
||||||
[SYS_GETS] = sys_gets,
|
[SYS_READ] = sys_read,
|
||||||
[SYS_MALLOC] = sys_malloc,
|
[SYS_MALLOC] = sys_malloc,
|
||||||
[SYS_FREE] = sys_free,
|
[SYS_FREE] = sys_free,
|
||||||
[SYS_OPEN] = sys_open,
|
[SYS_OPEN] = sys_open,
|
||||||
|
|||||||
Reference in New Issue
Block a user