1 /* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 * POSIX Standard: 5.1.2 Directory Operations <dirent.h>
29 #include <bits/types.h>
32 # ifndef __ino_t_defined
33 # ifndef __USE_FILE_OFFSET64
34 typedef __ino_t ino_t
;
36 typedef __ino64_t ino_t
;
38 # define __ino_t_defined
40 # if defined __USE_LARGEFILE64 && !defined __ino64_t_defined
41 typedef __ino64_t ino64_t
;
42 # define __ino64_t_defined
46 /* This file defines `struct dirent'.
48 It defines the macro `_DIRENT_HAVE_D_NAMLEN' iff there is a `d_namlen'
49 member that gives the length of `d_name'.
51 It defines the macro `_DIRENT_HAVE_D_RECLEN' iff there is a `d_reclen'
52 member that gives the size of the entire directory entry.
54 It defines the macro `_DIRENT_HAVE_D_OFF' iff there is a `d_off'
55 member that gives the file offset of the next directory entry.
57 It defines the macro `_DIRENT_HAVE_D_TYPE' iff there is a `d_type'
58 member that gives the type of the file.
61 #include <bits/dirent.h>
63 #if defined __USE_MISC && !defined d_fileno
64 # define d_ino d_fileno /* Backward compatibility. */
67 /* These macros extract size information from a `struct dirent *'.
68 They may evaluate their argument multiple times, so it must not
69 have side effects. Each of these may involve a relatively costly
70 call to `strlen' on some systems, so these values should be cached.
72 _D_EXACT_NAMLEN (DP) returns the length of DP->d_name, not including
73 its terminating null character.
75 _D_ALLOC_NAMLEN (DP) returns a size at least (_D_EXACT_NAMLEN (DP) + 1);
76 that is, the allocation size needed to hold the DP->d_name string.
77 Use this macro when you don't need the exact length, just an upper bound.
78 This macro is less likely to require calling `strlen' than _D_EXACT_NAMLEN.
81 #ifdef _DIRENT_HAVE_D_NAMLEN
82 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
83 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
85 # define _D_EXACT_NAMLEN(d) (strlen ((d)->d_name))
86 # ifdef _DIRENT_HAVE_D_RECLEN
87 # define _D_ALLOC_NAMLEN(d) (((char *) (d) + (d)->d_reclen) - &(d)->d_name[0])
89 # define _D_ALLOC_NAMLEN(d) (sizeof (d)->d_name > 1 ? sizeof (d)->d_name \
90 : _D_EXACT_NAMLEN (d) + 1)
96 /* File types for `d_type'. */
100 # define DT_UNKNOWN DT_UNKNOWN
102 # define DT_FIFO DT_FIFO
104 # define DT_CHR DT_CHR
106 # define DT_DIR DT_DIR
108 # define DT_BLK DT_BLK
110 # define DT_REG DT_REG
112 # define DT_LNK DT_LNK
114 # define DT_SOCK DT_SOCK
116 # define DT_WHT DT_WHT
119 /* Convert between stat structure types and directory types. */
120 # define IFTODT(mode) (((mode) & 0170000) >> 12)
121 # define DTTOIF(dirtype) ((dirtype) << 12)
125 /* This is the data type of directory stream objects.
126 The actual structure is opaque to users. */
127 typedef struct __dirstream
DIR;
129 /* Close the directory stream DIRP.
130 Return 0 if successful, -1 if not.
132 This function is a possible cancellation point and therefore not
133 marked with __THROW. */
134 extern int closedir (DIR *__dirp
) __nonnull ((1));
136 /* Open a directory stream on NAME.
137 Return a DIR stream on the directory, or NULL if it could not be opened.
139 This function is a possible cancellation point and therefore not
140 marked with __THROW. */
141 extern DIR *opendir (const char *__name
) __nonnull ((1))
142 __attribute_malloc__
__attr_dealloc (closedir
, 1);
144 #ifdef __USE_XOPEN2K8
145 /* Same as opendir, but open the stream on the file descriptor FD.
147 This function is a possible cancellation point and therefore not
148 marked with __THROW. */
149 extern DIR *fdopendir (int __fd
)
150 __attribute_malloc__
__attr_dealloc (closedir
, 1);
153 /* Read a directory entry from DIRP. Return a pointer to a `struct
154 dirent' describing the entry, or NULL for EOF or error. The
155 storage returned may be overwritten by a later readdir call on the
158 If the Large File Support API is selected we have to use the
159 appropriate interface.
161 This function is a possible cancellation point and therefore not
162 marked with __THROW. */
163 #ifndef __USE_FILE_OFFSET64
164 extern struct dirent
*readdir (DIR *__dirp
) __nonnull ((1));
167 extern struct dirent
*__REDIRECT (readdir
, (DIR *__dirp
), readdir64
)
170 # define readdir readdir64
174 #ifdef __USE_LARGEFILE64
175 extern struct dirent64
*readdir64 (DIR *__dirp
) __nonnull ((1));
179 /* Reentrant version of `readdir'. Return in RESULT a pointer to the
182 This function is a possible cancellation point and therefore not
183 marked with __THROW. */
184 # ifndef __USE_FILE_OFFSET64
185 extern int readdir_r (DIR *__restrict __dirp
,
186 struct dirent
*__restrict __entry
,
187 struct dirent
**__restrict __result
)
188 __nonnull ((1, 2, 3)) __attribute_deprecated__
;
191 extern int __REDIRECT (readdir_r
,
192 (DIR *__restrict __dirp
,
193 struct dirent
*__restrict __entry
,
194 struct dirent
**__restrict __result
),
196 __nonnull ((1, 2, 3)) __attribute_deprecated__
;
198 # define readdir_r readdir64_r
202 # ifdef __USE_LARGEFILE64
203 extern int readdir64_r (DIR *__restrict __dirp
,
204 struct dirent64
*__restrict __entry
,
205 struct dirent64
**__restrict __result
)
206 __nonnull ((1, 2, 3)) __attribute_deprecated__
;
208 #endif /* POSIX or misc */
210 /* Rewind DIRP to the beginning of the directory. */
211 extern void rewinddir (DIR *__dirp
) __THROW
__nonnull ((1));
213 #if defined __USE_MISC || defined __USE_XOPEN
214 # include <bits/types.h>
216 /* Seek to position POS on DIRP. */
217 extern void seekdir (DIR *__dirp
, long int __pos
) __THROW
__nonnull ((1));
219 /* Return the current position of DIRP. */
220 extern long int telldir (DIR *__dirp
) __THROW
__nonnull ((1));
223 #ifdef __USE_XOPEN2K8
225 /* Return the file descriptor used by DIRP. */
226 extern int dirfd (DIR *__dirp
) __THROW
__nonnull ((1));
228 # if defined __OPTIMIZE__ && defined _DIR_dirfd
229 # define dirfd(dirp) _DIR_dirfd (dirp)
234 /* Get the definitions of the POSIX.1 limits. */
235 # include <bits/posix1_lim.h>
237 /* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */
239 # define MAXNAMLEN NAME_MAX
241 # define MAXNAMLEN 255
246 # define __need_size_t
249 /* Scan the directory DIR, calling SELECTOR on each directory entry.
250 Entries for which SELECT returns nonzero are individually malloc'd,
251 sorted using qsort with CMP, and collected in a malloc'd array in
252 *NAMELIST. Returns the number of entries selected, or -1 on error.
254 This function is a cancellation point and therefore not marked with
256 # ifndef __USE_FILE_OFFSET64
257 extern int scandir (const char *__restrict __dir
,
258 struct dirent
***__restrict __namelist
,
259 int (*__selector
) (const struct dirent
*),
260 int (*__cmp
) (const struct dirent
**,
261 const struct dirent
**))
265 extern int __REDIRECT (scandir
,
266 (const char *__restrict __dir
,
267 struct dirent
***__restrict __namelist
,
268 int (*__selector
) (const struct dirent
*),
269 int (*__cmp
) (const struct dirent
**,
270 const struct dirent
**)),
271 scandir64
) __nonnull ((1, 2));
273 # define scandir scandir64
277 # if defined __USE_GNU && defined __USE_LARGEFILE64
278 /* This function is like `scandir' but it uses the 64bit dirent structure.
279 Please note that the CMP function must now work with struct dirent64 **. */
280 extern int scandir64 (const char *__restrict __dir
,
281 struct dirent64
***__restrict __namelist
,
282 int (*__selector
) (const struct dirent64
*),
283 int (*__cmp
) (const struct dirent64
**,
284 const struct dirent64
**))
289 /* Similar to `scandir' but a relative DIR name is interpreted relative
290 to the directory for which DFD is a descriptor.
292 This function is a cancellation point and therefore not marked with
294 # ifndef __USE_FILE_OFFSET64
295 extern int scandirat (int __dfd
, const char *__restrict __dir
,
296 struct dirent
***__restrict __namelist
,
297 int (*__selector
) (const struct dirent
*),
298 int (*__cmp
) (const struct dirent
**,
299 const struct dirent
**))
303 extern int __REDIRECT (scandirat
,
304 (int __dfd
, const char *__restrict __dir
,
305 struct dirent
***__restrict __namelist
,
306 int (*__selector
) (const struct dirent
*),
307 int (*__cmp
) (const struct dirent
**,
308 const struct dirent
**)),
309 scandirat64
) __nonnull ((2, 3));
311 # define scandirat scandirat64
315 /* This function is like `scandir' but it uses the 64bit dirent structure.
316 Please note that the CMP function must now work with struct dirent64 **. */
317 extern int scandirat64 (int __dfd
, const char *__restrict __dir
,
318 struct dirent64
***__restrict __namelist
,
319 int (*__selector
) (const struct dirent64
*),
320 int (*__cmp
) (const struct dirent64
**,
321 const struct dirent64
**))
325 /* Function to compare two `struct dirent's alphabetically. */
326 # ifndef __USE_FILE_OFFSET64
327 extern int alphasort (const struct dirent
**__e1
,
328 const struct dirent
**__e2
)
329 __THROW __attribute_pure__
__nonnull ((1, 2));
332 extern int __REDIRECT_NTH (alphasort
,
333 (const struct dirent
**__e1
,
334 const struct dirent
**__e2
),
335 alphasort64
) __attribute_pure__
__nonnull ((1, 2));
337 # define alphasort alphasort64
341 # if defined __USE_GNU && defined __USE_LARGEFILE64
342 extern int alphasort64 (const struct dirent64
**__e1
,
343 const struct dirent64
**__e2
)
344 __THROW __attribute_pure__
__nonnull ((1, 2));
346 #endif /* Use XPG7. */
350 /* Read directory entries from FD into BUF, reading at most NBYTES.
351 Reading starts at offset *BASEP, and *BASEP is updated with the new
352 position after reading. Returns the number of bytes read; zero when at
353 end of directory; or -1 for errors. */
354 # ifndef __USE_FILE_OFFSET64
355 extern __ssize_t
getdirentries (int __fd
, char *__restrict __buf
,
357 __off_t
*__restrict __basep
)
358 __THROW
__nonnull ((2, 4));
361 extern __ssize_t
__REDIRECT_NTH (getdirentries
,
362 (int __fd
, char *__restrict __buf
,
364 __off64_t
*__restrict __basep
),
365 getdirentries64
) __nonnull ((2, 4));
367 # define getdirentries getdirentries64
371 # ifdef __USE_LARGEFILE64
372 extern __ssize_t
getdirentries64 (int __fd
, char *__restrict __buf
,
374 __off64_t
*__restrict __basep
)
375 __THROW
__nonnull ((2, 4));
377 #endif /* Use misc. */
380 /* Function to compare two `struct dirent's by name & version. */
381 # ifndef __USE_FILE_OFFSET64
382 extern int versionsort (const struct dirent
**__e1
,
383 const struct dirent
**__e2
)
384 __THROW __attribute_pure__
__nonnull ((1, 2));
387 extern int __REDIRECT_NTH (versionsort
,
388 (const struct dirent
**__e1
,
389 const struct dirent
**__e2
),
391 __attribute_pure__
__nonnull ((1, 2));
393 # define versionsort versionsort64
397 # ifdef __USE_LARGEFILE64
398 extern int versionsort64 (const struct dirent64
**__e1
,
399 const struct dirent64
**__e2
)
400 __THROW __attribute_pure__
__nonnull ((1, 2));
402 #endif /* Use GNU. */
406 #include <bits/dirent_ext.h>
408 #endif /* dirent.h */