LibC: Write mostly functioning stdio

This commit is contained in:
Bananymous
2023-04-23 14:32:37 +03:00
parent 9b2a577fc3
commit cd74b2167d
13 changed files with 619 additions and 60 deletions

View File

@@ -43,6 +43,7 @@ namespace Kernel
BAN::ErrorOr<size_t> read(int, void*, size_t);
BAN::ErrorOr<size_t> write(int, const void*, size_t);
BAN::ErrorOr<void> creat(BAN::StringView, mode_t);
BAN::ErrorOr<void> seek(int, size_t);
BAN::ErrorOr<void> fstat(int, stat*);
BAN::ErrorOr<void> stat(BAN::StringView, stat*);
@@ -54,6 +55,8 @@ namespace Kernel
BAN::ErrorOr<BAN::String> working_directory() const;
BAN::ErrorOr<void> set_working_directory(BAN::StringView);
void termid(char*) const;
static Process& current() { return Thread::current().process(); }
MMU& mmu() { return m_mmu ? *m_mmu : MMU::get(); }

View File

@@ -3,6 +3,10 @@
#define SYS_EXIT 1
#define SYS_READ 2
#define SYS_WRITE 3
#define SYS_TERMID 4
#define SYS_CLOSE 5
#define SYS_SEEK 6
#define SYS_OPEN 7
#include <stdint.h>

View File

@@ -304,6 +304,15 @@ namespace Kernel
return ret;
}
BAN::ErrorOr<void> Process::seek(int fd, size_t offset)
{
LockGuard _(m_lock);
TRY(validate_fd(fd));
auto& open_fd = open_file_description(fd);
open_fd.offset = offset;
return {};
}
BAN::ErrorOr<BAN::Vector<BAN::String>> Process::read_directory_entries(int fd)
{
OpenFileDescription open_fd_copy;
@@ -349,6 +358,14 @@ namespace Kernel
return {};
}
void Process::termid(char* buffer) const
{
if (m_tty == nullptr)
buffer[0] = '\0';
strcpy(buffer, "/dev/");
strcpy(buffer + 5, m_tty->name().data());
}
BAN::ErrorOr<BAN::String> Process::absolute_path_of(BAN::StringView path) const
{
if (path.empty())

View File

@@ -14,7 +14,7 @@ namespace Kernel
{
auto res = Process::current().read(fd, buffer, size);
if (res.is_error())
return res.error().get_error_code();
return -res.error().get_error_code();
return res.value();
}
@@ -22,7 +22,36 @@ namespace Kernel
{
auto res = Process::current().write(fd, buffer, size);
if (res.is_error())
return res.error().get_error_code();
return -res.error().get_error_code();
return res.value();
}
int sys_close(int fd)
{
auto res = Process::current().close(fd);
if (res.is_error())
return -res.error().get_error_code();
return 0;
}
int sys_seek(int fd, long offset)
{
auto res = Process::current().seek(fd, offset);
if (res.is_error())
return -res.error().get_error_code();
return 0;
}
void sys_termid(char* buffer)
{
Process::current().termid(buffer);
}
int sys_open(const char* path, int oflags)
{
auto res = Process::current().open(path, oflags);
if (res.is_error())
return -res.error().get_error_code();
return res.value();
}
@@ -44,10 +73,20 @@ namespace Kernel
case SYS_WRITE:
ret = sys_write((int)(uintptr_t)arg1, arg2, (size_t)(uintptr_t)arg3);
break;
default:
ret = -1;
dprintln("Unknown syscall");
case SYS_TERMID:
sys_termid((char*)arg1);
break;
case SYS_CLOSE:
ret = sys_close((int)(uintptr_t)arg1);
break;
case SYS_SEEK:
ret = sys_seek((int)(uintptr_t)arg1, (long)arg2);
break;
case SYS_OPEN:
ret = sys_open((const char*)arg1, (int)(uintptr_t)arg2);
break;
default:
Kernel::panic("Unknown syscall {}", syscall);
}
asm volatile("cli");