@@ -87,10 +87,34 @@ int run(
87
87
const std::string &std_error)
88
88
{
89
89
#ifdef _WIN32
90
- // we don't support stdin/stdout/stderr redirection on Windows
91
- PRECONDITION (std_input.empty ());
92
- PRECONDITION (std_output.empty ());
93
- PRECONDITION (std_error.empty ());
90
+ // we use the cmd.exe shell to do stdin/stdout/stderr redirection on Windows
91
+ if (!std_input.empty () || !std_output.empty () || !std_error.empty ())
92
+ {
93
+ std::vector<std::string> new_argv = argv;
94
+ new_argv.insert (new_argv.begin (), " cmd.exe" );
95
+ new_argv.insert (new_argv.begin () + 1 , " /c" );
96
+
97
+ if (!std_input.empty ())
98
+ {
99
+ new_argv.push_back (" <" );
100
+ new_argv.push_back (std_input);
101
+ }
102
+
103
+ if (!std_output.empty ())
104
+ {
105
+ new_argv.push_back (" >" );
106
+ new_argv.push_back (std_output);
107
+ }
108
+
109
+ if (!std_error.empty ())
110
+ {
111
+ new_argv.push_back (" 2>" );
112
+ new_argv.push_back (std_error);
113
+ }
114
+
115
+ // this is recursive
116
+ return run (new_argv[0 ], new_argv, " " , " " , " " );
117
+ }
94
118
95
119
// unicode version of the arguments
96
120
std::vector<std::wstring> wargv;
@@ -107,12 +131,12 @@ int run(
107
131
108
132
_argv[argv.size ()]=NULL ;
109
133
110
- // warning: the arguments may still need escaping
134
+ // warning: the arguments may still need escaping,
135
+ // as windows will concatenate the argv strings back together,
136
+ // separating them with spaces
111
137
112
138
std::wstring wide_what=widen (what);
113
-
114
139
int status=_wspawnvp (_P_WAIT, wide_what.c_str (), _argv.data ());
115
-
116
140
return status;
117
141
118
142
#else
0 commit comments