bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] dirent: define DT_* macros on all platforms


From: Paul Eggert
Subject: [PATCH 1/2] dirent: define DT_* macros on all platforms
Date: Wed, 2 Oct 2024 17:41:43 -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                     | 12 +++++
 doc/posix-headers/dirent.texi | 23 ++++++++-
 lib/dirent.in.h               | 89 +++++++++++++++++++++++++++++++----
 modules/dirent                |  2 +
 4 files changed, 114 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3e23303eaa..fc6d4002ef 100644
--- 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.
diff --git a/doc/posix-headers/dirent.texi b/doc/posix-headers/dirent.texi
index 42dba5f66a..095fc8e9de 100644
--- a/doc/posix-headers/dirent.texi
+++ b/doc/posix-headers/dirent.texi
@@ -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:
diff --git a/lib/dirent.in.h b/lib/dirent.in.h
index 7ba8fc64d8..c8c39c3e6b 100644
--- a/lib/dirent.in.h
+++ b/lib/dirent.in.h
@@ -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'.
diff --git a/modules/dirent b/modules/dirent
index 6f5a63ad66..2d2f6c4e53 100644
--- a/modules/dirent
+++ b/modules/dirent
@@ -8,6 +8,8 @@ m4/unistd_h.m4
 m4/pid_t.m4
 
 Depends-on:
+assert-h
+extensions
 gen-header
 include_next
 snippet/arg-nonnull
-- 
2.43.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]