Skip to content

Commit 2c58fca

Browse files
committed
on Windows, redirect to NUL if output not requested
1 parent 237e9ff commit 2c58fca

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/stdlib_system_subprocess.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,40 @@ void process_create_windows(const char* cmd, const char* stdin_stream,
5757
fclose(stdin_fp);
5858
}
5959

60-
// Open stdout file if provided
60+
// Open stdout file if provided, otherwise use the null device
6161
if (stdout_file) {
6262
hStdout = CreateFile(stdout_file, GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
6363
if (hStdout == INVALID_HANDLE_VALUE) {
6464
fprintf(stderr, "Failed to open stdout file\n");
6565
return;
6666
}
67-
si.hStdOutput = hStdout;
68-
si.dwFlags |= STARTF_USESTDHANDLES;
67+
} else {
68+
hStdout = CreateFile("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
69+
if (hStdout == INVALID_HANDLE_VALUE) {
70+
fprintf(stderr, "Failed to open null device for stdout\n");
71+
return;
72+
}
6973
}
74+
si.hStdOutput = hStdout;
75+
si.dwFlags |= STARTF_USESTDHANDLES;
7076

71-
// Open stderr file if provided
77+
// Open stderr file if provided, otherwise use the null device
7278
if (stderr_file) {
7379
hStderr = CreateFile(stderr_file, GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
7480
if (hStderr == INVALID_HANDLE_VALUE) {
7581
fprintf(stderr, "Failed to open stderr file\n");
7682
return;
7783
}
78-
si.hStdError = hStderr;
79-
si.dwFlags |= STARTF_USESTDHANDLES;
84+
} else {
85+
hStderr = CreateFile("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
86+
if (hStderr == INVALID_HANDLE_VALUE) {
87+
fprintf(stderr, "Failed to open null device for stderr\n");
88+
return;
89+
}
8090
}
81-
91+
si.hStdError = hStderr;
92+
si.dwFlags |= STARTF_USESTDHANDLES;
93+
8294
// Prepare the command line with redirected stdin
8395
char full_cmd[4096];
8496
if (stdin_file) {

0 commit comments

Comments
 (0)