]> Savannah Git Hosting - gnulib.git/commitdiff
dirent: define DT_* macros on all platforms
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 2 Oct 2024 22:50:10 +0000 (15:50 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 3 Oct 2024 00:41:28 +0000 (17:41 -0700)
* lib/dirent.in.h (DT_UNKNOWN, DT_FIFO, DT_CHR, DT_DIR, DT_BLK)
(DT_REG, DT_LNK, DT_SOCK, DT_WHT): Define these on all platforms,
if the system does not already define them.  Check that they
have distinct values.
(_GL_DIRENT_S_ISWHT, _GL_DIRENT_S_IFWHT) [!(IFTODT && DTTOIF)]:
New macros.
(IFTODT, DTTOIF): Define if not already defined.
* modules/dirent (Depends-on): Add assert-h, extensions.

ChangeLog
doc/posix-headers/dirent.texi
lib/dirent.in.h
modules/dirent

index 3e23303eaaffc7e1ad33f6303fca2822e551c5b2..fc6d4002efa3fb8bb690ca13c6babdfe9a51ddc9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2024-10-02  Paul Eggert  <eggert@cs.ucla.edu>
+
+       dirent: define DT_* macros on all platforms
+       * lib/dirent.in.h (DT_UNKNOWN, DT_FIFO, DT_CHR, DT_DIR, DT_BLK)
+       (DT_REG, DT_LNK, DT_SOCK, DT_WHT): Define these on all platforms,
+       if the system does not already define them.  Check that they
+       have distinct values.
+       (_GL_DIRENT_S_ISWHT, _GL_DIRENT_S_IFWHT) [!(IFTODT && DTTOIF)]:
+       New macros.
+       (IFTODT, DTTOIF): Define if not already defined.
+       * modules/dirent (Depends-on): Add assert-h, extensions.
+
 2024-10-02  Bruno Haible  <bruno@clisp.org>
 
        Silence -Wunused-const-variable warnings in Gnulib code.
index 42dba5f66a8751215f7ec6b2f9158254ae62abb3..095fc8e9ded0aed762330525659d51f35065ca99 100644 (file)
@@ -11,6 +11,21 @@ Portability problems fixed by Gnulib:
 This header file is missing on some platforms:
 MSVC 14.
 
+@item
+Although many systems define directory entry type macros like @code{DT_DIR},
+some do not:
+Minix 3.1.8, AIX 7.2, HP-UX 11, Solaris 11.4, mingw.
+
+@item
+The directory entry type macro @code{DT_WHT} is missing on many systems:
+All systems where @code{DT_DIR} is missing, plus OpenBSD 7.5.
+
+@item
+The conversion macros @code{DTTOIF} and @code{IFTODT} are missing on
+many systems (however, the Gnulib replacement macros
+may evaluate their arguments multiple times):
+All systems where @code{DT_DIR} is missing, plus OpenBSD 7.5.
+
 @item
 The type @code{ino_t} is missing on some platforms:
 glibc 2.23 and others.
@@ -21,14 +36,18 @@ Portability problems not fixed by Gnulib:
 @itemize
 @item
 Although many systems define a @code{struct dirent} member named
-@code{d_type} and directory entry type macros like @code{DT_DIR} and
-@code{DT_LNK}, some do not:
+@code{d_type}, some do not:
 Minix 3.1.8, AIX 7.2, HP-UX 11, Solaris 11.4, mingw.
 
 @item
 On systems with @code{d_type}, not every filesystem supports
 @code{d_type}, and those lacking support will set it to @code{DT_UNKNOWN}.
 
+@item
+The POSIX.1-2024 directory entry type macros @code{DT_MQ},
+@code{DT_SEM}, @code{DT_SHM} and @code{DT_TMO} are missing on many
+platforms.
+
 @item
 Some systems define a @code{struct dirent} member named @code{d_namlen}
 containing the string length of @code{d_name}, but others do not:
index 7ba8fc64d896504efbb6ea7ef4df53462097c172..c8c39c3e6b853d834b6b6dcbc4ff4bc1bac79a02 100644 (file)
@@ -46,20 +46,89 @@ struct dirent
   char d_type;
   char d_name[1];
 };
-/* Possible values for 'd_type'.  */
-#  define DT_UNKNOWN 0
-#  define DT_FIFO    1          /* FIFO */
-#  define DT_CHR     2          /* character device */
-#  define DT_DIR     4          /* directory */
-#  define DT_BLK     6          /* block device */
-#  define DT_REG     8          /* regular file */
-#  define DT_LNK    10          /* symbolic link */
-#  define DT_SOCK   12          /* socket */
-#  define DT_WHT    14          /* whiteout */
 #  define GNULIB_defined_struct_dirent 1
 # endif
 #endif
 
