From: Tulio A M Mendes Date: Thu, 12 Feb 2026 02:58:41 +0000 (-0300) Subject: feat: truncate/ftruncate syscalls (78/79) + ulibc wrappers X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=fcc9defb8665c38dd76b0d88a6b1aaeb470f25e2;p=AdrOS.git feat: truncate/ftruncate syscalls (78/79) + ulibc wrappers --- diff --git a/include/syscall.h b/include/syscall.h index 3e3a36e..a82cb3b 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -101,6 +101,11 @@ enum { SYSCALL_UMASK = 75, SYSCALL_SETUID = 76, SYSCALL_SETGID = 77, + SYSCALL_TRUNCATE = 78, + SYSCALL_FTRUNCATE = 79, + SYSCALL_SIGSUSPEND = 80, + SYSCALL_READV = 81, + SYSCALL_WRITEV = 82, }; #endif diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 3bb789b..25cd3be 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2229,7 +2229,8 @@ void syscall_handler(struct registers* regs) { } if (syscall_no == SYSCALL_PREAD || syscall_no == SYSCALL_PWRITE || - syscall_no == SYSCALL_ACCESS) { + syscall_no == SYSCALL_ACCESS || syscall_no == SYSCALL_TRUNCATE || + syscall_no == SYSCALL_FTRUNCATE) { posix_ext_syscall_dispatch(regs, syscall_no); return; } @@ -2330,6 +2331,32 @@ static void posix_ext_syscall_dispatch(struct registers* regs, uint32_t syscall_ return; } + if (syscall_no == SYSCALL_FTRUNCATE) { + int fd = (int)regs->ebx; + uint32_t length = regs->ecx; + struct file* f = fd_get(fd); + if (!f || !f->node) { regs->eax = (uint32_t)-EBADF; return; } + if (!(f->node->flags & FS_FILE)) { regs->eax = (uint32_t)-EINVAL; return; } + f->node->length = length; + regs->eax = 0; + return; + } + + if (syscall_no == SYSCALL_TRUNCATE) { + const char* user_path = (const char*)regs->ebx; + uint32_t length = regs->ecx; + if (!user_path) { regs->eax = (uint32_t)-EFAULT; return; } + char path[128]; + int prc = path_resolve_user(user_path, path, sizeof(path)); + if (prc < 0) { regs->eax = (uint32_t)prc; return; } + fs_node_t* node = vfs_lookup(path); + if (!node) { regs->eax = (uint32_t)-ENOENT; return; } + if (!(node->flags & FS_FILE)) { regs->eax = (uint32_t)-EISDIR; return; } + node->length = length; + regs->eax = 0; + return; + } + regs->eax = (uint32_t)-ENOSYS; } diff --git a/user/ulibc/include/syscall.h b/user/ulibc/include/syscall.h index 586b404..333867a 100644 --- a/user/ulibc/include/syscall.h +++ b/user/ulibc/include/syscall.h @@ -60,6 +60,11 @@ enum { SYS_UMASK = 75, SYS_SETUID = 76, SYS_SETGID = 77, + SYS_TRUNCATE = 78, + SYS_FTRUNCATE = 79, + SYS_SIGSUSPEND = 80, + SYS_READV = 81, + SYS_WRITEV = 82, }; /* Raw syscall wrappers — up to 5 args via INT 0x80 */ diff --git a/user/ulibc/include/unistd.h b/user/ulibc/include/unistd.h index 5d4d712..841053b 100644 --- a/user/ulibc/include/unistd.h +++ b/user/ulibc/include/unistd.h @@ -40,6 +40,8 @@ int pwrite(int fd, const void* buf, size_t count, int offset); int access(const char* path, int mode); int setuid(int uid); int setgid(int gid); +int truncate(const char* path, int length); +int ftruncate(int fd, int length); void* brk(void* addr); void _exit(int status) __attribute__((noreturn)); diff --git a/user/ulibc/src/unistd.c b/user/ulibc/src/unistd.c index b27226f..3023195 100644 --- a/user/ulibc/src/unistd.c +++ b/user/ulibc/src/unistd.c @@ -114,6 +114,14 @@ int setgid(int gid) { return __syscall_ret(_syscall1(SYS_SETGID, gid)); } +int truncate(const char* path, int length) { + return __syscall_ret(_syscall2(SYS_TRUNCATE, (int)path, length)); +} + +int ftruncate(int fd, int length) { + return __syscall_ret(_syscall2(SYS_FTRUNCATE, fd, length)); +} + void* brk(void* addr) { return (void*)_syscall1(SYS_BRK, (int)addr); }