Skip to content

Commit 2b5e925

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2849 from diffblue/run-with-redirection-windows
run now does I/O redirection on Windows
2 parents c764708 + 9c22b7a commit 2b5e925

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/util/run.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,34 @@ int run(
8383
const std::string &std_error)
8484
{
8585
#ifdef _WIN32
86-
// we don't support stdin/stdout/stderr redirection on Windows
87-
PRECONDITION(std_input.empty());
88-
PRECONDITION(std_output.empty());
89-
PRECONDITION(std_error.empty());
86+
// we use the cmd.exe shell to do stdin/stdout/stderr redirection on Windows
87+
if(!std_input.empty() || !std_output.empty() || !std_error.empty())
88+
{
89+
std::vector<std::string> new_argv = argv;
90+
new_argv.insert(new_argv.begin(), "cmd.exe");
91+
new_argv.insert(new_argv.begin() + 1, "/c");
92+
93+
if(!std_input.empty())
94+
{
95+
new_argv.push_back("<");
96+
new_argv.push_back(std_input);
97+
}
98+
99+
if(!std_output.empty())
100+
{
101+
new_argv.push_back(">");
102+
new_argv.push_back(std_output);
103+
}
104+
105+
if(!std_error.empty())
106+
{
107+
new_argv.push_back("2>");
108+
new_argv.push_back(std_error);
109+
}
110+
111+
// this is recursive
112+
return run(new_argv[0], new_argv, "", "", "");
113+
}
90114

91115
// unicode version of the arguments
92116
std::vector<std::wstring> wargv;
@@ -103,12 +127,12 @@ int run(
103127

104128
_argv[argv.size()]=NULL;
105129

106-
// warning: the arguments may still need escaping
130+
// warning: the arguments may still need escaping,
131+
// as windows will concatenate the argv strings back together,
132+
// separating them with spaces
107133

108134
std::wstring wide_what=widen(what);
109-
110135
int status=_wspawnvp(_P_WAIT, wide_what.c_str(), _argv.data());
111-
112136
return status;
113137

114138
#else

0 commit comments

Comments
 (0)