]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
stat, lstat, fstat tests: Enhance tests regarding /dev/null or NUL.
authorBruno Haible <bruno@clisp.org>
Thu, 12 Jun 2025 17:45:19 +0000 (19:45 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 12 Jun 2025 22:05:42 +0000 (00:05 +0200)
* tests/test-fstat.c: Include <fcntl.h>.
(main): Check that fstat reports /dev/null or NUL as a character device.
* tests/test-stat.h (test_stat_func): Likewise for stat.
* tests/test-lstat.h (test_lstat_func): Likewise for lstat.

ChangeLog
tests/test-fstat.c
tests/test-lstat.h
tests/test-stat.h

index cb4853e4b37252fc1abb35d018c3302faf02e5ae..da241662d7d9f8b604fc113adae6e4476c245e85 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-06-12  Bruno Haible  <bruno@clisp.org>
+
+       stat, lstat, fstat tests: Enhance tests regarding /dev/null or NUL.
+       * tests/test-fstat.c: Include <fcntl.h>.
+       (main): Check that fstat reports /dev/null or NUL as a character device.
+       * tests/test-stat.h (test_stat_func): Likewise for stat.
+       * tests/test-lstat.h (test_lstat_func): Likewise for lstat.
+
 2025-06-12  Bruno Haible  <bruno@clisp.org>
 
        stat: Document mingw bug.
index 474e2620771af2a29090390a34c0804b42c1e7f1..7e65ee2f238f8278a85bb2f1d9c30f43c77729d6 100644 (file)
@@ -22,6 +22,7 @@
 SIGNATURE_CHECK (fstat, int, (int, struct stat *));
 
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include "macros.h"
@@ -46,5 +47,21 @@ main ()
     ASSERT (errno == EBADF);
   }
 
+  /* /dev/null is a character device.  */
+  {
+    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));
+    ASSERT (S_ISCHR (statbuf.st_mode));
+  }
+
   return test_exit_status;
 }
index b9846df710c6b5b67290338aff817f6fe24d27d9..fb9a808a76efb01dd2725b8f61920847cb87e54e 100644 (file)
@@ -64,6 +64,15 @@ test_lstat_func (int (*func) (char const *, struct stat *), bool print)
   ASSERT (func (BASE "file/", &st1) == -1);
   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));
+
   /* Now for some symlink tests, where supported.  We set up:
      link1 -> directory
      link2 -> file
index fc0a9d4dd50a28b7fbbd41c34097ece51d29e2c6..3d2b912e8fef154e54077e6c8908bddcaebcaba9 100644 (file)
@@ -61,6 +61,15 @@ test_stat_func (int (*func) (char const *, struct stat *), bool print)
   ASSERT (func (BASE "file/", &st1) == -1);
   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));
+
   /* Now for some symlink tests, where supported.  We set up:
      link1 -> directory
      link2 -> file