]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
stat: Support the file name "/dev/null" on native Windows.
authorBruno Haible <bruno@clisp.org>
Fri, 13 Jun 2025 10:27:44 +0000 (12:27 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 13 Jun 2025 10:27:44 +0000 (12:27 +0200)
Reported by Collin Funk in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-06/msg00119.html>.

* lib/stat.c (rpl_stat): On native Windows, map "/dev/null" to "NUL".
* tests/test-fstat.c (main): Test /dev/null also on native Windows.
* tests/test-stat.h (test_stat_func): Likewise.
* tests/test-lstat.h (test_lstat_func): Likewise.
* doc/posix-functions/stat.texi: Mention problem of null device name.
* doc/posix-functions/lstat.texi: Likewise.

ChangeLog
doc/posix-functions/lstat.texi
doc/posix-functions/stat.texi
lib/stat.c
tests/test-fstat.c
tests/test-lstat.h
tests/test-stat.h

index 797f5be68acaddde49e73af6d9c1b101f0e2c031..80fbd2b39747006311e57fb16c30fda570932af5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2025-06-13  Bruno Haible  <bruno@clisp.org>
+
+       stat: Support the file name "/dev/null" on native Windows.
+       Reported by Collin Funk in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2025-06/msg00119.html>.
+       * lib/stat.c (rpl_stat): On native Windows, map "/dev/null" to "NUL".
+       * tests/test-fstat.c (main): Test /dev/null also on native Windows.
+       * tests/test-stat.h (test_stat_func): Likewise.
+       * tests/test-lstat.h (test_lstat_func): Likewise.
+       * doc/posix-functions/stat.texi: Mention problem of null device name.
+       * doc/posix-functions/lstat.texi: Likewise.
+
 2025-06-12  Bruno Haible  <bruno@clisp.org>
 
        lstat tests: Avoid test failure on Solaris.
index 43728a534397389b326cf7935ef43f3b11d08e6c..7bbc32a81eeb7da0075def621c6bd63729588627 100644 (file)
@@ -33,6 +33,9 @@ nonpositive @code{tv_sec} value, @code{tv_nsec} might be in the range
 offset from @code{tv_sec}.  Solaris 11.4 is similar, except that
 @code{tv_sec} might also be @minus{}1000000000.
 @item
+On Windows platforms (excluding Cygwin), a different name has to be used
+for the null device, namely @code{"NUL"} instead of @code{"/dev/null"}.
+@item
 On Windows platforms (excluding Cygwin), symlinks are not supported, so
 @code{lstat} does not exist.
 @end itemize
index 6e0df396a521b689b87e33addf42512209096420..80762977a819694b85a21268e1dd39f94f99cc62 100644 (file)
@@ -45,6 +45,9 @@ nonpositive @code{tv_sec} value, @code{tv_nsec} might be in the range
 @minus{}999999999..@minus{}1, representing a negative nanoseconds
 offset from @code{tv_sec}.  Solaris 11.4 is similar, except that
 @code{tv_sec} might also be @minus{}1000000000.
+@item
+On Windows platforms (excluding Cygwin), a different name has to be used
+for the null device, namely @code{"NUL"} instead of @code{"/dev/null"}.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index ebed63e55abbc4734f7fb883d2fd26d9e7458d20..6663783703f59b61a3844fdd7e1d8d7bc8c7bd67 100644 (file)
@@ -118,6 +118,10 @@ rpl_stat (char const *name, struct stat *buf)
      around length limitations
      <https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file> ?  */
 
+  /* To ease portability.  Like in open.c.  */
+  if (strcmp (name, "/dev/null") == 0)
+    name = "NUL";
+
   /* POSIX <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>
      specifies: "More than two leading <slash> characters shall be treated as
      a single <slash> character."  */
index 7e65ee2f238f8278a85bb2f1d9c30f43c77729d6..2fda69630a6a27b6e074467184a7823708adf8e3 100644 (file)
@@ -52,11 +52,7 @@ main ()
     int fd;
     struct stat statbuf;
 
-#if defined _WIN32 && !defined __CYGWIN__
-    fd = open ("NUL", O_RDWR);
-#else
     fd = open ("/dev/null", O_RDWR);
-#endif
     ASSERT (fstat (fd, &statbuf) == 0);
     close (fd);
     ASSERT (!S_ISREG (statbuf.st_mode));
index bfd5b5526b28ed130fa0179917ff0f78ccbd8d4d..899cb2675555e7c5a547fc2de1bf5ecdd6357002 100644 (file)
@@ -66,11 +66,7 @@ test_lstat_func (int (*func) (char const *, struct stat *), bool print)
 
   /* /dev/null is a character device.
      Except on Solaris, where it is a symlink.  */
-#if defined _WIN32 && !defined __CYGWIN__
-  ASSERT (func ("NUL", &st1) == 0);
-#else
   ASSERT (func ("/dev/null", &st1) == 0);
-#endif
   ASSERT (!S_ISREG (st1.st_mode));
 #if !defined __sun
   ASSERT (S_ISCHR (st1.st_mode));
index 3d2b912e8fef154e54077e6c8908bddcaebcaba9..d055522f39e240f79362122f05464aba43d98efb 100644 (file)
@@ -62,11 +62,7 @@ test_stat_func (int (*func) (char const *, struct stat *), bool print)
   ASSERT (errno == ENOTDIR);
 
   /* /dev/null is a character device.  */
-#if defined _WIN32 && !defined __CYGWIN__
-  ASSERT (func ("NUL", &st1) == 0);
-#else
   ASSERT (func ("/dev/null", &st1) == 0);
-#endif
   ASSERT (!S_ISREG (st1.st_mode));
   ASSERT (S_ISCHR (st1.st_mode));