dirent.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* Copyright (C) 1991-2020 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. /* Open a directory stream on NAME.
  104. Return a DIR stream on the directory, or NULL if it could not be opened.
  105. This function is a possible cancellation point and therefore not
  106. marked with __THROW. */
  107. extern DIR *opendir (const char *__name) __nonnull ((1));
  108. #ifdef __USE_XOPEN2K8
  109. /* Same as opendir, but open the stream on the file descriptor FD.
  110. This function is a possible cancellation point and therefore not
  111. marked with __THROW. */
  112. extern DIR *fdopendir (int __fd);
  113. #endif
  114. /* Close the directory stream DIRP.
  115. Return 0 if successful, -1 if not.
  116. This function is a possible cancellation point and therefore not
  117. marked with __THROW. */
  118. extern int closedir (DIR *__dirp) __nonnull ((1));
  119. /* Read a directory entry from DIRP. Return a pointer to a `struct
  120. dirent' describing the entry, or NULL for EOF or error. The
  121. storage returned may be overwritten by a later readdir call on the
  122. same DIR stream.
  123. If the Large File Support API is selected we have to use the
  124. appropriate interface.
  125. This function is a possible cancellation point and therefore not
  126. marked with __THROW. */
  127. #ifndef __USE_FILE_OFFSET64
  128. extern struct dirent *readdir (DIR *__dirp) __nonnull ((1));
  129. #else
  130. # ifdef __REDIRECT
  131. extern struct dirent *__REDIRECT (readdir, (DIR *__dirp), readdir64)
  132. __nonnull ((1));
  133. # else
  134. # define readdir readdir64
  135. # endif
  136. #endif
  137. #ifdef __USE_LARGEFILE64
  138. extern struct dirent64 *readdir64 (DIR *__dirp) __nonnull ((1));
  139. #endif
  140. #ifdef __USE_POSIX
  141. /* Reentrant version of `readdir'. Return in RESULT a pointer to the
  142. next entry.
  143. This function is a possible cancellation point and therefore not
  144. marked with __THROW. */
  145. # ifndef __USE_FILE_OFFSET64
  146. extern int readdir_r (DIR *__restrict __dirp,
  147. struct dirent *__restrict __entry,
  148. struct dirent **__restrict __result)
  149. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  150. # else
  151. # ifdef __REDIRECT
  152. extern int __REDIRECT (readdir_r,
  153. (DIR *__restrict __dirp,
  154. struct dirent *__restrict __entry,
  155. struct dirent **__restrict __result),
  156. readdir64_r)
  157. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  158. # else
  159. # define readdir_r readdir64_r
  160. # endif
  161. # endif
  162. # ifdef __USE_LARGEFILE64
  163. extern int readdir64_r (DIR *__restrict __dirp,
  164. struct dirent64 *__restrict __entry,
  165. struct dirent64 **__restrict __result)
  166. __nonnull ((1, 2, 3)) __attribute_deprecated__;
  167. # endif
  168. #endif /* POSIX or misc */
  169. /* Rewind DIRP to the beginning of the directory. */
  170. extern void rewinddir (DIR *__dirp) __THROW __nonnull ((1));
  171. #if defined __USE_MISC || defined __USE_XOPEN
  172. # include <bits/types.h>
  173. /* Seek to position POS on DIRP. */
  174. extern void seekdir (DIR *__dirp, long int __pos) __THROW __nonnull ((1));
  175. /* Return the current position of DIRP. */
  176. extern long int telldir (DIR *__dirp) __THROW __nonnull ((1));
  177. #endif
  178. #ifdef __USE_XOPEN2K8
  179. /* Return the file descriptor used by DIRP. */
  180. extern int dirfd (DIR *__dirp) __THROW __nonnull ((1));
  181. # if defined __OPTIMIZE__ && defined _DIR_dirfd
  182. # define dirfd(dirp) _DIR_dirfd (dirp)
  183. # endif
  184. # ifdef __USE_MISC
  185. # ifndef MAXNAMLEN
  186. /* Get the definitions of the POSIX.1 limits. */
  187. # include <bits/posix1_lim.h>
  188. /* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */
  189. # ifdef NAME_MAX
  190. # define MAXNAMLEN NAME_MAX
  191. # else
  192. # define MAXNAMLEN 255
  193. # endif
  194. # endif
  195. # endif
  196. # define __need_size_t
  197. # include <stddef.h>
  198. /* Scan the directory DIR, calling SELECTOR on each directory entry.
  199. Entries for which SELECT returns nonzero are individually malloc'd,
  200. sorted using qsort with CMP, and collected in a malloc'd array in
  201. *NAMELIST. Returns the number of entries selected, or -1 on error.
  202. This function is a cancellation point and therefore not marked with
  203. __THROW. */
  204. # ifndef __USE_FILE_OFFSET64
  205. extern int scandir (const char *__restrict __dir,
  206. struct dirent ***__restrict __namelist,
  207. int (*__selector) (const struct dirent *),
  208. int (*__cmp) (const struct dirent **,
  209. const struct dirent **))
  210. __nonnull ((1, 2));
  211. # else
  212. # ifdef __REDIRECT
  213. extern int __REDIRECT (scandir,
  214. (const char *__restrict __dir,
  215. struct dirent ***__restrict __namelist,
  216. int (*__selector) (const struct dirent *),
  217. int (*__cmp) (const struct dirent **,
  218. const struct dirent **)),
  219. scandir64) __nonnull ((1, 2));
  220. # else
  221. # define scandir scandir64
  222. # endif
  223. # endif
  224. # if defined __USE_GNU && defined __USE_LARGEFILE64
  225. /* This function is like `scandir' but it uses the 64bit dirent structure.
  226. Please note that the CMP function must now work with struct dirent64 **. */
  227. extern int scandir64 (const char *__restrict __dir,
  228. struct dirent64 ***__restrict __namelist,
  229. int (*__selector) (const struct dirent64 *),
  230. int (*__cmp) (const struct dirent64 **,
  231. const struct dirent64 **))
  232. __nonnull ((1, 2));
  233. # endif
  234. # ifdef __USE_GNU
  235. /* Similar to `scandir' but a relative DIR name is interpreted relative
  236. to the directory for which DFD is a descriptor.
  237. This function is a cancellation point and therefore not marked with
  238. __THROW. */
  239. # ifndef __USE_FILE_OFFSET64
  240. extern int scandirat (int __dfd, const char *__restrict __dir,
  241. struct dirent ***__restrict __namelist,
  242. int (*__selector) (const struct dirent *),
  243. int (*__cmp) (const struct dirent **,
  244. const struct dirent **))
  245. __nonnull ((2, 3));
  246. # else
  247. # ifdef __REDIRECT
  248. extern int __REDIRECT (scandirat,
  249. (int __dfd, const char *__restrict __dir,
  250. struct dirent ***__restrict __namelist,
  251. int (*__selector) (const struct dirent *),
  252. int (*__cmp) (const struct dirent **,
  253. const struct dirent **)),
  254. scandirat64) __nonnull ((2, 3));
  255. # else
  256. # define scandirat scandirat64
  257. # endif
  258. # endif
  259. /* This function is like `scandir' but it uses the 64bit dirent structure.
  260. Please note that the CMP function must now work with struct dirent64 **. */
  261. extern int scandirat64 (int __dfd, const char *__restrict __dir,
  262. struct dirent64 ***__restrict __namelist,
  263. int (*__selector) (const struct dirent64 *),
  264. int (*__cmp) (const struct dirent64 **,
  265. const struct dirent64 **))
  266. __nonnull ((2, 3));
  267. # endif
  268. /* Function to compare two `struct dirent's alphabetically. */
  269. # ifndef __USE_FILE_OFFSET64
  270. extern int alphasort (const struct dirent **__e1,
  271. const struct dirent **__e2)
  272. __THROW __attribute_pure__ __nonnull ((1, 2));
  273. # else
  274. # ifdef __REDIRECT
  275. extern int __REDIRECT_NTH (alphasort,
  276. (const struct dirent **__e1,
  277. const struct dirent **__e2),
  278. alphasort64) __attribute_pure__ __nonnull ((1, 2));
  279. # else
  280. # define alphasort alphasort64
  281. # endif
  282. # endif
  283. # if defined __USE_GNU && defined __USE_LARGEFILE64
  284. extern int alphasort64 (const struct dirent64 **__e1,
  285. const struct dirent64 **__e2)
  286. __THROW __attribute_pure__ __nonnull ((1, 2));
  287. # endif
  288. #endif /* Use XPG7. */
  289. #ifdef __USE_MISC
  290. /* Read directory entries from FD into BUF, reading at most NBYTES.
  291. Reading starts at offset *BASEP, and *BASEP is updated with the new
  292. position after reading. Returns the number of bytes read; zero when at
  293. end of directory; or -1 for errors. */
  294. # ifndef __USE_FILE_OFFSET64
  295. extern __ssize_t getdirentries (int __fd, char *__restrict __buf,
  296. size_t __nbytes,
  297. __off_t *__restrict __basep)
  298. __THROW __nonnull ((2, 4));
  299. # else
  300. # ifdef __REDIRECT
  301. extern __ssize_t __REDIRECT_NTH (getdirentries,
  302. (int __fd, char *__restrict __buf,
  303. size_t __nbytes,
  304. __off64_t *__restrict __basep),
  305. getdirentries64) __nonnull ((2, 4));
  306. # else
  307. # define getdirentries getdirentries64
  308. # endif
  309. # endif
  310. # ifdef __USE_LARGEFILE64
  311. extern __ssize_t getdirentries64 (int __fd, char *__restrict __buf,
  312. size_t __nbytes,
  313. __off64_t *__restrict __basep)
  314. __THROW __nonnull ((2, 4));
  315. # endif
  316. #endif /* Use misc. */
  317. #ifdef __USE_GNU
  318. /* Function to compare two `struct dirent's by name & version. */
  319. # ifndef __USE_FILE_OFFSET64
  320. extern int versionsort (const struct dirent **__e1,
  321. const struct dirent **__e2)
  322. __THROW __attribute_pure__ __nonnull ((1, 2));
  323. # else
  324. # ifdef __REDIRECT
  325. extern int __REDIRECT_NTH (versionsort,
  326. (const struct dirent **__e1,
  327. const struct dirent **__e2),
  328. versionsort64)
  329. __attribute_pure__ __nonnull ((1, 2));
  330. # else
  331. # define versionsort versionsort64
  332. # endif
  333. # endif
  334. # ifdef __USE_LARGEFILE64
  335. extern int versionsort64 (const struct dirent64 **__e1,
  336. const struct dirent64 **__e2)
  337. __THROW __attribute_pure__ __nonnull ((1, 2));
  338. # endif
  339. #endif /* Use GNU. */
  340. __END_DECLS
  341. #include <bits/dirent_ext.h>
  342. #endif /* dirent.h */