Skip to content

Commit 8db3b47

Browse files
committed
Just use normal stdio for the child process
Make sure we flush all output so it's seen by the parent as it happens.
1 parent 8c3f88b commit 8db3b47

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

test/childprocess.c

+19-22
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
#include <stdio.h>
66
#include <errno.h>
77

8-
#ifdef SDL_PLATFORM_WINDOWS
9-
#include <windows.h>
10-
#else
11-
#include <fcntl.h>
12-
#include <unistd.h>
13-
#endif
14-
158
int main(int argc, char *argv[]) {
169
SDLTest_CommonState *state;
1710
int i;
@@ -101,6 +94,7 @@ int main(int argc, char *argv[]) {
10194
for (print_i = 0; i + print_i < argc; print_i++) {
10295
fprintf(stdout, "|%d=%s|\r\n", print_i, argv[i + print_i]);
10396
}
97+
fflush(stdout);
10498
}
10599

106100
if (print_environment) {
@@ -111,30 +105,33 @@ int main(int argc, char *argv[]) {
111105
}
112106
SDL_free(env);
113107
}
108+
fflush(stdout);
114109
}
115110

116-
#ifdef SDL_PLATFORM_WINDOWS
117-
{
118-
DWORD mode;
119-
HANDLE stdout_handle = GetStdHandle(STD_INPUT_HANDLE);
120-
GetConsoleMode(stdout_handle, &mode);
121-
SetConsoleMode(stdout_handle, mode & ~(ENABLE_LINE_INPUT));
122-
}
123-
#else
124-
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) & ~(O_NONBLOCK));
125-
#endif
126-
127111
if (stdin_to_stdout || stdin_to_stderr || read_stdin) {
128112
for (;;) {
129113
char buffer[4 * 4096];
130114
size_t result;
131115

132116
result = fread(buffer, 1, sizeof(buffer), stdin);
133117
if (result == 0) {
134-
if (errno == EAGAIN) {
135-
clearerr(stdin);
136-
SDL_Delay(20);
137-
continue;
118+
if (!feof(stdin)) {
119+
char error[128];
120+
121+
if (errno == EAGAIN) {
122+
clearerr(stdin);
123+
SDL_Delay(20);
124+
continue;
125+
}
126+
127+
#ifdef SDL_PLATFORM_WINDOWS
128+
if (strerror_s(error, sizeof(error), errno) != 0) {
129+
SDL_strlcpy(error, "Unknown error", sizeof(error));
130+
}
131+
#else
132+
SDL_strlcpy(error, strerror(errno), sizeof(error));
133+
#endif
134+
SDL_Log("Error reading from stdin: %s\n", error);
138135
}
139136
break;
140137
}

0 commit comments

Comments
 (0)