SDL: SDL_RWFromFile, stdio: allow named pipes along with regular files. (177a8)

From 177a836653e0d2690ff8802dade165c392502f81 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Wed, 6 Mar 2024 18:50:00 +0300
Subject: [PATCH] SDL_RWFromFile, stdio: allow named pipes along with regular
 files.

Fixes https://github.com/libsdl-org/SDL/issues/9174
---
 src/file/SDL_rwops.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
index 439f20a5e39a0..bf47ba7110b4d 100644
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -38,7 +38,6 @@
 #include <stdio.h>
 #include <sys/stat.h>
 #endif
-
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
 #endif
@@ -525,16 +524,17 @@ static int SDLCALL mem_close(SDL_RWops *context)
 /* Functions to create SDL_RWops structures from various data sources */
 
 #if defined(HAVE_STDIO_H) && !(defined(__WIN32__) || defined(__GDK__))
-static SDL_bool SDL_IsRegularFile(FILE *f)
+static SDL_bool IsRegularFileOrPipe(FILE *f)
 {
     #ifdef __WINRT__
     struct __stat64 st;
-    if (_fstat64(_fileno(f), &st) < 0 || (st.st_mode & _S_IFMT) != _S_IFREG) {
+    if (_fstat64(_fileno(f), &st) < 0 ||
+        !((st.st_mode & _S_IFMT) == _S_IFREG || (st.st_mode & _S_IFMT) == _S_IFIFO)) {
         return SDL_FALSE;
     }
     #else
     struct stat st;
-    if (fstat(fileno(f), &st) < 0 || !S_ISREG(st.st_mode)) {
+    if (fstat(fileno(f), &st) < 0 || !(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) {
         return SDL_FALSE;
     }
     #endif
@@ -555,9 +555,9 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
     if (*file == '/') {
         FILE *fp = fopen(file, mode);
         if (fp) {
-            if (!SDL_IsRegularFile(fp)) {
+            if (!IsRegularFileOrPipe(fp)) {
                 fclose(fp);
-                SDL_SetError("%s is not a regular file", file);
+                SDL_SetError("%s is not a regular file or pipe", file);
                 return NULL;
             }
             return SDL_RWFromFP(fp, 1);
@@ -575,9 +575,9 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
             fp = fopen(path, mode);
             SDL_stack_free(path);
             if (fp) {
-                if (!SDL_IsRegularFile(fp)) {
+                if (!IsRegularFileOrPipe(fp)) {
                     fclose(fp);
-                    SDL_SetError("%s is not a regular file", path);
+                    SDL_SetError("%s is not a regular file or pipe", path);
                     return NULL;
                 }
                 return SDL_RWFromFP(fp, 1);
@@ -633,10 +633,10 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
 #endif
         if (!fp) {
             SDL_SetError("Couldn't open %s", file);
-        } else if (!SDL_IsRegularFile(fp)) {
+        } else if (!IsRegularFileOrPipe(fp)) {
             fclose(fp);
             fp = NULL;
-            SDL_SetError("%s is not a regular file", file);
+            SDL_SetError("%s is not a regular file or pipe", file);
         } else {
             rwops = SDL_RWFromFP(fp, SDL_TRUE);
         }