1 /* A GNU-like <dirent.h>.
2 Copyright (C) 2006-2022 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 /* Get ino_t. Needed on some systems, including glibc 2.8. */
33 #include <sys/types.h>
36 /* Define types DIR and 'struct dirent'. */
37 # if !GNULIB_defined_struct_dirent
43 /* Possible values for 'd_type'. */
45 # define DT_FIFO 1 /* FIFO */
46 # define DT_CHR 2 /* character device */
47 # define DT_DIR 4 /* directory */
48 # define DT_BLK 6 /* block device */
49 # define DT_REG 8 /* regular file */
50 # define DT_LNK 10 /* symbolic link */
51 # define DT_SOCK 12 /* socket */
52 # define DT_WHT 14 /* whiteout */
53 typedef struct gl_directory
DIR;
54 # define GNULIB_defined_struct_dirent 1
58 /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
59 that can be freed by passing them as the Ith argument to the
61 #ifndef _GL_ATTRIBUTE_DEALLOC
63 # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
65 # define _GL_ATTRIBUTE_DEALLOC(f, i)
69 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
71 /* Applies to: functions. */
72 #ifndef _GL_ATTRIBUTE_MALLOC
73 # if __GNUC__ >= 3 || defined __clang__
74 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
76 # define _GL_ATTRIBUTE_MALLOC
80 /* The __attribute__ feature is available in gcc versions 2.5 and later.
81 The attribute __pure__ was added in gcc 2.96. */
82 #ifndef _GL_ATTRIBUTE_PURE
83 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
84 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
86 # define _GL_ATTRIBUTE_PURE /* empty */
90 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
92 /* The definition of _GL_ARG_NONNULL is copied here. */
94 /* The definition of _GL_WARN_ON_USE is copied here. */
97 /* Declare overridden functions. */
100 # if @REPLACE_CLOSEDIR@
101 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
103 # define closedir rpl_closedir
104 # define GNULIB_defined_closedir 1
106 _GL_FUNCDECL_RPL (closedir
, int, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
107 _GL_CXXALIAS_RPL (closedir
, int, (DIR *dirp
));
109 # if !@HAVE_CLOSEDIR@
110 _GL_FUNCDECL_SYS (closedir
, int, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
112 _GL_CXXALIAS_SYS (closedir
, int, (DIR *dirp
));
114 _GL_CXXALIASWARN (closedir
);
115 #elif defined GNULIB_POSIXCHECK
117 # if HAVE_RAW_DECL_CLOSEDIR
118 _GL_WARN_ON_USE (closedir
, "closedir is not portable - "
119 "use gnulib module closedir for portability");
124 # if @REPLACE_OPENDIR@
125 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
127 # define opendir rpl_opendir
128 # define GNULIB_defined_opendir 1
130 _GL_FUNCDECL_RPL (opendir
, DIR *,
131 (const char *dir_name
)
132 _GL_ARG_NONNULL ((1))
133 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
134 _GL_CXXALIAS_RPL (opendir
, DIR *, (const char *dir_name
));
136 # if !@HAVE_OPENDIR@ || __GNUC__ >= 11
137 _GL_FUNCDECL_SYS (opendir
, DIR *,
138 (const char *dir_name
)
139 _GL_ARG_NONNULL ((1))
140 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
142 _GL_CXXALIAS_SYS (opendir
, DIR *, (const char *dir_name
));
144 _GL_CXXALIASWARN (opendir
);
146 # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined opendir
147 /* For -Wmismatched-dealloc: Associate opendir with closedir or
149 _GL_FUNCDECL_SYS (opendir
, DIR *,
150 (const char *dir_name
)
151 _GL_ARG_NONNULL ((1))
152 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
154 # if defined GNULIB_POSIXCHECK
156 # if HAVE_RAW_DECL_OPENDIR
157 _GL_WARN_ON_USE (opendir
, "opendir is not portable - "
158 "use gnulib module opendir for portability");
165 _GL_FUNCDECL_SYS (readdir
, struct dirent
*, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
167 _GL_CXXALIAS_SYS (readdir
, struct dirent
*, (DIR *dirp
));
168 _GL_CXXALIASWARN (readdir
);
169 #elif defined GNULIB_POSIXCHECK
171 # if HAVE_RAW_DECL_READDIR
172 _GL_WARN_ON_USE (readdir
, "readdir is not portable - "
173 "use gnulib module readdir for portability");
177 #if @GNULIB_REWINDDIR@
178 # if !@HAVE_REWINDDIR@
179 _GL_FUNCDECL_SYS (rewinddir
, void, (DIR *dirp
) _GL_ARG_NONNULL ((1)));
181 _GL_CXXALIAS_SYS (rewinddir
, void, (DIR *dirp
));
182 _GL_CXXALIASWARN (rewinddir
);
183 #elif defined GNULIB_POSIXCHECK
185 # if HAVE_RAW_DECL_REWINDDIR
186 _GL_WARN_ON_USE (rewinddir
, "rewinddir is not portable - "
187 "use gnulib module rewinddir for portability");
192 /* Return the file descriptor associated with the given directory stream,
193 or -1 if none exists. */
195 /* On kLIBC, dirfd() is a macro that does not work. Undefine it. */
196 # if !(defined __cplusplus && defined GNULIB_NAMESPACE) || defined dirfd
198 # define dirfd rpl_dirfd
200 _GL_FUNCDECL_RPL (dirfd
, int, (DIR *) _GL_ARG_NONNULL ((1)));
201 _GL_CXXALIAS_RPL (dirfd
, int, (DIR *));
204 /* Gnulib internal hooks needed to maintain the dirfd metadata. */
205 _GL_EXTERN_C
int _gl_register_dirp_fd (int fd
, DIR *dirp
)
206 _GL_ARG_NONNULL ((2));
207 _GL_EXTERN_C
void _gl_unregister_dirp_fd (int fd
);
210 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
211 /* dirfd is defined as a macro and not as a function.
212 Turn it into a function and get rid of the macro. */
213 static inline int (dirfd
) (DIR *dp
) { return dirfd (dp
); }
216 # if !(@HAVE_DECL_DIRFD@ || defined dirfd)
217 _GL_FUNCDECL_SYS (dirfd
, int, (DIR *) _GL_ARG_NONNULL ((1)));
219 _GL_CXXALIAS_SYS (dirfd
, int, (DIR *));
221 _GL_CXXALIASWARN (dirfd
);
222 #elif defined GNULIB_POSIXCHECK
224 # if HAVE_RAW_DECL_DIRFD
225 _GL_WARN_ON_USE (dirfd
, "dirfd is unportable - "
226 "use gnulib module dirfd for portability");
230 #if @GNULIB_FDOPENDIR@
231 /* Open a directory stream visiting the given directory file
232 descriptor. Return NULL and set errno if fd is not visiting a
233 directory. On success, this function consumes fd (it will be
234 implicitly closed either by this function or by a subsequent
236 # if @REPLACE_FDOPENDIR@
237 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
239 # define fdopendir rpl_fdopendir
241 _GL_FUNCDECL_RPL (fdopendir
, DIR *,
243 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
244 _GL_CXXALIAS_RPL (fdopendir
, DIR *, (int fd
));
246 # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ || __GNUC__ >= 11
247 _GL_FUNCDECL_SYS (fdopendir
, DIR *,
249 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
251 _GL_CXXALIAS_SYS (fdopendir
, DIR *, (int fd
));
253 _GL_CXXALIASWARN (fdopendir
);
255 # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined fdopendir
256 /* For -Wmismatched-dealloc: Associate fdopendir with closedir or
258 _GL_FUNCDECL_SYS (fdopendir
, DIR *,
260 _GL_ATTRIBUTE_MALLOC
_GL_ATTRIBUTE_DEALLOC (closedir
, 1));
262 # if defined GNULIB_POSIXCHECK
264 # if HAVE_RAW_DECL_FDOPENDIR
265 _GL_WARN_ON_USE (fdopendir
, "fdopendir is unportable - "
266 "use gnulib module fdopendir for portability");
272 /* Scan the directory DIR, calling FILTER on each directory entry.
273 Entries for which FILTER returns nonzero are individually malloc'd,
274 sorted using qsort with CMP, and collected in a malloc'd array in
275 *NAMELIST. Returns the number of entries selected, or -1 on error. */
277 _GL_FUNCDECL_SYS (scandir
, int,
278 (const char *dir
, struct dirent
***namelist
,
279 int (*filter
) (const struct dirent
*),
280 int (*cmp
) (const struct dirent
**, const struct dirent
**))
281 _GL_ARG_NONNULL ((1, 2, 4)));
283 /* Need to cast, because on glibc systems, the fourth parameter is
284 int (*cmp) (const void *, const void *). */
285 _GL_CXXALIAS_SYS_CAST (scandir
, int,
286 (const char *dir
, struct dirent
***namelist
,
287 int (*filter
) (const struct dirent
*),
288 int (*cmp
) (const struct dirent
**, const struct dirent
**)));
289 _GL_CXXALIASWARN (scandir
);
290 #elif defined GNULIB_POSIXCHECK
292 # if HAVE_RAW_DECL_SCANDIR
293 _GL_WARN_ON_USE (scandir
, "scandir is unportable - "
294 "use gnulib module scandir for portability");
298 #if @GNULIB_ALPHASORT@
299 /* Compare two 'struct dirent' entries alphabetically. */
300 # if !@HAVE_ALPHASORT@
301 _GL_FUNCDECL_SYS (alphasort
, int,
302 (const struct dirent
**, const struct dirent
**)
304 _GL_ARG_NONNULL ((1, 2)));
306 /* Need to cast, because on glibc systems, the parameters are
307 (const void *, const void *). */
308 _GL_CXXALIAS_SYS_CAST (alphasort
, int,
309 (const struct dirent
**, const struct dirent
**));
310 _GL_CXXALIASWARN (alphasort
);
311 #elif defined GNULIB_POSIXCHECK
313 # if HAVE_RAW_DECL_ALPHASORT
314 _GL_WARN_ON_USE (alphasort
, "alphasort is unportable - "
315 "use gnulib module alphasort for portability");
320 #endif /* _@GUARD_PREFIX@_DIRENT_H */
321 #endif /* _@GUARD_PREFIX@_DIRENT_H */