]> 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 b6418d71b48abb880cdff219895c81becc398e49..adee948697efaa6a2556809be2c9c7b157f24ba5 100644 (file)
@@ -110,6 +110,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 8fab4e38a744737caf0420c8cdcb6119a79eacce..949bd5e0fa825397d20b04ffdc1d9b440e4e7275 100644 (file)
@@ -2238,7 +2238,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;
     }
@@ -2339,6 +2340,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 8404b67d34722d891e38b3e7c72221027de140dd..f43ab0275129eb1a8bb90ab1395f4359403ecd7d 100644 (file)
@@ -69,6 +69,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 356780c91728bf76fd0985e9e9a2adc4442e522b..af7f24dff72974e80cffdf432d303ec41d3dffb2 100644 (file)
@@ -49,6 +49,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 8e2e0d297d88eabedbce4232a942218cc5902417..f1c044839f8987f783c162162e774e4b2c0b2505 100644 (file)
@@ -123,6 +123,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);
 }