From 1695418dcb32ee2dc1ae21a05772e9ad8ec8a2cb Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Thu, 26 Dec 2024 03:20:27 +0200 Subject: [PATCH] kernel/syscalls: Implement sys_tell & sys_seek Signed-off-by: EnderIce2 --- Kernel/include/interface/syscalls.h | 53 +++++++++++++++++++++++++++++ Kernel/syscalls/native.cpp | 18 ++++++++++ 2 files changed, 71 insertions(+) diff --git a/Kernel/include/interface/syscalls.h b/Kernel/include/interface/syscalls.h index 74cb8105..34913e77 100644 --- a/Kernel/include/interface/syscalls.h +++ b/Kernel/include/interface/syscalls.h @@ -232,6 +232,13 @@ typedef enum __SYS_SET_FS = 3, } prctl_options_t; +typedef enum +{ + __SYS_SEEK_SET = 0, + __SYS_SEEK_CUR = 1, + __SYS_SEEK_END = 2 +} seek_whence_t; + typedef enum { __SYS_SIGNULL = 0, @@ -685,6 +692,46 @@ typedef enum * - #EINVAL if `length` is invalid */ SYS_FTRUNCATE, + /** + * @brief Get the current file offset + * + * @code + * off_t tell(int fd); + * @endcode + * + * @details Returns the current file offset for the file referred to by `fd`. + * + * @param fd File descriptor + * + * @return + * - Current file offset on success + * - #EBADF if `fd` is not a valid file descriptor + */ + SYS_TELL, + /** + * @brief Set the file offset + * + * @code + * off_t seek(int fd, off_t offset, int whence); + * @endcode + * + * @details Sets the file offset for the file referred to by `fd` to the + * specified `offset` according to the directive `whence`. + * + * @param fd File descriptor + * @param offset Offset to set + * @param whence Directive for setting the offset\n + * Supported values: + * - #__SYS_SEEK_SET: Set the offset to `offset` bytes + * - #__SYS_SEEK_CUR: Set the offset to the current offset plus `offset` + * - #__SYS_SEEK_END: Set the offset to the size of the file plus `offset` + * + * @return + * - New file offset on success + * - #EBADF if `fd` is not a valid file descriptor + * - #EINVAL if `whence` is invalid + */ + SYS_SEEK, /* Process Control */ @@ -1376,6 +1423,12 @@ typedef enum /** @copydoc SYS_FTRUNCATE */ #define call_ftruncate(fd, length) syscall2(SYS_FTRUNCATE, (scarg)fd, (scarg)length) +/** @copydoc SYS_TELL */ +#define call_tell(fd) syscall1(SYS_TELL, (scarg)fd) + +/** @copydoc SYS_SEEK */ +#define call_seek(fd, offset, whence) syscall3(SYS_SEEK, (scarg)fd, (scarg)offset, (scarg)whence) + /* Process Control */ /** @copydoc SYS_EXIT */ diff --git a/Kernel/syscalls/native.cpp b/Kernel/syscalls/native.cpp index 088ea40e..9a8649c8 100644 --- a/Kernel/syscalls/native.cpp +++ b/Kernel/syscalls/native.cpp @@ -159,6 +159,22 @@ static int sys_access(SysFrm *Frame, const char *pathname, int mode) static int sys_truncate(SysFrm *Frame, const char *pathname, off_t length) { return -ENOSYS; } static int sys_ftruncate(SysFrm *Frame, int fd, off_t length) { return -ENOSYS; } +static int sys_tell(SysFrm *Frame, int fd) +{ + PCB *pcb = thisProcess; + vfs::FileDescriptorTable *fdt = pcb->FileDescriptors; + + return fdt->usr_lseek(fd, 0, SEEK_CUR); +} + +static off_t sys_seek(SysFrm *Frame, int fd, off_t offset, int whence) +{ + PCB *pcb = thisProcess; + vfs::FileDescriptorTable *fdt = pcb->FileDescriptors; + + return fdt->usr_lseek(fd, offset, whence); +} + static __noreturn void sys_exit(SysFrm *Frame, int status) { TCB *t = thisThread; @@ -333,6 +349,8 @@ __constructor void __init_native_syscalls(void) scTbl[SYS_ACCESS] = {"SYS_ACCESS", (void *)sys_access}; scTbl[SYS_TRUNCATE] = {"SYS_TRUNCATE", (void *)sys_truncate}; scTbl[SYS_FTRUNCATE] = {"SYS_FTRUNCATE", (void *)sys_ftruncate}; + scTbl[SYS_TELL] = {"SYS_TELL", (void *)sys_tell}; + scTbl[SYS_SEEK] = {"SYS_SEEK", (void *)sys_seek}; /* Process Control */ scTbl[SYS_EXIT] = {"SYS_EXIT", (void *)sys_exit};