]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
feat: truncate/ftruncate syscalls (78/79) + ulibc wrappers
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 02:58:41 +0000 (23:58 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:20:50 +0000 (23:20 -0300)
include/syscall.h
src/kernel/syscall.c
user/ulibc/include/syscall.h
user/ulibc/include/unistd.h
user/ulibc/src/unistd.c

index 3e3a36e4ef233942cd9cc724f4e0a69915ac5177..a82cb3bdf97e8f138664b15d83150b5f8fa70d15 100644 (file)
@@ -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
index 3bb789bcf6841446feb0ddbbdae1f6f33ae5d78d..25cd3beb3e41dba3488c1a42f1c936ce69191eb3 100644 (file)
@@ -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;
 }
 
index 586b404ea25c53fa0fc508f566b07c8c1a1f382a..333867ad9154d344d51bceed2796fc58332d631d 100644 (file)
@@ -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 */
index 5d4d7128221e33ec8009ec7054790cd28c7309fa..841053bb67c8836c35491a46c09953c0a88d3657 100644 (file)
@@ -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));
index b27226f6110d63c95a4ccfd5b68324777c007921..302319515b0c24ae884c2794b2d5072e570cdafa 100644 (file)
@@ -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);
 }