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