dirent.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * POSIX Standard: 5.1.2 Directory Operations <dirent.h>
  16. */
  17. #ifndef _DIRENT_H
  18. #define _DIRENT_H 1
  19. #include <features.h>
  20. __BEGIN_DECLS
  21. #include <bits/types.h>
  22. #ifdef __USE_XOPEN
  23. # ifndef __ino_t_defined
  24. # ifndef __USE_FILE_OFFSET64
  25. typedef __ino_t ino_t;
  26. # else
  27. typedef __ino64_t ino_t;
  28. # endif
  29. # define __ino_t_defined
  30. # endif
  31. # if defined __USE_LARGEFILE64 && !defined __ino64_t_defined
  32. typedef __ino64_t ino64_t;
  33. # define __ino64_t_defined
  34. # endif
  35. #endif
  36. /* This file defines `struct dirent'.
  37. It defines the macro `_DIRENT_HAVE_D_NAMLEN' iff there is a `d_namlen'
  38. member that gives the length of `d_name'.
  39. It defines the macro `_DIRENT_HAVE_D_RECLEN' iff there is a `d_reclen'
  40. member that gives the size of the entire directory entry.
  41. It defines the macro `_DIRENT_HAVE_D_OFF' iff there is a `d_off'
  42. member that gives the file offset of the next directory entry.
  43. It defines the macro `_DIRENT_HAVE_D_TYPE' iff there is a `d_type'
  44. member that gives the type of the file.
  45. */
  46. #include <bits/dirent.h>
  47. #if defined __USE_MISC && !defined d_fileno
  48. # define d_ino d_fileno /* Backward compatibility. */
  49. #endif
  50. /* These macros extract size information from a `struct dirent *'.
  51. They may evaluate their argument multiple times, so it must not
  52. have side effects. Each of these may involve a relatively costly
  53. call to `strlen' on some systems, so these values should be cached.
  54. _D_EXACT_NAMLEN (DP) returns the length of DP->d_name, not including
  55. its terminating null character.
  56. _D_ALLOC_NAMLEN (DP) returns a size at least (_D_EXACT_NAMLEN (DP) + 1);
  57. that is, the allocation size needed to hold the DP->d_name string.
  58. Use this macro when you don't need the exact length, just an upper bound.
  59. This macro is less likely to require calling `strlen' than _D_EXACT_NAMLEN.
  60. */
  61. #ifdef _DIRENT_HAVE_D_NAMLEN
  62. # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
  63. # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
  64. #else
  65. # define _D_EXACT_NAMLEN(d) (strlen ((d)->d_name))
  66. # ifdef _DIRENT_HAVE_D_RECLEN
  67. # define _D_ALLOC_NAMLEN(d) (((char *) (d) + (d)->d_reclen) - &(d)->d_name[0])
  68. # else
  69. # define _D_ALLOC_NAMLEN(d) (sizeof (d)->d_name > 1 ? sizeof (d)->d_name \
  70. : _D_EXACT_NAMLEN (d) + 1)
  71. # endif
  72. #endif
  73. #ifdef __USE_MISC
  74. /* File types for `d_type'. */
  75. enum
  76. {
  77. DT_UNKNOWN = 0,
  78. # define DT_UNKNOWN DT_UNKNOWN
  79. DT_FIFO = 1,
  80. # define DT_FIFO DT_FIFO
  81. DT_CHR = 2,
  82. # define DT_CHR DT_CHR
  83. DT_DIR = 4,
  84. # define DT_DIR DT_DIR
  85. DT_BLK = 6,
  86. # define DT_BLK DT_BLK
  87. DT_REG = 8,
  88. # define DT_REG DT_REG
  89. DT_LNK = 10,
  90. # define DT_LNK DT_LNK
  91. DT_SOCK = 12,
  92. # define DT_SOCK DT_SOCK
  93. DT_WHT = 14
  94. # define DT_WHT DT_WHT
  95. };
  96. /* Convert between stat structure types and directory types. */
  97. # define IFTODT(mode) (((mode) & 0170000) >> 12)
  98. # define DTTOIF(dirtype) ((dirtype) << 12)
  99. #endif
  100. /* This is the data type of directory stream objects.
  101. The actual structure is opaque to users. */
  102. typedef struct __dirstream DIR;
  103. /* Close the directory stream DIRP.
  104. Return 0 if successful, -1 if not.
  105. This function is a possible cancellation point and therefore not
  106. marked with __THROW. */
  107. extern int closedir (DIR *__dirp) __nonnull ((1));
  108. /* Open a directory stream on NAME.
  109. Return a DIR stream on the directory, or NULL if it could not be opened.
  110. This function is a possible cancellation point and therefore not
  111. marked with __THROW. */
  112. extern DIR *opendir (const char *__name) __nonnull ((1))
  113. __attribute_malloc__ __attr_dealloc (closedir, 1);
  114. #ifdef __USE_XOPEN2K8
  115. /* Same as opendir, but open the stream on the file descriptor FD.
  116. This function is a possible cancellation point and therefore not
  117. marked with __THROW. */
  118. extern DIR *fdopendir (int __fd)
  119. __attribute_malloc__ __attr_dealloc (closedir, 1);
  120. #endif
  121. /* Read a directory entry from DIRP. Return a pointer to a `struct
  122. dirent' describing the entry, or NULL for EOF or error. The
  123. storage returned may be overwritten by a later readdir call on the
  124. same DIR stream.
  125. If the Large File Support API is selected we have to use the
  126. appropriate interface.
  127. This function is a possible cancellation point and therefore not
  128. marked with __THROW. */
  129. #ifndef __USE_FILE_OFFSET64
  130. extern struct dirent *readdir (DIR *__dirp) __nonnull ((1));
  131. #else
  132. # ifdef __REDIRECT
  133. extern struct dirent *__REDIRECT (readdir, (DIR *__dirp), readdir64)
  134. __nonnull ((1));
  135. # else
  136. # define readdir readdir64
  137. # endif
  138. #endif
  139. #ifdef __USE_LARGEFILE64
  140. extern struct dirent64 *readdir64 (DIR *__dirp) __nonnull ((1));
  141. #endif
  142. #ifdef __USE_POSIX
  143. /* Reentrant version of `readdir'. Return in RESULT a pointer to the
  144. next entry.
  145. This function is a possible cancellation point and therefore not
  146. marked with __THROW. */
  147. # ifndef __USE_FILE_OFFSET64
  148. extern int readdir_r (DIR *__restrict __dirp,
  149. struct dirent *__restrict __entry,
  150. struct dirent **__restrict __result)
  151. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  152. # else
  153. # ifdef __REDIRECT
  154. extern int __REDIRECT (readdir_r,
  155. (DIR *__restrict __dirp,
  156. struct dirent *__restrict __entry,
  157. struct dirent **__restrict __result),
  158. readdir64_r)
  159. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  160. # else
  161. # define readdir_r readdir64_r
  162. # endif
  163. # endif
  164. # ifdef __USE_LARGEFILE64
  165. extern int readdir64_r (DIR *__restrict __dirp,
  166. struct dirent64 *__restrict __entry,
  167. struct dirent64 **__restrict __result)
  168. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  169. # endif
  170. #endif /* POSIX or misc */
  171. /* Rewind DIRP to the beginning of the directory. */
  172. extern void rewinddir (DIR *__dirp) __THROW __nonnull ((1));
  173. #if defined __USE_MISC || defined __USE_XOPEN
  174. # include <bits/types.h>
  175. /* Seek to position POS on DIRP. */
  176. extern void seekdir (DIR *__dirp, long int __pos) __THROW __nonnull ((1));
  177. /* Return the current position of DIRP. */
  178. extern long int telldir (DIR *__dirp) __THROW __nonnull ((1));
  179. #endif
  180. #ifdef __USE_XOPEN2K8
  181. /* Return the file descriptor used by DIRP. */
  182. extern int dirfd (DIR *__dirp) __THROW __nonnull ((1));
  183. # if defined __OPTIMIZE__ && defined _DIR_dirfd
  184. # define dirfd(dirp) _DIR_dirfd (dirp)
  185. # endif
  186. # ifdef __USE_MISC
  187. # ifndef MAXNAMLEN
  188. /* Get the definitions of the POSIX.1 limits. */
  189. # include <bits/posix1_lim.h>
  190. /* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */
  191. # ifdef NAME_MAX
  192. # define MAXNAMLEN NAME_MAX
  193. # else
  194. # define MAXNAMLEN 255
  195. # endif
  196. # endif
  197. # endif
  198. # define __need_size_t
  199. # include <stddef.h>
  200. /* Scan the directory DIR, calling SELECTOR on each directory entry.
  201. Entries for which SELECT returns nonzero are individually malloc'd,
  202. sorted using qsort with CMP, and collected in a malloc'd array in
  203. *NAMELIST. Returns the number of entries selected, or -1 on error.
  204. This function is a cancellation point and therefore not marked with
  205. __THROW. */
  206. # ifndef __USE_FILE_OFFSET64
  207. extern int scandir (const char *__restrict __dir,
  208. struct dirent ***__restrict __namelist,
  209. int (*__selector) (const struct dirent *),
  210. int (*__cmp) (const struct dirent **,
  211. const struct dirent **))
  212. __nonnull ((1, 2));
  213. # else
  214. # ifdef __REDIRECT
  215. extern int __REDIRECT (scandir,
  216. (const char *__restrict __dir,
  217. struct dirent ***__restrict __namelist,
  218. int (*__selector) (const struct dirent *),
  219. int (*__cmp) (const struct dirent **,
  220. const struct dirent **)),
  221. scandir64) __nonnull ((1, 2));
  222. # else
  223. # define scandir scandir64
  224. # endif
  225. # endif
  226. # if defined __USE_GNU && defined __USE_LARGEFILE64
  227. /* This function is like `scandir' but it uses the 64bit dirent structure.
  228. Please note that the CMP function must now work with struct dirent64 **. */
  229. extern int scandir64 (const char *__restrict __dir,
  230. struct dirent64 ***__restrict __namelist,
  231. int (*__selector) (const struct dirent64 *),
  232. int (*__cmp) (const struct dirent64 **,
  233. const struct dirent64 **))
  234. __nonnull ((1, 2));
  235. # endif
  236. # ifdef __USE_GNU
  237. /* Similar to `scandir' but a relative DIR name is interpreted relative
  238. to the directory for which DFD is a descriptor.
  239. This function is a cancellation point and therefore not marked with
  240. __THROW. */
  241. # ifndef __USE_FILE_OFFSET64
  242. extern int scandirat (int __dfd, const char *__restrict __dir,
  243. struct dirent ***__restrict __namelist,
  244. int (*__selector) (const struct dirent *),
  245. int (*__cmp) (const struct dirent **,
  246. const struct dirent **))
  247. __nonnull ((2, 3));
  248. # else
  249. # ifdef __REDIRECT
  250. extern int __REDIRECT (scandirat,
  251. (int __dfd, const char *__restrict __dir,
  252. struct dirent ***__restrict __namelist,
  253. int (*__selector) (const struct dirent *),
  254. int (*__cmp) (const struct dirent **,
  255. const struct dirent **)),
  256. scandirat64) __nonnull ((2, 3));
  257. # else
  258. # define scandirat scandirat64
  259. # endif
  260. # endif
  261. /* This function is like `scandir' but it uses the 64bit dirent structure.
  262. Please note that the CMP function must now work with struct dirent64 **. */
  263. extern int scandirat64 (int __dfd, const char *__restrict __dir,
  264. struct dirent64 ***__restrict __namelist,
  265. int (*__selector) (const struct dirent64 *),
  266. int (*__cmp) (const struct dirent64 **,
  267. const struct dirent64 **))
  268. __nonnull ((2, 3));
  269. # endif
  270. /* Function to compare two `struct dirent's alphabetically. */
  271. # ifndef __USE_FILE_OFFSET64
  272. extern int alphasort (const struct dirent **__e1,
  273. const struct dirent **__e2)
  274. __THROW __attribute_pure__ __nonnull ((1, 2));
  275. # else
  276. # ifdef __REDIRECT
  277. extern int __REDIRECT_NTH (alphasort,
  278. (const struct dirent **__e1,
  279. const struct dirent **__e2),
  280. alphasort64) __attribute_pure__ __nonnull ((1, 2));
  281. # else
  282. # define alphasort alphasort64
  283. # endif
  284. # endif
  285. # if defined __USE_GNU && defined __USE_LARGEFILE64
  286. extern int alphasort64 (const struct dirent64 **__e1,
  287. const struct dirent64 **__e2)
  288. __THROW __attribute_pure__ __nonnull ((1, 2));
  289. # endif
  290. #endif /* Use XPG7. */
  291. #ifdef __USE_MISC
  292. /* Read directory entries from FD into BUF, reading at most NBYTES.
  293. Reading starts at offset *BASEP, and *BASEP is updated with the new
  294. position after reading. Returns the number of bytes read; zero when at
  295. end of directory; or -1 for errors. */
  296. # ifndef __USE_FILE_OFFSET64
  297. extern __ssize_t getdirentries (int __fd, char *__restrict __buf,
  298. size_t __nbytes,
  299. __off_t *__restrict __basep)
  300. __THROW __nonnull ((2, 4));
  301. # else
  302. # ifdef __REDIRECT
  303. extern __ssize_t __REDIRECT_NTH (getdirentries,
  304. (int __fd, char *__restrict __buf,
  305. size_t __nbytes,
  306. __off64_t *__restrict __basep),
  307. getdirentries64) __nonnull ((2, 4));
  308. # else
  309. # define getdirentries getdirentries64
  310. # endif
  311. # endif
  312. # ifdef __USE_LARGEFILE64
  313. extern __ssize_t getdirentries64 (int __fd, char *__restrict __buf,
  314. size_t __nbytes,
  315. __off64_t *__restrict __basep)
  316. __THROW __nonnull ((2, 4));
  317. # endif
  318. #endif /* Use misc. */
  319. #ifdef __USE_GNU
  320. /* Function to compare two `struct dirent's by name & version. */
  321. # ifndef __USE_FILE_OFFSET64
  322. extern int versionsort (const struct dirent **__e1,
  323. const struct dirent **__e2)
  324. __THROW __attribute_pure__ __nonnull ((1, 2));
  325. # else
  326. # ifdef __REDIRECT
  327. extern int __REDIRECT_NTH (versionsort,
  328. (const struct dirent **__e1,
  329. const struct dirent **__e2),
  330. versionsort64)
  331. __attribute_pure__ __nonnull ((1, 2));
  332. # else
  333. # define versionsort versionsort64
  334. # endif
  335. # endif
  336. # ifdef __USE_LARGEFILE64
  337. extern int versionsort64 (const struct dirent64 **__e1,
  338. const struct dirent64 **__e2)
  339. __THROW __attribute_pure__ __nonnull ((1, 2));
  340. # endif
  341. #endif /* Use GNU. */
  342. __END_DECLS
  343. #include <bits/dirent_ext.h>
  344. #endif /* dirent.h */