1 /* A GNU-like <dirent.h>.
2    Copyright (C) 2006-2021 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
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/>.  */
16 
17 #ifndef _@GUARD_PREFIX@_DIRENT_H
18 
19 #if __GNUC__ >= 3
20 @PRAGMA_SYSTEM_HEADER@
21 #endif
22 @PRAGMA_COLUMNS@
23 
24 /* The include_next requires a split double-inclusion guard.  */
25 #if @HAVE_DIRENT_H@
26 # @INCLUDE_NEXT@ @NEXT_DIRENT_H@
27 #endif
28 
29 #ifndef _@GUARD_PREFIX@_DIRENT_H
30 #define _@GUARD_PREFIX@_DIRENT_H
31 
32 /* Get ino_t.  Needed on some systems, including glibc 2.8.  */
33 #include <sys/types.h>
34 
35 #if !@HAVE_DIRENT_H@
36 /* Define types DIR and 'struct dirent'.  */
37 # if !GNULIB_defined_struct_dirent
38 struct dirent
39 {
40   char d_type;
41   char d_name[1];
42 };
43 /* Possible values for 'd_type'.  */
44 #  define DT_UNKNOWN 0
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
55 # endif
56 #endif
57 
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
60    function F.  */
61 #ifndef _GL_ATTRIBUTE_DEALLOC
62 # if __GNUC__ >= 11
63 #  define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
64 # else
65 #  define _GL_ATTRIBUTE_DEALLOC(f, i)
66 # endif
67 #endif
68 
69 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
70    allocated memory.  */
71 /* Applies to: functions.  */
72 #ifndef _GL_ATTRIBUTE_MALLOC
73 # if __GNUC__ >= 3 || defined __clang__
74 #  define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
75 # else
76 #  define _GL_ATTRIBUTE_MALLOC
77 # endif
78 #endif
79 
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__))
85 # else
86 #  define _GL_ATTRIBUTE_PURE /* empty */
87 # endif
88 #endif
89 
90 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
91 
92 /* The definition of _GL_ARG_NONNULL is copied here.  */
93 
94 /* The definition of _GL_WARN_ON_USE is copied here.  */
95 
96 
97 /* Declare overridden functions.  */
98 
99 #if @GNULIB_CLOSEDIR@
100 # if @REPLACE_CLOSEDIR@
101 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
102 #   undef closedir
103 #   define closedir rpl_closedir
104 #   define GNULIB_defined_closedir 1
105 #  endif
106 _GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
107 _GL_CXXALIAS_RPL (closedir, int, (DIR *dirp));
108 # else
109 #  if !@HAVE_CLOSEDIR@
110 _GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
111 #  endif
112 _GL_CXXALIAS_SYS (closedir, int, (DIR *dirp));
113 # endif
114 _GL_CXXALIASWARN (closedir);
115 #elif defined GNULIB_POSIXCHECK
116 # undef closedir
117 # if HAVE_RAW_DECL_CLOSEDIR
118 _GL_WARN_ON_USE (closedir, "closedir is not portable - "
119                  "use gnulib module closedir for portability");
120 # endif
121 #endif
122 
123 #if @GNULIB_OPENDIR@
124 # if @REPLACE_OPENDIR@
125 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
126 #   undef opendir
127 #   define opendir rpl_opendir
128 #   define GNULIB_defined_opendir 1
129 #  endif
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));
135 # else
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));
141 #  endif
142 _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name));
143 # endif
144 _GL_CXXALIASWARN (opendir);
145 #else
146 # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined opendir
147 /* For -Wmismatched-dealloc: Associate opendir with closedir or
148    rpl_closedir.  */
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));
153 # endif
154 # if defined GNULIB_POSIXCHECK
155 #  undef opendir
156 #  if HAVE_RAW_DECL_OPENDIR
157 _GL_WARN_ON_USE (opendir, "opendir is not portable - "
158                  "use gnulib module opendir for portability");
159 #  endif
160 # endif
161 #endif
162 
163 #if @GNULIB_READDIR@
164 # if !@HAVE_READDIR@
165 _GL_FUNCDECL_SYS (readdir, struct dirent *, (DIR *dirp) _GL_ARG_NONNULL ((1)));
166 # endif
167 _GL_CXXALIAS_SYS (readdir, struct dirent *, (DIR *dirp));
168 _GL_CXXALIASWARN (readdir);
169 #elif defined GNULIB_POSIXCHECK
170 # undef readdir
171 # if HAVE_RAW_DECL_READDIR
172 _GL_WARN_ON_USE (readdir, "readdir is not portable - "
173                  "use gnulib module readdir for portability");
174 # endif
175 #endif
176 
177 #if @GNULIB_REWINDDIR@
178 # if !@HAVE_REWINDDIR@
179 _GL_FUNCDECL_SYS (rewinddir, void, (DIR *dirp) _GL_ARG_NONNULL ((1)));
180 # endif
181 _GL_CXXALIAS_SYS (rewinddir, void, (DIR *dirp));
182 _GL_CXXALIASWARN (rewinddir);
183 #elif defined GNULIB_POSIXCHECK
184 # undef rewinddir
185 # if HAVE_RAW_DECL_REWINDDIR
186 _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - "
187                  "use gnulib module rewinddir for portability");
188 # endif
189 #endif
190 
191 #if @GNULIB_DIRFD@
192 /* Return the file descriptor associated with the given directory stream,
193    or -1 if none exists.  */
194 # if @REPLACE_DIRFD@
195 /* On kLIBC, dirfd() is a macro that does not work.  Undefine it.  */
196 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE) || defined dirfd
197 #   undef dirfd
198 #   define dirfd rpl_dirfd
199 #  endif
200 _GL_FUNCDECL_RPL (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
201 _GL_CXXALIAS_RPL (dirfd, int, (DIR *));
202 
203 #  ifdef __KLIBC__
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);
208 #  endif
209 # else
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); }
214 #   undef dirfd
215 #  endif
216 #  if !(@HAVE_DECL_DIRFD@ || defined dirfd)
217 _GL_FUNCDECL_SYS (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
218 #  endif
219 _GL_CXXALIAS_SYS (dirfd, int, (DIR *));
220 # endif
221 _GL_CXXALIASWARN (dirfd);
222 #elif defined GNULIB_POSIXCHECK
223 # undef dirfd
224 # if HAVE_RAW_DECL_DIRFD
225 _GL_WARN_ON_USE (dirfd, "dirfd is unportable - "
226                  "use gnulib module dirfd for portability");
227 # endif
228 #endif
229 
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
235    closedir).  */
236 # if @REPLACE_FDOPENDIR@
237 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
238 #   undef fdopendir
239 #   define fdopendir rpl_fdopendir
240 #  endif
241 _GL_FUNCDECL_RPL (fdopendir, DIR *,
242                   (int fd)
243                   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
244 _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd));
245 # else
246 #  if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ || __GNUC__ >= 11
247 _GL_FUNCDECL_SYS (fdopendir, DIR *,
248                   (int fd)
249                   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
250 #  endif
251 _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd));
252 # endif
253 _GL_CXXALIASWARN (fdopendir);
254 #else
255 # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined fdopendir
256 /* For -Wmismatched-dealloc: Associate fdopendir with closedir or
257    rpl_closedir.  */
258 _GL_FUNCDECL_SYS (fdopendir, DIR *,
259                   (int fd)
260                   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
261 # endif
262 # if defined GNULIB_POSIXCHECK
263 #  undef fdopendir
264 #  if HAVE_RAW_DECL_FDOPENDIR
265 _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - "
266                  "use gnulib module fdopendir for portability");
267 #  endif
268 # endif
269 #endif
270 
271 #if @GNULIB_SCANDIR@
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.  */
276 # if !@HAVE_SCANDIR@
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)));
282 # endif
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
291 # undef scandir
292 # if HAVE_RAW_DECL_SCANDIR
293 _GL_WARN_ON_USE (scandir, "scandir is unportable - "
294                  "use gnulib module scandir for portability");
295 # endif
296 #endif
297 
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 **)
303                   _GL_ATTRIBUTE_PURE
304                   _GL_ARG_NONNULL ((1, 2)));
305 # endif
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
312 # undef alphasort
313 # if HAVE_RAW_DECL_ALPHASORT
314 _GL_WARN_ON_USE (alphasort, "alphasort is unportable - "
315                  "use gnulib module alphasort for portability");
316 # endif
317 #endif
318 
319 
320 #endif /* _@GUARD_PREFIX@_DIRENT_H */
321 #endif /* _@GUARD_PREFIX@_DIRENT_H */
322