From a85653040477ec4e8a722de3af5afd04ee66bd59 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 7 Jan 2019 07:54:40 +0000 Subject: win32: implement umask(2) umask() in the Microsoft C runtime takes different arguments to umask(2). Implement a fake umask(2) that remembers the mask and calls the Microsoft umask() with an appropriate value. Since the mask won't be inherited by children use an environment variable to pass any value set by the shell built-in umask. --- include/mingw.h | 5 +++++ libbb/appletlib.c | 9 +++++++++ shell/ash.c | 3 +++ win32/mingw.c | 14 ++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/include/mingw.h b/include/mingw.h index b712e4ec0..2dc3ddfd7 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -243,6 +243,11 @@ NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len #define S_IWOTH (S_IWGRP >> 3) #define S_IXOTH (S_IXGRP >> 3) +mode_t mingw_umask(mode_t mode); +#define umask mingw_umask + +#define DEFAULT_UMASK 0002 + IMPL(fchmod,int,0,int fildes UNUSED_PARAM, mode_t mode UNUSED_PARAM); NOIMPL(fchown,int fd UNUSED_PARAM, uid_t uid UNUSED_PARAM, gid_t gid UNUSED_PARAM); int mingw_mkdir(const char *path, int mode); diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 8a66b3303..ba52ef581 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -1081,6 +1081,15 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar applet_name = name; #if ENABLE_PLATFORM_MINGW32 strcpy(bb_applet_name, applet_name); + + { + const char *vmask; + unsigned int mask; + + vmask = getenv("BB_UMASK"); + if (vmask && sscanf(vmask, "%o", &mask) == 1) + umask((mode_t)(mask&0777)); + } #endif /* Special case. POSIX says "test --help" diff --git a/shell/ash.c b/shell/ash.c index 5195a8557..d9a4a8cfa 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -14489,6 +14489,9 @@ umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) if (!isdigit(modestr[0])) mask ^= 0777; umask(mask); +#if ENABLE_PLATFORM_MINGW32 + setvareq(xasprintf("BB_UMASK=0%o", mask), VEXPORT|VNOSAVE); +#endif } return 0; } diff --git a/win32/mingw.c b/win32/mingw.c index 8d1da5199..c72a306ac 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -318,6 +318,20 @@ static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fd } } +#undef umask +mode_t mingw_umask(mode_t new_mode) +{ + static mode_t old_mode = DEFAULT_UMASK; + mode_t tmp_mode; + + tmp_mode = old_mode; + old_mode = new_mode; + + umask((new_mode & S_IWUSR) ? _S_IWRITE : 0); + + return tmp_mode; +} + /* * Examine a file's contents to determine if it can be executed. This * should be a last resort: in most cases it's much more efficient to -- cgit v1.2.3-55-g6feb