From: Piotr Caban Subject: [PATCH 3/3] ucrtbase: Add support for x mode in fopen. Message-Id: Date: Mon, 11 Oct 2021 16:15:39 +0200 From: Ted Lyngmo Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51846 Signed-off-by: Ted Lyngmo Signed-off-by: Piotr Caban --- dlls/msvcrt/file.c | 7 ++++++ dlls/ucrtbase/tests/misc.c | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index da43316ebb8..274ddd5a29b 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -1586,6 +1586,13 @@ static int msvcrt_get_flags(const wchar_t* mode, int *open_flags, int* stream_fl *open_flags |= _O_TEXT; *open_flags &= ~_O_BINARY; break; +#if _MSVCR_VER>=140 + case 'x': + if(!MSVCRT_CHECK_PMT((*open_flags & (_O_CREAT | _O_APPEND)) == _O_CREAT)) + return -1; + *open_flags |= _O_EXCL; + break; +#endif case 'D': *open_flags |= _O_TEMPORARY; break; diff --git a/dlls/ucrtbase/tests/misc.c b/dlls/ucrtbase/tests/misc.c index c2b6dc18d28..a91ea6c98b8 100644 --- a/dlls/ucrtbase/tests/misc.c +++ b/dlls/ucrtbase/tests/misc.c @@ -1539,6 +1539,54 @@ static void test_fenv(void) ok(!except, "expected 0, got %lx\n", except); } +static void test_fopen_exclusive( void ) +{ + char path[MAX_PATH*2]; + DWORD len; + FILE *fp; + + if (!GetProcAddress(GetModuleHandleA("ucrtbase.dll"), "__std_terminate")) + { + win_skip("skipping fopen x mode tests.\n"); + return; + } + + len = GetTempPathA(MAX_PATH, path); + ok(len, "GetTempPathA failed\n"); + strcat(path, "\\fileexcl.tst"); + + fp = fopen(path, "wx"); + ok(fp != NULL, "creating file with mode wx failed\n"); + fclose(fp); + + fp = fopen(path, "wx"); + ok(!fp, "overwrote existing file with mode wx\n"); + unlink(path); + + fp = fopen(path, "w+x"); + ok(fp != NULL, "creating file with mode w+x failed\n"); + fclose(fp); + + fp = fopen(path, "w+x"); + ok(!fp, "overwrote existing file with mode w+x\n"); + + SET_EXPECT(global_invalid_parameter_handler); + fp = fopen(path, "rx"); + CHECK_CALLED(global_invalid_parameter_handler); + ok(!fp, "opening file with mode rx succeeded\n"); + unlink(path); + + SET_EXPECT(global_invalid_parameter_handler); + fp = fopen(path, "xw"); + CHECK_CALLED(global_invalid_parameter_handler); + ok(!fp, "creating file with mode xw succeeded\n"); + + fp = fopen(path, "wbx"); + ok(fp != NULL, "creating file with mode wx failed\n"); + fclose(fp); + unlink(path); +} + START_TEST(misc) { int arg_c; @@ -1580,4 +1628,5 @@ START_TEST(misc) test_clock(); test_thread_storage(); test_fenv(); + test_fopen_exclusive(); }