From 23968c9f1116ab35d2f7c45be40ae18896517b79 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sat, 23 Nov 2024 15:55:21 +0100 Subject: vfs: Support O_TRUNC --- kernel/fs/vfs.c | 6 +++++- userland/libc/stdio/fopen.c | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c index 25e2423..0d03dce 100644 --- a/kernel/fs/vfs.c +++ b/kernel/fs/vfs.c @@ -287,7 +287,11 @@ int vfs_open(const char *file, int flags, int mode) { } // FIXME: Maybe it is sometimes a TTY? - return vfs_create_fd(flags, mode, 0 /*is_tty*/, inode, NULL); + int rc = vfs_create_fd(flags, mode, 0 /*is_tty*/, inode, NULL); + if (flags & O_TRUNC) { + vfs_ftruncate(rc, 0); + } + return rc; } int vfs_close_process(process_t *p, int fd) { diff --git a/userland/libc/stdio/fopen.c b/userland/libc/stdio/fopen.c index fe0b5cf..38ccbf1 100644 --- a/userland/libc/stdio/fopen.c +++ b/userland/libc/stdio/fopen.c @@ -18,7 +18,7 @@ FILE *fopen(const char *pathname, const char *mode) { flag |= O_READ; break; case 'w': - flag |= O_WRITE; + flag |= O_WRITE | O_TRUNC; break; case 'a': flag |= O_APPEND; @@ -36,9 +36,6 @@ FILE *fopen(const char *pathname, const char *mode) { close(fd); return NULL; } - if(flag & O_WRITE) { - ftruncate(fd, 0); - } r->has_control_over_the_fd = 1; return r; } -- cgit v1.2.3