SDL: Just use normal stdio for the child process

From 8db3b474826766937333079956d8bcb1a7c48589 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 4 Oct 2024 12:50:38 -0700
Subject: [PATCH] Just use normal stdio for the child process

Make sure we flush all output so it's seen by the parent as it happens.
---
 test/childprocess.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)

diff --git a/test/childprocess.c b/test/childprocess.c
index e30ba52e912c9..dede17717d59c 100644
--- a/test/childprocess.c
+++ b/test/childprocess.c
@@ -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;
@@ -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) {
@@ -111,19 +105,9 @@ 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];
@@ -131,10 +115,23 @@ int main(int argc, char *argv[]) {
 
             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;
             }