Skip to content

Commit c03655a

Browse files
committed
Merge branch 'subprocess' of github.com:perazz/stdlib into subprocess
2 parents ed0565c + 2c58fca commit c03655a

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
@@ -56,28 +56,40 @@ void process_create_windows(const char* cmd, const char* stdin_stream,
5656
fclose(stdin_fp);
5757
}
5858

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

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

0 commit comments

Comments
 (0)