1 /* A GNU-like <dirent.h>.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #ifndef _@GUARD_PREFIX@_DIRENT_H
20 @PRAGMA_SYSTEM_HEADER@
24 /* The include_next requires a split double-inclusion guard. */
26 # @INCLUDE_NEXT@ @NEXT_DIRENT_H@
29 #ifndef _@GUARD_PREFIX@_DIRENT_H
30 #define _@GUARD_PREFIX@_DIRENT_H
32 /* This file uses _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_MALLOC,
33 _GL_ATTRIBUTE_PURE, GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */
34 #if !_GL_CONFIG_H_INCLUDED
35 #error "Please include config.h first."
38 /* Get ino_t. Needed on some systems, including glibc 2.8. */
39 #include <sys/types.h>
42 /* Define types DIR and 'struct dirent'. */
43 # if !GNULIB_defined_struct_dirent
49 /* Possible values for 'd_type'. */
51 # define DT_FIFO 1 /* FIFO */
52 # define DT_CHR 2 /* character device */
53 # define DT_DIR 4 /* directory */
54 # define DT_BLK 6 /* block device */
55 # define DT_REG 8 /* regular file */
56 # define DT_LNK 10 /* symbolic link */
57 # define DT_SOCK 12 /* socket */
58 # define DT_WHT 14 /* whiteout */
59 # define GNULIB_defined_struct_dirent 1
63 #if !@DIR_HAS_FD_MEMBER@
64 # if !GNULIB_defined_DIR
65 /* struct gl_directory is a type with a field 'int fd_to_close'.
66 It is needed for implementing fdopendir(). */
69 # define DIR struct gl_directory
71 typedef struct gl_directory
DIR;
73 # define GNULIB_defined_DIR 1
77 /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
78 that can be freed by passing them as the Ith argument to the
80 #ifndef _GL_ATTRIBUTE_DEALLOC
82 # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
84 # define _GL_ATTRIBUTE_DEALLOC(f, i)
88 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
90 /* Applies to: functions. */
91 #ifndef _GL_ATTRIBUTE_MALLOC
92 # if __GNUC__ >= 3 || defined __clang__
93 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
95 # define _GL_ATTRIBUTE_MALLOC
99 /* The __attribute__ feature is available in gcc versions 2.5 and later.
100 The attribute __pure__ was added in gcc 2.96. */
101 #ifndef _GL_ATTRIBUTE_PURE
102 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
103 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
105 # define _GL_ATTRIBUTE_PURE /* empty */
109 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
111 /* The definition of _GL_ARG_NONNULL is copied here. */
113 /* The definition of _GL_WARN_ON_USE is copied here. */
116 /* Declare overridden functions. */
118 #if @GNULIB_CLOSEDIR@
119 # if @REPLACE_CLOSEDIR@
120 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
122 # define closedir rpl_closedir
123 # define GNULIB_defined_closedir 1
125 _GL_FUNCDECL_RPL (closedir
, int, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
126 _GL_CXXALIAS_RPL (closedir
, int, (DIR *dirp
));
128 # if !@HAVE_CLOSEDIR@
129 _GL_FUNCDECL_SYS (closedir
, int, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
131 _GL_CXXALIAS_SYS (closedir
, int, (DIR *dirp
));
133 _GL_CXXALIASWARN (closedir
);
134 #elif defined GNULIB_POSIXCHECK
136 # if HAVE_RAW_DECL_CLOSEDIR
137 _GL_WARN_ON_USE (closedir
, "closedir is not portable - "
138 "use gnulib module closedir for portability");
143 # if @REPLACE_OPENDIR@
144 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
146 # define opendir rpl_opendir
147 # define GNULIB_defined_opendir 1
149 _GL_FUNCDECL_RPL (opendir
, DIR *,
150 (const char *dir_name
)
151 _GL_ARG_NONNULL ((1))
152 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
153 _GL_CXXALIAS_RPL (opendir
, DIR *, (const char *dir_name
));
155 # if !@HAVE_OPENDIR@ || __GNUC__ >= 11
156 _GL_FUNCDECL_SYS (opendir
, DIR *,
157 (const char *dir_name
)
158 _GL_ARG_NONNULL ((1))
159 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
161 _GL_CXXALIAS_SYS (opendir
, DIR *, (const char *dir_name
));
163 _GL_CXXALIASWARN (opendir
);
165 # if @GNULIB_CLOSEDIR@ && !GNULIB_defined_DIR && __GNUC__ >= 11 && !defined opendir
166 /* For -Wmismatched-dealloc: Associate opendir with closedir or
168 _GL_FUNCDECL_SYS (opendir
, DIR *,
169 (const char *dir_name
)
170 _GL_ARG_NONNULL ((1))
171 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
173 # if defined GNULIB_POSIXCHECK
175 # if HAVE_RAW_DECL_OPENDIR
176 _GL_WARN_ON_USE (opendir
, "opendir is not portable - "
177 "use gnulib module opendir for portability");
183 # if @REPLACE_READDIR@
184 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
186 # define readdir rpl_readdir
188 _GL_FUNCDECL_RPL (readdir
, struct dirent
*, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
189 _GL_CXXALIAS_RPL (readdir
, struct dirent
*, (DIR *dirp
));
192 _GL_FUNCDECL_SYS (readdir
, struct dirent
*, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
194 _GL_CXXALIAS_SYS (readdir
, struct dirent
*, (DIR *dirp
));
196 _GL_CXXALIASWARN (readdir
);
197 #elif defined GNULIB_POSIXCHECK
199 # if HAVE_RAW_DECL_READDIR
200 _GL_WARN_ON_USE (readdir
, "readdir is not portable - "
201 "use gnulib module readdir for portability");
205 #if @GNULIB_REWINDDIR@
206 # if @REPLACE_REWINDDIR@
207 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
209 # define rewinddir rpl_rewinddir
211 _GL_FUNCDECL_RPL (rewinddir
, void, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
212 _GL_CXXALIAS_RPL (rewinddir
, void, (DIR *dirp
));
214 # if !@HAVE_REWINDDIR@
215 _GL_FUNCDECL_SYS (rewinddir
, void, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
217 _GL_CXXALIAS_SYS (rewinddir
, void, (DIR *dirp
));
219 _GL_CXXALIASWARN (rewinddir
);
220 #elif defined GNULIB_POSIXCHECK
222 # if HAVE_RAW_DECL_REWINDDIR
223 _GL_WARN_ON_USE (rewinddir
, "rewinddir is not portable - "
224 "use gnulib module rewinddir for portability");
229 /* Return the file descriptor associated with the given directory stream,
230 or -1 if none exists. */
232 /* On kLIBC, dirfd() is a macro that does not work. Undefine it. */
233 # if !(defined __cplusplus && defined GNULIB_NAMESPACE) || defined dirfd
235 # define dirfd rpl_dirfd
237 _GL_FUNCDECL_RPL (dirfd
, int, (DIR *) _GL_ARG_NONNULL ((1)));
238 _GL_CXXALIAS_RPL (dirfd
, int, (DIR *));
241 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
242 /* dirfd is defined as a macro and not as a function.
243 Turn it into a function and get rid of the macro. */
244 static inline int (dirfd
) (DIR *dp
) { return dirfd (dp
); }
247 # if !(@HAVE_DECL_DIRFD@ || defined dirfd)
248 _GL_FUNCDECL_SYS (dirfd
, int, (DIR *) _GL_ARG_NONNULL ((1)));
250 _GL_CXXALIAS_SYS (dirfd
, int, (DIR *));
252 _GL_CXXALIASWARN (dirfd
);
253 #elif defined GNULIB_POSIXCHECK
255 # if HAVE_RAW_DECL_DIRFD
256 _GL_WARN_ON_USE (dirfd
, "dirfd is unportable - "
257 "use gnulib module dirfd for portability");
261 #if @GNULIB_FDOPENDIR@
262 /* Open a directory stream visiting the given directory file
263 descriptor. Return NULL and set errno if fd is not visiting a
264 directory. On success, this function consumes fd (it will be
265 implicitly closed either by this function or by a subsequent
267 # if @REPLACE_FDOPENDIR@
268 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
270 # define fdopendir rpl_fdopendir
272 _GL_FUNCDECL_RPL (fdopendir
, DIR *,
274 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
275 _GL_CXXALIAS_RPL (fdopendir
, DIR *, (int fd
));
277 # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ || __GNUC__ >= 11
278 _GL_FUNCDECL_SYS (fdopendir
, DIR *,
280 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
282 _GL_CXXALIAS_SYS (fdopendir
, DIR *, (int fd
));
284 _GL_CXXALIASWARN (fdopendir
);
286 # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined fdopendir
287 /* For -Wmismatched-dealloc: Associate fdopendir with closedir or
289 _GL_FUNCDECL_SYS (fdopendir
, DIR *,
291 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
293 # if defined GNULIB_POSIXCHECK
295 # if HAVE_RAW_DECL_FDOPENDIR
296 _GL_WARN_ON_USE (fdopendir
, "fdopendir is unportable - "
297 "use gnulib module fdopendir for portability");
303 /* Scan the directory DIR, calling FILTER on each directory entry.
304 Entries for which FILTER returns nonzero are individually malloc'd,
305 sorted using qsort with CMP, and collected in a malloc'd array in
306 *NAMELIST. Returns the number of entries selected, or -1 on error. */
308 _GL_FUNCDECL_SYS (scandir
, int,
309 (const char *dir
, struct dirent
***namelist
,
310 int (*filter
) (const struct dirent
*),
311 int (*cmp
) (const struct dirent
**, const struct dirent
**))
312 _GL_ARG_NONNULL ((1, 2, 4)));
314 /* Need to cast, because on glibc systems, the fourth parameter is
315 int (*cmp) (const void *, const void *). */
316 _GL_CXXALIAS_SYS_CAST (scandir
, int,
317 (const char *dir
, struct dirent
***namelist
,
318 int (*filter
) (const struct dirent
*),
319 int (*cmp
) (const struct dirent
**, const struct dirent
**)));
320 _GL_CXXALIASWARN (scandir
);
321 #elif defined GNULIB_POSIXCHECK
323 # if HAVE_RAW_DECL_SCANDIR
324 _GL_WARN_ON_USE (scandir
, "scandir is unportable - "
325 "use gnulib module scandir for portability");
329 #if @GNULIB_ALPHASORT@
330 /* Compare two 'struct dirent' entries alphabetically. */
331 # if !@HAVE_ALPHASORT@
332 _GL_FUNCDECL_SYS (alphasort
, int,
333 (const struct dirent
**, const struct dirent
**)
335 _GL_ARG_NONNULL ((1, 2)));
337 /* Need to cast, because on glibc systems, the parameters are
338 (const void *, const void *). */
339 _GL_CXXALIAS_SYS_CAST (alphasort
, int,
340 (const struct dirent
**, const struct dirent
**));
341 _GL_CXXALIASWARN (alphasort
);
342 #elif defined GNULIB_POSIXCHECK
344 # if HAVE_RAW_DECL_ALPHASORT
345 _GL_WARN_ON_USE (alphasort
, "alphasort is unportable - "
346 "use gnulib module alphasort for portability");
351 #endif /* _@GUARD_PREFIX@_DIRENT_H */
352 #endif /* _@GUARD_PREFIX@_DIRENT_H */