From 5fca5c774a1af6898affa22ea2009358679649db Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 31 Jul 2024 23:25:11 +0300 Subject: [PATCH] LibC: Implement umask() --- userspace/libraries/LibC/fcntl.cpp | 6 +++--- userspace/libraries/LibC/include/sys/stat.h | 3 +++ userspace/libraries/LibC/sys/stat.cpp | 11 ++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/userspace/libraries/LibC/fcntl.cpp b/userspace/libraries/LibC/fcntl.cpp index 288fa0aa12..bbfc84597f 100644 --- a/userspace/libraries/LibC/fcntl.cpp +++ b/userspace/libraries/LibC/fcntl.cpp @@ -6,7 +6,7 @@ int creat(const char* path, mode_t mode) { - return syscall(SYS_CREATE, path, S_IFREG | mode); + return syscall(SYS_CREATE, path, S_IFREG | __UMASKED_MODE(mode)); } int open(const char* path, int oflag, ...) @@ -16,7 +16,7 @@ int open(const char* path, int oflag, ...) mode_t mode = va_arg(args, mode_t); va_end(args); - return syscall(SYS_OPEN, path, oflag, mode); + return syscall(SYS_OPEN, path, oflag, __UMASKED_MODE(mode)); } int openat(int fd, const char* path, int oflag, ...) @@ -26,7 +26,7 @@ int openat(int fd, const char* path, int oflag, ...) mode_t mode = va_arg(args, mode_t); va_end(args); - return syscall(SYS_OPENAT, fd, path, oflag, mode); + return syscall(SYS_OPENAT, fd, path, oflag, __UMASKED_MODE(mode)); } int fcntl(int fildes, int cmd, ...) diff --git a/userspace/libraries/LibC/include/sys/stat.h b/userspace/libraries/LibC/include/sys/stat.h index 1a8eb393e9..b12d03b9a4 100644 --- a/userspace/libraries/LibC/include/sys/stat.h +++ b/userspace/libraries/LibC/include/sys/stat.h @@ -42,6 +42,9 @@ struct stat #define st_ctime st_ctim.tv_sec #define st_mtime st_mtim.tv_sec +extern mode_t __umask; +#define __UMASKED_MODE(mode) ((mode) & ~__umask) + #define S_IRWXU 00700 #define S_IRUSR 00400 #define S_IWUSR 00200 diff --git a/userspace/libraries/LibC/sys/stat.cpp b/userspace/libraries/LibC/sys/stat.cpp index 923e6dec1f..8e2a270a88 100644 --- a/userspace/libraries/LibC/sys/stat.cpp +++ b/userspace/libraries/LibC/sys/stat.cpp @@ -4,6 +4,8 @@ #include #include +mode_t __umask = 0; + int chmod(const char* path, mode_t mode) { return syscall(SYS_CHMOD, path, mode); @@ -29,7 +31,14 @@ int stat(const char* __restrict path, struct stat* __restrict buf) return syscall(SYS_STAT, path, buf, 0); } +mode_t umask(mode_t cmask) +{ + mode_t old = __umask; + __umask = cmask; + return old; +} + int mkdir(const char* path, mode_t mode) { - return syscall(SYS_CREATE_DIR, path, mode); + return syscall(SYS_CREATE_DIR, path, __UMASKED_MODE(mode)); }