Debian Codegraph @

lib/dirent.in.h

raw
1 /* A GNU-like <dirent.h>.
2 Copyright (C) 2006-2026 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 /* 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."
36 #endif
37
38 /* Get ino_t. Needed on some systems, including glibc 2.8. */
39 #include <sys/types.h>
40
41 #if !@HAVE_DIRENT_H@
42 /* Define types DIR and 'struct dirent'. */
43 # if !GNULIB_defined_struct_dirent
44 struct dirent
45 {
46 char d_type;
47 char d_name[1];
48 };
49 # define GNULIB_defined_struct_dirent 1
50 # endif
51 #endif
52
53 /* 'd_type' macros specified in GNU, i.e., POSIX.1-2024 plus DT_WHT,
54 but not (yet) DT_MQ, DT_SEM, DT_SHM, DT_TMO.
55 These macros can be useful even on platforms that do not support
56 d_type or the corresponding file types.
57 The values of these macros are all in the 'unsigned char' range.
58 Default to the Linux values which are also popular elsewhere,
59 and check that all macros have distinct values. */
60 #ifndef DT_UNKNOWN
61 # define DT_UNKNOWN 0
62 #endif
63 #ifndef DT_FIFO
64 # define DT_FIFO 1 /* FIFO */
65 #endif
66 #ifndef DT_CHR
67 # define DT_CHR 2 /* character device */
68 #endif
69 #ifndef DT_DIR
70 # define DT_DIR 4 /* directory */
71 #endif
72 #ifndef DT_BLK
73 # define DT_BLK 6 /* block device */
74 #endif
75 #ifndef DT_REG
76 # define DT_REG 8 /* regular file */
77 #endif
78 #ifndef DT_LNK
79 # define DT_LNK 10 /* symbolic link */
80 #endif
81 #ifndef DT_SOCK
82 # define DT_SOCK 12 /* socket */
83 #endif
84 #ifndef DT_WHT
85 # define DT_WHT 14 /* whiteout */
86 #endif
87 static_assert (DT_UNKNOWN != DT_FIFO && DT_UNKNOWN != DT_CHR
88 && DT_UNKNOWN != DT_BLK && DT_UNKNOWN != DT_REG
89 && DT_UNKNOWN != DT_LNK && DT_UNKNOWN != DT_SOCK
90 && DT_UNKNOWN != DT_WHT
91 && DT_FIFO != DT_CHR && DT_FIFO != DT_BLK && DT_FIFO != DT_REG
92 && DT_FIFO != DT_LNK && DT_FIFO != DT_SOCK && DT_FIFO != DT_WHT
93 && DT_CHR != DT_BLK && DT_CHR != DT_REG && DT_CHR != DT_LNK
94 && DT_CHR != DT_SOCK && DT_CHR != DT_WHT
95 && DT_BLK != DT_REG && DT_BLK != DT_LNK && DT_BLK != DT_SOCK
96 && DT_BLK != DT_WHT
97 && DT_REG != DT_LNK && DT_REG != DT_SOCK && DT_REG != DT_WHT
98 && DT_LNK != DT_SOCK && DT_LNK != DT_WHT
99 && DT_SOCK != DT_WHT);
100
101 /* Other optional information about a directory entry. */
102 #define _GL_DT_NOTDIR 0x100 /* Not a directory */
103
104 #if !@DIR_HAS_FD_MEMBER@
105 # if !GNULIB_defined_DIR
106 /* struct gl_directory is a type with a field 'int fd_to_close'.
107 It is needed for implementing fdopendir(). */
108 struct gl_directory;
109 # if @HAVE_DIRENT_H@
110 # define DIR struct gl_directory
111 # else
112 typedef struct gl_directory DIR;
113 # endif
114 # define GNULIB_defined_DIR 1
115 # endif
116 #endif
117
118 /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
119 that can be freed by passing them as the Ith argument to the
120 function F. */
121 #ifndef _GL_ATTRIBUTE_DEALLOC
122 # if __GNUC__ >= 11 && !defined __clang__
123 # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i)))
124 # else
125 # define _GL_ATTRIBUTE_DEALLOC(f, i)
126 # endif
127 #endif
128
129 /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly
130 allocated memory. */
131 /* Applies to: functions. */
132 #ifndef _GL_ATTRIBUTE_MALLOC
133 # if __GNUC__ >= 3 || defined __clang__
134 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
135 # else
136 # define _GL_ATTRIBUTE_MALLOC
137 # endif
138 #endif
139
140 /* The __attribute__ feature is available in gcc versions 2.5 and later.
141 The attribute __pure__ was added in gcc 2.96. */
142 #ifndef _GL_ATTRIBUTE_PURE
143 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
144 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
145 # else
146 # define _GL_ATTRIBUTE_PURE /* empty */
147 # endif
148 #endif
149
150 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
151
152 /* The definition of _GL_ARG_NONNULL is copied here. */
153
154 /* The definition of _GL_WARN_ON_USE is copied here. */
155
156
157 /* Declare overridden functions. */
158
159 #if @GNULIB_CLOSEDIR@
160 # if @REPLACE_CLOSEDIR@
161 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
162 # undef closedir
163 # define closedir rpl_closedir
164 # define GNULIB_defined_closedir 1
165 # endif
166 _GL_FUNCDECL_RPL (closedir, int, (DIR *dirp), _GL_ARG_NONNULL ((1)));
167 _GL_CXXALIAS_RPL (closedir, int, (DIR *dirp));
168 # else
169 # if !@HAVE_CLOSEDIR@
170 _GL_FUNCDECL_SYS (closedir, int, (DIR *dirp), _GL_ARG_NONNULL ((1)));
171 # endif
172 _GL_CXXALIAS_SYS (closedir, int, (DIR *dirp));
173 # endif
174 _GL_CXXALIASWARN (closedir);
175 #elif defined GNULIB_POSIXCHECK
176 # if HAVE_RAW_DECL_CLOSEDIR
177 _GL_WARN_ON_USE (closedir, "closedir is not portable - "
178 "use gnulib module closedir for portability");
179 # endif
180 #endif
181
182 #if @GNULIB_OPENDIR@
183 # if @REPLACE_OPENDIR@
184 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
185 # undef opendir
186 # define opendir rpl_opendir
187 # define GNULIB_defined_opendir 1
188 # endif
189 _GL_FUNCDECL_RPL (opendir, DIR *,
190 (const char *dir_name),
191 _GL_ARG_NONNULL ((1))
192 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
193 _GL_CXXALIAS_RPL (opendir, DIR *, (const char *dir_name));
194 # else
195 # if !@HAVE_OPENDIR@ || (__GNUC__ >= 11 && !defined __clang__)
196 _GL_FUNCDECL_SYS (opendir, DIR *,
197 (const char *dir_name),
198 _GL_ARG_NONNULL ((1))
199 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
200 # endif
201 _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name));
202 # endif
203 _GL_CXXALIASWARN (opendir);
204 #else
205 # if @GNULIB_CLOSEDIR@ && !GNULIB_defined_DIR \
206 && (__GNUC__ >= 11 && !defined __clang__) && !defined opendir
207 /* For -Wmismatched-dealloc: Associate opendir with closedir or
208 rpl_closedir. */
209 _GL_FUNCDECL_SYS (opendir, DIR *,
210 (const char *dir_name),
211 _GL_ARG_NONNULL ((1))
212 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
213 # endif
214 # if defined GNULIB_POSIXCHECK
215 # if HAVE_RAW_DECL_OPENDIR
216 _GL_WARN_ON_USE (opendir, "opendir is not portable - "
217 "use gnulib module opendir for portability");
218 # endif
219 # endif
220 #endif
221
222 #if @GNULIB_READDIR@
223 # if @REPLACE_READDIR@
224 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
225 # undef readdir
226 # define readdir rpl_readdir
227 # endif
228 _GL_FUNCDECL_RPL (readdir, struct dirent *, (DIR *dirp), _GL_ARG_NONNULL ((1)));
229 _GL_CXXALIAS_RPL (readdir, struct dirent *, (DIR *dirp));
230 # else
231 # if !@HAVE_READDIR@
232 _GL_FUNCDECL_SYS (readdir, struct dirent *, (DIR *dirp), _GL_ARG_NONNULL ((1)));
233 # endif
234 _GL_CXXALIAS_SYS (readdir, struct dirent *, (DIR *dirp));
235 # endif
236 _GL_CXXALIASWARN (readdir);
237 #elif defined GNULIB_POSIXCHECK
238 # if HAVE_RAW_DECL_READDIR
239 _GL_WARN_ON_USE (readdir, "readdir is not portable - "
240 "use gnulib module readdir for portability");
241 # endif
242 #endif
243
244 #if @GNULIB_REWINDDIR@
245 # if @REPLACE_REWINDDIR@
246 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
247 # undef rewinddir
248 # define rewinddir rpl_rewinddir
249 # endif
250 _GL_FUNCDECL_RPL (rewinddir, void, (DIR *dirp), _GL_ARG_NONNULL ((1)));
251 _GL_CXXALIAS_RPL (rewinddir, void, (DIR *dirp));
252 # else
253 # if !@HAVE_REWINDDIR@
254 _GL_FUNCDECL_SYS (rewinddir, void, (DIR *dirp), _GL_ARG_NONNULL ((1)));
255 # endif
256 _GL_CXXALIAS_SYS (rewinddir, void, (DIR *dirp));
257 # endif
258 _GL_CXXALIASWARN (rewinddir);
259 #elif defined GNULIB_POSIXCHECK
260 # if HAVE_RAW_DECL_REWINDDIR
261 _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - "
262 "use gnulib module rewinddir for portability");
263 # endif
264 #endif
265
266 #if @GNULIB_DIRFD@
267 /* Return the file descriptor associated with the given directory stream,
268 or -1 if none exists. */
269 # if @REPLACE_DIRFD@
270 /* On kLIBC, dirfd() is a macro that does not work. Undefine it. */
271 # if !(defined __cplusplus && defined GNULIB_NAMESPACE) || defined dirfd
272 # undef dirfd
273 # define dirfd rpl_dirfd
274 # endif
275 _GL_FUNCDECL_RPL (dirfd, int, (DIR *), _GL_ARG_NONNULL ((1)));
276 _GL_CXXALIAS_RPL (dirfd, int, (DIR *));
277
278 # else
279 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
280 /* dirfd is defined as a macro and not as a function.
281 Turn it into a function and get rid of the macro. */
282 static inline int (dirfd) (DIR *dp) { return dirfd (dp); }
283 # undef dirfd
284 # endif
285 # if !(@HAVE_DECL_DIRFD@ || defined dirfd)
286 _GL_FUNCDECL_SYS (dirfd, int, (DIR *), _GL_ARG_NONNULL ((1)));
287 # endif
288 _GL_CXXALIAS_SYS (dirfd, int, (DIR *));
289 # endif
290 _GL_CXXALIASWARN (dirfd);
291 #elif defined GNULIB_POSIXCHECK
292 # if HAVE_RAW_DECL_DIRFD
293 _GL_WARN_ON_USE (dirfd, "dirfd is unportable - "
294 "use gnulib module dirfd for portability");
295 # endif
296 #endif
297
298 #if @GNULIB_FDOPENDIR@
299 /* Open a directory stream visiting the given directory file
300 descriptor. Return NULL and set errno if fd is not visiting a
301 directory. On success, this function consumes fd (it will be
302 implicitly closed either by this function or by a subsequent
303 closedir). */
304 # if @REPLACE_FDOPENDIR@
305 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
306 # undef fdopendir
307 # define fdopendir rpl_fdopendir
308 # endif
309 _GL_FUNCDECL_RPL (fdopendir, DIR *,
310 (int fd),
311 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
312 _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd));
313 # else
314 # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ \
315 || (__GNUC__ >= 11 && !defined __clang__)
316 _GL_FUNCDECL_SYS (fdopendir, DIR *,
317 (int fd),
318 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
319 # endif
320 _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd));
321 # endif
322 _GL_CXXALIASWARN (fdopendir);
323 #else
324 # if @GNULIB_CLOSEDIR@ \
325 && (__GNUC__ >= 11 && !defined __clang__) && !defined fdopendir
326 /* For -Wmismatched-dealloc: Associate fdopendir with closedir or
327 rpl_closedir. */
328 _GL_FUNCDECL_SYS (fdopendir, DIR *,
329 (int fd),
330 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1));
331 # endif
332 # if defined GNULIB_POSIXCHECK
333 # if HAVE_RAW_DECL_FDOPENDIR
334 _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - "
335 "use gnulib module fdopendir for portability");
336 # endif
337 # endif
338 #endif
339
340 #if @GNULIB_SCANDIR@
341 /* Scan the directory DIR, calling FILTER on each directory entry.
342 Entries for which FILTER returns nonzero are individually malloc'd,
343 sorted using qsort with CMP, and collected in a malloc'd array in
344 *NAMELIST. Returns the number of entries selected, or -1 on error. */
345 # if !@HAVE_SCANDIR@
346 _GL_FUNCDECL_SYS (scandir, int,
347 (const char *dir, struct dirent ***namelist,
348 int (*filter) (const struct dirent *),
349 int (*cmp) (const struct dirent **, const struct dirent **)),
350 _GL_ARG_NONNULL ((1, 2, 4)));
351 # endif
352 /* Need to cast, because on glibc systems, the fourth parameter is
353 int (*cmp) (const void *, const void *). */
354 _GL_CXXALIAS_SYS_CAST (scandir, int,
355 (const char *dir, struct dirent ***namelist,
356 int (*filter) (const struct dirent *),
357 int (*cmp) (const struct dirent **, const struct dirent **)));
358 _GL_CXXALIASWARN (scandir);
359 #elif defined GNULIB_POSIXCHECK
360 # if HAVE_RAW_DECL_SCANDIR
361 _GL_WARN_ON_USE (scandir, "scandir is unportable - "
362 "use gnulib module scandir for portability");
363 # endif
364 #endif
365
366 #if @GNULIB_ALPHASORT@
367 /* Compare two 'struct dirent' entries alphabetically. */
368 # if !@HAVE_ALPHASORT@
369 _GL_FUNCDECL_SYS (alphasort, int,
370 (const struct dirent **, const struct dirent **),
371 _GL_ATTRIBUTE_PURE
372 _GL_ARG_NONNULL ((1, 2)));
373 # endif
374 /* Need to cast, because on glibc systems, the parameters are
375 (const void *, const void *). */
376 _GL_CXXALIAS_SYS_CAST (alphasort, int,
377 (const struct dirent **, const struct dirent **));
378 _GL_CXXALIASWARN (alphasort);
379 #elif defined GNULIB_POSIXCHECK
380 # if HAVE_RAW_DECL_ALPHASORT
381 _GL_WARN_ON_USE (alphasort, "alphasort is unportable - "
382 "use gnulib module alphasort for portability");
383 # endif
384 #endif
385
386
387 /* Includes that provide only macros that don't need to be overridden.
388 (Includes that are needed for type definitions and function declarations
389 have their place above, before the function overrides.) */
390
391 /* Conversion between S_IF* and DT_* file types. */
392 #if ! (defined IFTODT && defined DTTOIF)
393 # include <sys/stat.h>
394 # ifdef S_ISWHT
395 # define _GL_DIRENT_S_ISWHT(mode) S_ISWHT(mode)
396 # else
397 # define _GL_DIRENT_S_ISWHT(mode) 0
398 # endif
399 # ifdef S_IFWHT
400 # define _GL_DIRENT_S_IFWHT S_IFWHT
401 # else
402 # define _GL_DIRENT_S_IFWHT (DT_WHT << 12) /* just a guess */
403 # endif
404 #endif
405 /* Conversion from a 'stat' mode to a DT_* value. */
406 #ifndef IFTODT
407 # define IFTODT(mode) \
408 (S_ISREG (mode) ? DT_REG : S_ISDIR (mode) ? DT_DIR \
409 : S_ISLNK (mode) ? DT_LNK : S_ISBLK (mode) ? DT_BLK \
410 : S_ISCHR (mode) ? DT_CHR : S_ISFIFO (mode) ? DT_FIFO \
411 : S_ISSOCK (mode) ? DT_SOCK \
412 : _GL_DIRENT_S_ISWHT (mode) ? DT_WHT : DT_UNKNOWN)
413 #endif
414 /* Conversion from a DT_* value to a 'stat' mode. */
415 #ifndef DTTOIF
416 # define DTTOIF(dirtype) \
417 ((dirtype) == DT_REG ? S_IFREG : (dirtype) == DT_DIR ? S_IFDIR \
418 : (dirtype) == DT_LNK ? S_IFLNK : (dirtype) == DT_BLK ? S_IFBLK \
419 : (dirtype) == DT_CHR ? S_IFCHR : dirtype == DT_FIFO ? S_IFIFO \
420 : (dirtype) == DT_SOCK ? S_IFSOCK \
421 : (dirtype) == DT_WHT ? _GL_DIRENT_S_IFWHT \
422 : (dirtype) << 12 /* just a guess */)
423 #endif
424
425
426 #endif /* _@GUARD_PREFIX@_DIRENT_H */
427 #endif /* _@GUARD_PREFIX@_DIRENT_H */