+/* 'd_type' macros specified in GNU, i.e., POSIX.1-2024 plus DT_WHT,
+   but not (yet) DT_MQ, DT_SEM, DT_SHM, DT_TMO.
+   These macros can be useful even on platforms that do not support
+   d_type or the corresponding file types.
+   Default to the Linux values which are also popular elsewhere,
+   and check that all macros have distinct values.  */
+#ifndef DT_UNKNOWN
+# define DT_UNKNOWN 0
+#endif
+#ifndef DT_FIFO
+# define DT_FIFO  1 /* FIFO */
+#endif
+#ifndef DT_CHR
+# define DT_CHR   2 /* character device */
+#endif
+#ifndef DT_DIR
+# define DT_DIR   4 /* directory */
+#endif
+#ifndef DT_BLK
+# define DT_BLK   6 /* block device */
+#endif
+#ifndef DT_REG
+# define DT_REG   8 /* regular file */
+#endif
+#ifndef DT_LNK
+# define DT_LNK  10 /* symbolic link */
+#endif
+#ifndef DT_SOCK
+# define DT_SOCK 12 /* socket */
+#endif
+#ifndef DT_WHT
+# define DT_WHT  14 /* whiteout */
+#endif
+static_assert (DT_UNKNOWN != DT_FIFO && DT_UNKNOWN != DT_CHR
+               && DT_UNKNOWN != DT_BLK && DT_UNKNOWN != DT_REG
+               && DT_UNKNOWN != DT_LNK && DT_UNKNOWN != DT_SOCK
+               && DT_UNKNOWN != DT_WHT
+               && DT_FIFO != DT_CHR && DT_FIFO != DT_BLK && DT_FIFO != DT_REG
+               && DT_FIFO != DT_LNK && DT_FIFO != DT_SOCK && DT_FIFO != DT_WHT
+               && DT_CHR != DT_BLK && DT_CHR != DT_REG && DT_CHR != DT_LNK
+               && DT_CHR != DT_SOCK && DT_CHR != DT_WHT
+               && DT_BLK != DT_REG && DT_BLK != DT_LNK && DT_BLK != DT_SOCK
+               && DT_BLK != DT_WHT
+               && DT_REG != DT_LNK && DT_REG != DT_SOCK && DT_REG != DT_WHT
+               && DT_LNK != DT_SOCK && DT_LNK != DT_WHT
+               && DT_SOCK != DT_WHT);
+
+/* Conversion between S_IF* and DT_* file types.  */
+#if ! (defined IFTODT && defined DTTOIF)
+# include <sys/stat.h>
+# ifdef S_ISWHT
+#  define _GL_DIRENT_S_ISWHT(mode) S_ISWHT(mode)
+# else
+#  define _GL_DIRENT_S_ISWHT(mode) 0
+# endif
+# ifdef S_IFWHT
+#  define _GL_DIRENT_S_IFWHT S_IFWHT
+# else
+#  define _GL_DIRENT_S_IFWHT (DT_WHT << 12) /* just a guess */
+# endif
+#endif
+#ifndef IFTODT
+# define IFTODT(mode) \
+   (S_ISREG (mode) ? DT_REG : S_ISDIR (mode) ? DT_DIR \
+    : S_ISLNK (mode) ? DT_LNK : S_ISBLK (mode) ? DT_BLK \
+    : S_ISCHR (mode) ? DT_CHR : S_ISFIFO (mode) ? DT_FIFO \
+    : S_ISSOCK (mode) ? DT_SOCK \
+    : _GL_DIRENT_S_ISWHT (mode) ? DT_WHT : DT_UNKNOWN)
+#endif
+#ifndef DTTOIF
+# define DTTOIF(dirtype) \
+   ((dirtype) == DT_REG ? S_IFREG : (dirtype) == DT_DIR ? S_IFDIR \
+    : (dirtype) == DT_LNK ? S_IFLNK : (dirtype) == DT_BLK ? S_IFBLK \
+    : (dirtype) == DT_CHR ? S_IFCHR :  dirtype == DT_FIFO ? S_IFIFO \
+    : (dirtype) == DT_SOCK ? S_IFSOCK \
+    : (dirtype) == DT_WHT ? _GL_DIRENT_S_IFWHT \
+    : (dirtype) << 12 /* just a guess */)
+#endif
+
 #if !@DIR_HAS_FD_MEMBER@
 # if !GNULIB_defined_DIR
 /* struct gl_directory is a type with a field 'int fd_to_close'.
index 6f5a63ad66e878fd976be06d9566a35ab7e48a29..2d2f6c4e5379fe9068150fd5b14f5ed27b7e84db 100644 (file)
@@ -8,6 +8,8 @@ m4/unistd_h.m4
 m4/pid_t.m4
 
 Depends-on:
+assert-h
+extensions
 gen-header
 include_next
 snippet/arg-nonnull