1*1424dfb3Schristos /* A GNU-like <dirent.h>.
2*1424dfb3Schristos    Copyright (C) 2006-2020 Free Software Foundation, Inc.
3*1424dfb3Schristos 
4*1424dfb3Schristos    This program is free software: you can redistribute it and/or modify
5*1424dfb3Schristos    it under the terms of the GNU General Public License as published by
6*1424dfb3Schristos    the Free Software Foundation; either version 3 of the License, or
7*1424dfb3Schristos    (at your option) any later version.
8*1424dfb3Schristos 
9*1424dfb3Schristos    This program is distributed in the hope that it will be useful,
10*1424dfb3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1424dfb3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1424dfb3Schristos    GNU General Public License for more details.
13*1424dfb3Schristos 
14*1424dfb3Schristos    You should have received a copy of the GNU General Public License
15*1424dfb3Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16*1424dfb3Schristos 
17*1424dfb3Schristos #ifndef _@GUARD_PREFIX@_DIRENT_H
18*1424dfb3Schristos 
19*1424dfb3Schristos #if __GNUC__ >= 3
20*1424dfb3Schristos @PRAGMA_SYSTEM_HEADER@
21*1424dfb3Schristos #endif
22*1424dfb3Schristos @PRAGMA_COLUMNS@
23*1424dfb3Schristos 
24*1424dfb3Schristos /* The include_next requires a split double-inclusion guard.  */
25*1424dfb3Schristos #if @HAVE_DIRENT_H@
26*1424dfb3Schristos # @INCLUDE_NEXT@ @NEXT_DIRENT_H@
27*1424dfb3Schristos #endif
28*1424dfb3Schristos 
29*1424dfb3Schristos #ifndef _@GUARD_PREFIX@_DIRENT_H
30*1424dfb3Schristos #define _@GUARD_PREFIX@_DIRENT_H
31*1424dfb3Schristos 
32*1424dfb3Schristos /* Get ino_t.  Needed on some systems, including glibc 2.8.  */
33*1424dfb3Schristos #include <sys/types.h>
34*1424dfb3Schristos 
35*1424dfb3Schristos #if !@HAVE_DIRENT_H@
36*1424dfb3Schristos /* Define types DIR and 'struct dirent'.  */
37*1424dfb3Schristos # if !GNULIB_defined_struct_dirent
38*1424dfb3Schristos struct dirent
39*1424dfb3Schristos {
40*1424dfb3Schristos   char d_type;
41*1424dfb3Schristos   char d_name[1];
42*1424dfb3Schristos };
43*1424dfb3Schristos /* Possible values for 'd_type'.  */
44*1424dfb3Schristos #  define DT_UNKNOWN 0
45*1424dfb3Schristos #  define DT_FIFO    1          /* FIFO */
46*1424dfb3Schristos #  define DT_CHR     2          /* character device */
47*1424dfb3Schristos #  define DT_DIR     4          /* directory */
48*1424dfb3Schristos #  define DT_BLK     6          /* block device */
49*1424dfb3Schristos #  define DT_REG     8          /* regular file */
50*1424dfb3Schristos #  define DT_LNK    10          /* symbolic link */
51*1424dfb3Schristos #  define DT_SOCK   12          /* socket */
52*1424dfb3Schristos #  define DT_WHT    14          /* whiteout */
53*1424dfb3Schristos typedef struct gl_directory DIR;
54*1424dfb3Schristos #  define GNULIB_defined_struct_dirent 1
55*1424dfb3Schristos # endif
56*1424dfb3Schristos #endif
57*1424dfb3Schristos 
58*1424dfb3Schristos /* The __attribute__ feature is available in gcc versions 2.5 and later.
59*1424dfb3Schristos    The attribute __pure__ was added in gcc 2.96.  */
60*1424dfb3Schristos #ifndef _GL_ATTRIBUTE_PURE
61*1424dfb3Schristos # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
62*1424dfb3Schristos #  define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
63*1424dfb3Schristos # else
64*1424dfb3Schristos #  define _GL_ATTRIBUTE_PURE /* empty */
65*1424dfb3Schristos # endif
66*1424dfb3Schristos #endif
67*1424dfb3Schristos 
68*1424dfb3Schristos /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
69*1424dfb3Schristos 
70*1424dfb3Schristos /* The definition of _GL_ARG_NONNULL is copied here.  */
71*1424dfb3Schristos 
72*1424dfb3Schristos /* The definition of _GL_WARN_ON_USE is copied here.  */
73*1424dfb3Schristos 
74*1424dfb3Schristos 
75*1424dfb3Schristos /* Declare overridden functions.  */
76*1424dfb3Schristos 
77*1424dfb3Schristos #if @GNULIB_OPENDIR@
78*1424dfb3Schristos # if @REPLACE_OPENDIR@
79*1424dfb3Schristos #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
80*1424dfb3Schristos #   undef opendir
81*1424dfb3Schristos #   define opendir rpl_opendir
82*1424dfb3Schristos #   define GNULIB_defined_opendir 1
83*1424dfb3Schristos #  endif
84*1424dfb3Schristos _GL_FUNCDECL_RPL (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1)));
85*1424dfb3Schristos _GL_CXXALIAS_RPL (opendir, DIR *, (const char *dir_name));
86*1424dfb3Schristos # else
87*1424dfb3Schristos #  if !@HAVE_OPENDIR@
88*1424dfb3Schristos _GL_FUNCDECL_SYS (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1)));
89*1424dfb3Schristos #  endif
90*1424dfb3Schristos _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name));
91*1424dfb3Schristos # endif
92*1424dfb3Schristos _GL_CXXALIASWARN (opendir);
93*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
94*1424dfb3Schristos # undef opendir
95*1424dfb3Schristos # if HAVE_RAW_DECL_OPENDIR
96*1424dfb3Schristos _GL_WARN_ON_USE (opendir, "opendir is not portable - "
97*1424dfb3Schristos                  "use gnulib module opendir for portability");
98*1424dfb3Schristos # endif
99*1424dfb3Schristos #endif
100*1424dfb3Schristos 
101*1424dfb3Schristos #if @GNULIB_READDIR@
102*1424dfb3Schristos # if !@HAVE_READDIR@
103*1424dfb3Schristos _GL_FUNCDECL_SYS (readdir, struct dirent *, (DIR *dirp) _GL_ARG_NONNULL ((1)));
104*1424dfb3Schristos # endif
105*1424dfb3Schristos _GL_CXXALIAS_SYS (readdir, struct dirent *, (DIR *dirp));
106*1424dfb3Schristos _GL_CXXALIASWARN (readdir);
107*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
108*1424dfb3Schristos # undef readdir
109*1424dfb3Schristos # if HAVE_RAW_DECL_READDIR
110*1424dfb3Schristos _GL_WARN_ON_USE (readdir, "readdir is not portable - "
111*1424dfb3Schristos                  "use gnulib module readdir for portability");
112*1424dfb3Schristos # endif
113*1424dfb3Schristos #endif
114*1424dfb3Schristos 
115*1424dfb3Schristos #if @GNULIB_REWINDDIR@
116*1424dfb3Schristos # if !@HAVE_REWINDDIR@
117*1424dfb3Schristos _GL_FUNCDECL_SYS (rewinddir, void, (DIR *dirp) _GL_ARG_NONNULL ((1)));
118*1424dfb3Schristos # endif
119*1424dfb3Schristos _GL_CXXALIAS_SYS (rewinddir, void, (DIR *dirp));
120*1424dfb3Schristos _GL_CXXALIASWARN (rewinddir);
121*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
122*1424dfb3Schristos # undef rewinddir
123*1424dfb3Schristos # if HAVE_RAW_DECL_REWINDDIR
124*1424dfb3Schristos _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - "
125*1424dfb3Schristos                  "use gnulib module rewinddir for portability");
126*1424dfb3Schristos # endif
127*1424dfb3Schristos #endif
128*1424dfb3Schristos 
129*1424dfb3Schristos #if @GNULIB_CLOSEDIR@
130*1424dfb3Schristos # if @REPLACE_CLOSEDIR@
131*1424dfb3Schristos #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
132*1424dfb3Schristos #   undef closedir
133*1424dfb3Schristos #   define closedir rpl_closedir
134*1424dfb3Schristos #   define GNULIB_defined_closedir 1
135*1424dfb3Schristos #  endif
136*1424dfb3Schristos _GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
137*1424dfb3Schristos _GL_CXXALIAS_RPL (closedir, int, (DIR *dirp));
138*1424dfb3Schristos # else
139*1424dfb3Schristos #  if !@HAVE_CLOSEDIR@
140*1424dfb3Schristos _GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
141*1424dfb3Schristos #  endif
142*1424dfb3Schristos _GL_CXXALIAS_SYS (closedir, int, (DIR *dirp));
143*1424dfb3Schristos # endif
144*1424dfb3Schristos _GL_CXXALIASWARN (closedir);
145*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
146*1424dfb3Schristos # undef closedir
147*1424dfb3Schristos # if HAVE_RAW_DECL_CLOSEDIR
148*1424dfb3Schristos _GL_WARN_ON_USE (closedir, "closedir is not portable - "
149*1424dfb3Schristos                  "use gnulib module closedir for portability");
150*1424dfb3Schristos # endif
151*1424dfb3Schristos #endif
152*1424dfb3Schristos 
153*1424dfb3Schristos #if @GNULIB_DIRFD@
154*1424dfb3Schristos /* Return the file descriptor associated with the given directory stream,
155*1424dfb3Schristos    or -1 if none exists.  */
156*1424dfb3Schristos # if @REPLACE_DIRFD@
157*1424dfb3Schristos #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
158*1424dfb3Schristos #   undef dirfd
159*1424dfb3Schristos #   define dirfd rpl_dirfd
160*1424dfb3Schristos #  endif
161*1424dfb3Schristos _GL_FUNCDECL_RPL (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
162*1424dfb3Schristos _GL_CXXALIAS_RPL (dirfd, int, (DIR *));
163*1424dfb3Schristos 
164*1424dfb3Schristos #  ifdef __KLIBC__
165*1424dfb3Schristos /* Gnulib internal hooks needed to maintain the dirfd metadata.  */
166*1424dfb3Schristos _GL_EXTERN_C int _gl_register_dirp_fd (int fd, DIR *dirp)
167*1424dfb3Schristos      _GL_ARG_NONNULL ((2));
168*1424dfb3Schristos _GL_EXTERN_C void _gl_unregister_dirp_fd (int fd);
169*1424dfb3Schristos #  endif
170*1424dfb3Schristos # else
171*1424dfb3Schristos #  if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
172*1424dfb3Schristos     /* dirfd is defined as a macro and not as a function.
173*1424dfb3Schristos        Turn it into a function and get rid of the macro.  */
174*1424dfb3Schristos static inline int (dirfd) (DIR *dp) { return dirfd (dp); }
175*1424dfb3Schristos #   undef dirfd
176*1424dfb3Schristos #  endif
177*1424dfb3Schristos #  if !(@HAVE_DECL_DIRFD@ || defined dirfd)
178*1424dfb3Schristos _GL_FUNCDECL_SYS (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
179*1424dfb3Schristos #  endif
180*1424dfb3Schristos _GL_CXXALIAS_SYS (dirfd, int, (DIR *));
181*1424dfb3Schristos # endif
182*1424dfb3Schristos _GL_CXXALIASWARN (dirfd);
183*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
184*1424dfb3Schristos # undef dirfd
185*1424dfb3Schristos # if HAVE_RAW_DECL_DIRFD
186*1424dfb3Schristos _GL_WARN_ON_USE (dirfd, "dirfd is unportable - "
187*1424dfb3Schristos                  "use gnulib module dirfd for portability");
188*1424dfb3Schristos # endif
189*1424dfb3Schristos #endif
190*1424dfb3Schristos 
191*1424dfb3Schristos #if @GNULIB_FDOPENDIR@
192*1424dfb3Schristos /* Open a directory stream visiting the given directory file
193*1424dfb3Schristos    descriptor.  Return NULL and set errno if fd is not visiting a
194*1424dfb3Schristos    directory.  On success, this function consumes fd (it will be
195*1424dfb3Schristos    implicitly closed either by this function or by a subsequent
196*1424dfb3Schristos    closedir).  */
197*1424dfb3Schristos # if @REPLACE_FDOPENDIR@
198*1424dfb3Schristos #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
199*1424dfb3Schristos #   undef fdopendir
200*1424dfb3Schristos #   define fdopendir rpl_fdopendir
201*1424dfb3Schristos #  endif
202*1424dfb3Schristos _GL_FUNCDECL_RPL (fdopendir, DIR *, (int fd));
203*1424dfb3Schristos _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd));
204*1424dfb3Schristos # else
205*1424dfb3Schristos #  if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@
206*1424dfb3Schristos _GL_FUNCDECL_SYS (fdopendir, DIR *, (int fd));
207*1424dfb3Schristos #  endif
208*1424dfb3Schristos _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd));
209*1424dfb3Schristos # endif
210*1424dfb3Schristos _GL_CXXALIASWARN (fdopendir);
211*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
212*1424dfb3Schristos # undef fdopendir
213*1424dfb3Schristos # if HAVE_RAW_DECL_FDOPENDIR
214*1424dfb3Schristos _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - "
215*1424dfb3Schristos                  "use gnulib module fdopendir for portability");
216*1424dfb3Schristos # endif
217*1424dfb3Schristos #endif
218*1424dfb3Schristos 
219*1424dfb3Schristos #if @GNULIB_SCANDIR@
220*1424dfb3Schristos /* Scan the directory DIR, calling FILTER on each directory entry.
221*1424dfb3Schristos    Entries for which FILTER returns nonzero are individually malloc'd,
222*1424dfb3Schristos    sorted using qsort with CMP, and collected in a malloc'd array in
223*1424dfb3Schristos    *NAMELIST.  Returns the number of entries selected, or -1 on error.  */
224*1424dfb3Schristos # if !@HAVE_SCANDIR@
225*1424dfb3Schristos _GL_FUNCDECL_SYS (scandir, int,
226*1424dfb3Schristos                   (const char *dir, struct dirent ***namelist,
227*1424dfb3Schristos                    int (*filter) (const struct dirent *),
228*1424dfb3Schristos                    int (*cmp) (const struct dirent **, const struct dirent **))
229*1424dfb3Schristos                   _GL_ARG_NONNULL ((1, 2, 4)));
230*1424dfb3Schristos # endif
231*1424dfb3Schristos /* Need to cast, because on glibc systems, the fourth parameter is
232*1424dfb3Schristos                         int (*cmp) (const void *, const void *).  */
233*1424dfb3Schristos _GL_CXXALIAS_SYS_CAST (scandir, int,
234*1424dfb3Schristos                        (const char *dir, struct dirent ***namelist,
235*1424dfb3Schristos                         int (*filter) (const struct dirent *),
236*1424dfb3Schristos                         int (*cmp) (const struct dirent **, const struct dirent **)));
237*1424dfb3Schristos _GL_CXXALIASWARN (scandir);
238*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
239*1424dfb3Schristos # undef scandir
240*1424dfb3Schristos # if HAVE_RAW_DECL_SCANDIR
241*1424dfb3Schristos _GL_WARN_ON_USE (scandir, "scandir is unportable - "
242*1424dfb3Schristos                  "use gnulib module scandir for portability");
243*1424dfb3Schristos # endif
244*1424dfb3Schristos #endif
245*1424dfb3Schristos 
246*1424dfb3Schristos #if @GNULIB_ALPHASORT@
247*1424dfb3Schristos /* Compare two 'struct dirent' entries alphabetically.  */
248*1424dfb3Schristos # if !@HAVE_ALPHASORT@
249*1424dfb3Schristos _GL_FUNCDECL_SYS (alphasort, int,
250*1424dfb3Schristos                   (const struct dirent **, const struct dirent **)
251*1424dfb3Schristos                   _GL_ATTRIBUTE_PURE
252*1424dfb3Schristos                   _GL_ARG_NONNULL ((1, 2)));
253*1424dfb3Schristos # endif
254*1424dfb3Schristos /* Need to cast, because on glibc systems, the parameters are
255*1424dfb3Schristos                        (const void *, const void *).  */
256*1424dfb3Schristos _GL_CXXALIAS_SYS_CAST (alphasort, int,
257*1424dfb3Schristos                        (const struct dirent **, const struct dirent **));
258*1424dfb3Schristos _GL_CXXALIASWARN (alphasort);
259*1424dfb3Schristos #elif defined GNULIB_POSIXCHECK
260*1424dfb3Schristos # undef alphasort
261*1424dfb3Schristos # if HAVE_RAW_DECL_ALPHASORT
262*1424dfb3Schristos _GL_WARN_ON_USE (alphasort, "alphasort is unportable - "
263*1424dfb3Schristos                  "use gnulib module alphasort for portability");
264*1424dfb3Schristos # endif
265*1424dfb3Schristos #endif
266*1424dfb3Schristos 
267*1424dfb3Schristos 
268*1424dfb3Schristos #endif /* _@GUARD_PREFIX@_DIRENT_H */
269*1424dfb3Schristos #endif /* _@GUARD_PREFIX@_DIRENT_H */
270