From: Piotr Caban piotr@codeweavers.com
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=57008 --- dlls/msvcrt/file.c | 7 +++++-- dlls/msvcrt/tests/file.c | 18 ++++++++++++++++++ dlls/ucrtbase/tests/file.c | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index 05630fb54b1..422e7712660 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -618,10 +618,13 @@ static int msvcrt_alloc_fd(HANDLE hand, int flag) /* caller must hold the files lock */ static FILE* msvcrt_alloc_fp(void) { - int i; + int i = 0; FILE *file;
- for (i = 3; i < MSVCRT_max_streams; i++) +#if _MSVCR_VER >= 140 + i = 3; +#endif + for (; i < MSVCRT_max_streams; i++) { file = msvcrt_get_file(i); if (!file) diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c index bce6a0b0509..9ce3654bebe 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -3123,6 +3123,23 @@ static void test_std_stream_buffering(void) ok(DeleteFileA("std_stream_test.tmp"), "DeleteFile failed\n"); }
+static void test_std_stream_open(void) +{ + FILE *f; + int fd; + + fd = _dup(STDIN_FILENO); + ok(fd != -1, "_dup failed\n"); + + ok(!fclose(stdin), "fclose failed\n"); + f = fopen("nul", "r"); + ok(f == stdin, "f = %p, expected %p\n", f, stdin); + ok(_fileno(f) == STDIN_FILENO, "_fileno(f) = %d\n", _fileno(f)); + + _dup2(fd, STDIN_FILENO); + close(fd); +} + START_TEST(file) { int arg_c; @@ -3200,6 +3217,7 @@ START_TEST(file) test_open_hints(); test_ioinfo_flags(); test_std_stream_buffering(); + test_std_stream_open();
/* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report * file contains lines in the correct order diff --git a/dlls/ucrtbase/tests/file.c b/dlls/ucrtbase/tests/file.c index d128a9bffb0..9bfb7af3384 100644 --- a/dlls/ucrtbase/tests/file.c +++ b/dlls/ucrtbase/tests/file.c @@ -140,8 +140,31 @@ static void test_iobuf_layout(void) unlink(tempf); }
+static void test_std_stream_open(void) +{ + FILE *f; + int fd; + + fd = _dup(STDIN_FILENO); + ok(fd != -1, "_dup failed\n"); + + ok(!fclose(stdin), "fclose failed\n"); + f = fopen("nul", "r"); + ok(f != stdin, "f = %p, stdin = %p\n", f, stdin); + ok(_fileno(f) == STDIN_FILENO, "_fileno(f) = %d\n", _fileno(f)); + ok(!fclose(f), "fclose failed\n"); + + f = freopen("nul", "r", stdin); + ok(f == stdin, "f = %p, expected %p\n", f, stdin); + ok(_fileno(f) == STDIN_FILENO, "_fileno(f) = %d\n", _fileno(f)); + + _dup2(fd, STDIN_FILENO); + close(fd); +} + START_TEST(file) { test_std_stream_buffering(); test_iobuf_layout(); + test_std_stream_open(); }