From 8abbd0ad60aece1a582e1bfea0fe4ed72f6bc130 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 20 Apr 2023 23:21:42 +0200 Subject: [PATCH] getumask: Make it work on native Windows. * lib/getumask.c (getumask): When TMPDIR is unset, try TMP and TEMP. * tests/test-getumask.c (ASSUME_UMASK_CONSTANT): Define to 1 on native Windows. --- ChangeLog | 7 +++++++ lib/getumask.c | 12 +++++++++++- tests/test-getumask.c | 7 ++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 88e8748c97..a0d37b10ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-04-20 Bruno Haible + + getumask: Make it work on native Windows. + * lib/getumask.c (getumask): When TMPDIR is unset, try TMP and TEMP. + * tests/test-getumask.c (ASSUME_UMASK_CONSTANT): Define to 1 on native + Windows. + 2023-04-20 Bruno Haible posixtm tests: Fix link error. diff --git a/lib/getumask.c b/lib/getumask.c index 066d6cd361..e979f9b874 100644 --- a/lib/getumask.c +++ b/lib/getumask.c @@ -1,5 +1,5 @@ /* Retrieve the umask of the process (multithread-safe). - Copyright (C) 2020-2022 Free Software Foundation, Inc. + Copyright (C) 2020-2023 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -106,6 +106,16 @@ getumask (void) { /* Create a temporary file and inspect its access permissions. */ const char *tmpdir = getenv ("TMPDIR"); +# if defined _WIN32 && !defined __CYGWIN__ + if (tmpdir == NULL || *tmpdir == '\0') + { + /* On native Windows, TMPDIR is typically not set, and /tmp does not + exist. $TMP and $TEMP can be used instead. */ + tmpdir = getenv ("TMP"); + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = getenv ("TEMP"); + } +# endif if (tmpdir == NULL || *tmpdir == '\0') tmpdir = "/tmp"; size_t tmpdir_length = strlen (tmpdir); diff --git a/tests/test-getumask.c b/tests/test-getumask.c index 552b5c9a9c..ab1e48ecd9 100644 --- a/tests/test-getumask.c +++ b/tests/test-getumask.c @@ -1,5 +1,5 @@ /* Test of retrieving the umask of the process. - Copyright (C) 2020-2022 Free Software Foundation, Inc. + Copyright (C) 2020-2023 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,6 +25,11 @@ SIGNATURE_CHECK (getumask, mode_t, (void)); #include "macros.h" +#if defined _WIN32 && !defined __CYGWIN__ +/* On native Windows, getumask() always returns 0111. */ +# define ASSUME_UMASK_CONSTANT 1 +#endif + int main (void) { -- 2.39.5