Skip to content

Commit

Permalink
Just use normal stdio for the child process
Browse files Browse the repository at this point in the history
Make sure we flush all output so it's seen by the parent as it happens.
  • Loading branch information
slouken committed Oct 4, 2024
1 parent 8c3f88b commit 8db3b47
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions test/childprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
#include <stdio.h>
#include <errno.h>

#ifdef SDL_PLATFORM_WINDOWS
#include <windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif

int main(int argc, char *argv[]) {
SDLTest_CommonState *state;
int i;
Expand Down Expand Up @@ -101,6 +94,7 @@ int main(int argc, char *argv[]) {
for (print_i = 0; i + print_i < argc; print_i++) {
fprintf(stdout, "|%d=%s|\r\n", print_i, argv[i + print_i]);
}
fflush(stdout);
}

if (print_environment) {
Expand All @@ -111,30 +105,33 @@ int main(int argc, char *argv[]) {
}
SDL_free(env);
}
fflush(stdout);
}

#ifdef SDL_PLATFORM_WINDOWS
{
DWORD mode;
HANDLE stdout_handle = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(stdout_handle, &mode);
SetConsoleMode(stdout_handle, mode & ~(ENABLE_LINE_INPUT));
}
#else
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) & ~(O_NONBLOCK));
#endif

if (stdin_to_stdout || stdin_to_stderr || read_stdin) {
for (;;) {
char buffer[4 * 4096];
size_t result;

result = fread(buffer, 1, sizeof(buffer), stdin);
if (result == 0) {
if (errno == EAGAIN) {
clearerr(stdin);
SDL_Delay(20);
continue;
if (!feof(stdin)) {
char error[128];

if (errno == EAGAIN) {
clearerr(stdin);
SDL_Delay(20);
continue;
}

#ifdef SDL_PLATFORM_WINDOWS
if (strerror_s(error, sizeof(error), errno) != 0) {
SDL_strlcpy(error, "Unknown error", sizeof(error));
}
#else
SDL_strlcpy(error, strerror(errno), sizeof(error));
#endif
SDL_Log("Error reading from stdin: %s\n", error);
}
break;
}
Expand Down

0 comments on commit 8db3b47

Please sign in to comment.