posix: sdl: Stop reading STDIN on EOF

If EOF is read on stdin (such as when it is /dev/null), stop calling
select() on it. Otherwise, the code goes into an infinite loop
attempting to read from stdin which is always ready but never has any
data and it never calls the timer callback to progress.
This commit is contained in:
Joshua Watt 2023-03-08 16:34:14 -06:00 committed by Christoph Oelckers
parent 9c765a70bf
commit d2ac72129d

View file

@ -257,6 +257,7 @@ bool FTTYStartupScreen::NetLoop(bool (*timer_callback)(void *), void *userdata)
struct timeval tv; struct timeval tv;
int retval; int retval;
char k; char k;
bool stdin_eof = false;
for (;;) for (;;)
{ {
@ -265,7 +266,10 @@ bool FTTYStartupScreen::NetLoop(bool (*timer_callback)(void *), void *userdata)
tv.tv_usec = 500000; tv.tv_usec = 500000;
FD_ZERO (&rfds); FD_ZERO (&rfds);
FD_SET (STDIN_FILENO, &rfds); if (!stdin_eof)
{
FD_SET (STDIN_FILENO, &rfds);
}
retval = select (1, &rfds, NULL, NULL, &tv); retval = select (1, &rfds, NULL, NULL, &tv);
@ -281,13 +285,21 @@ bool FTTYStartupScreen::NetLoop(bool (*timer_callback)(void *), void *userdata)
return true; return true;
} }
} }
else if (read (STDIN_FILENO, &k, 1) == 1) else
{ {
// Check input on stdin ssize_t amt = read (STDIN_FILENO, &k, 1); // Check input on stdin
if (k == 'q' || k == 'Q') if (amt == 0)
{ {
fprintf (stderr, "\nNetwork game synchronization aborted."); // EOF. Stop reading
return false; stdin_eof = true;
}
else if (amt == 1)
{
if (k == 'q' || k == 'Q')
{
fprintf (stderr, "\nNetwork game synchronization aborted.");
return false;
}
} }
} }
} }