summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2024-01-26 15:15:39 (GMT)
committerKitware Robot <kwrobot@kitware.com>2024-01-26 15:15:47 (GMT)
commitb7c11ded92997ac232989e208642f9646e3f5b59 (patch)
tree0a3ea4e79425e98fd7b39690966361b6fc6864fa /Source
parentd9b9f630832174c2b6ee31d8a06f6e587ab2dd84 (diff)
parent96010cc96848dc2ef731115d5e459e343bdaf024 (diff)
downloadCMake-b7c11ded92997ac232989e208642f9646e3f5b59.zip
CMake-b7c11ded92997ac232989e208642f9646e3f5b59.tar.gz
CMake-b7c11ded92997ac232989e208642f9646e3f5b59.tar.bz2
Merge topic 'stdio-streams-always'
96010cc968 Ensure stdin, stdout, stderr FILE streams are open on Windows Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !9189
Diffstat (limited to 'Source')
-rw-r--r--Source/cmSystemTools.cxx43
1 files changed, 15 insertions, 28 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index bd914e5..18ca85b 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -2377,43 +2377,30 @@ cmSystemTools::WaitForLineResult cmSystemTools::WaitForLine(
}
#ifdef _WIN32
-static void EnsureStdPipe(DWORD fd)
+static void EnsureStdPipe(int stdFd, DWORD nStdHandle, FILE* stream,
+ const wchar_t* mode)
{
- if (GetStdHandle(fd) != INVALID_HANDLE_VALUE) {
+ if (fileno(stream) >= 0) {
return;
}
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(sa);
- sa.lpSecurityDescriptor = nullptr;
- sa.bInheritHandle = TRUE;
-
- HANDLE h = CreateFileW(
- L"NUL",
- fd == STD_INPUT_HANDLE ? FILE_GENERIC_READ
- : FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES,
- FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, OPEN_EXISTING, 0, nullptr);
-
- if (h == INVALID_HANDLE_VALUE) {
- LPSTR message = nullptr;
- DWORD size = FormatMessageA(
- FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPSTR)&message, 0, nullptr);
- std::string msg = std::string(message, size);
- LocalFree(message);
- std::cerr << "failed to open NUL for missing stdio pipe: " << msg;
+ _close(stdFd);
+ _wfreopen(L"NUL", mode, stream);
+ int fd = fileno(stream);
+ if (fd < 0) {
+ perror("failed to open NUL for missing stdio pipe");
abort();
}
-
- SetStdHandle(fd, h);
+ if (fd != stdFd) {
+ _dup2(fd, stdFd);
+ }
+ SetStdHandle(nStdHandle, reinterpret_cast<HANDLE>(_get_osfhandle(fd)));
}
void cmSystemTools::EnsureStdPipes()
{
- EnsureStdPipe(STD_INPUT_HANDLE);
- EnsureStdPipe(STD_OUTPUT_HANDLE);
- EnsureStdPipe(STD_ERROR_HANDLE);
+ EnsureStdPipe(0, STD_INPUT_HANDLE, stdin, L"rb");
+ EnsureStdPipe(1, STD_OUTPUT_HANDLE, stdout, L"wb");
+ EnsureStdPipe(2, STD_ERROR_HANDLE, stderr, L"wb");
}
#else
static void EnsureStdPipe(int fd)