libiberty: Handle Windows nul device in unlink-if-ordinary.c [PR108276]

Message ID fNaJU0FQkpY1sbMSTBhtyL9Fe3rKjTMaPdqQoq0VZhJBQxB1UtH_QU19Rai4usWKkETmSjqNT7cW5JaJxPnLy6iDTYpq4LHcEZsk2twCHAE=@proton.me
State New
Headers
Series libiberty: Handle Windows nul device in unlink-if-ordinary.c [PR108276] |

Commit Message

Himal Jan. 4, 2023, 3:09 a.m. UTC
  libiberty/ChangeLog:
        * unlink-if-ordinary.c (unlink_if_ordinary):
        Handle Windows nul device

---
 libiberty/unlink-if-ordinary.c | 7 +++++++
 1 file changed, 7 insertions(+)

--
2.39.0
  

Patch

diff --git a/libiberty/unlink-if-ordinary.c b/libiberty/unlink-if-ordinary.c
index 84328b216..ae9090e54 100644
--- a/libiberty/unlink-if-ordinary.c
+++ b/libiberty/unlink-if-ordinary.c
@@ -62,11 +62,18 @@  was made to unlink the file because it is special.
 int
 unlink_if_ordinary (const char *name)
 {
+/* MS-Windows 'stat' function (and in turn, S_ISREG)
+   reports the null device as a regular file.  */
+#ifdef _WIN32
+    if (stricmp (name, "nul") == 0)
+      return 1;
+#else
   struct stat st;

   if (lstat (name, &st) == 0
       && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
     return unlink (name);
+#endif

   return 1;
 }