dirent.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* Copyright (C) 1991-2016 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. <http://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));
  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) __nonnull ((1, 2, 3));
  157. # else
  158. # define readdir_r readdir64_r
  159. # endif
  160. # endif
  161. # ifdef __USE_LARGEFILE64
  162. extern int readdir64_r (DIR *__restrict __dirp,
  163. struct dirent64 *__restrict __entry,
  164. struct dirent64 **__restrict __result)
  165. __nonnull ((1, 2, 3));
  166. # endif
  167. #endif /* POSIX or misc */
  168. /* Rewind DIRP to the beginning of the directory. */
  169. extern void rewinddir (DIR *__dirp) __THROW __nonnull ((1));
  170. #if defined __USE_MISC || defined __USE_XOPEN
  171. # include <bits/types.h>
  172. /* Seek to position POS on DIRP. */
  173. extern void seekdir (DIR *__dirp, long int __pos) __THROW __nonnull ((1));
  174. /* Return the current position of DIRP. */
  175. extern long int telldir (DIR *__dirp) __THROW __nonnull ((1));
  176. #endif
  177. #ifdef __USE_XOPEN2K8
  178. /* Return the file descriptor used by DIRP. */
  179. extern int dirfd (DIR *__dirp) __THROW __nonnull ((1));
  180. # if defined __OPTIMIZE__ && defined _DIR_dirfd
  181. # define dirfd(dirp) _DIR_dirfd (dirp)
  182. # endif
  183. # ifdef __USE_MISC
  184. # ifndef MAXNAMLEN
  185. /* Get the definitions of the POSIX.1 limits. */
  186. # include <bits/posix1_lim.h>
  187. /* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */
  188. # ifdef NAME_MAX
  189. # define MAXNAMLEN NAME_MAX
  190. # else
  191. # define MAXNAMLEN 255
  192. # endif
  193. # endif
  194. # endif
  195. # define __need_size_t
  196. # include <stddef.h>
  197. /* Scan the directory DIR, calling SELECTOR on each directory entry.
  198. Entries for which SELECT returns nonzero are individually malloc'd,
  199. sorted using qsort with CMP, and collected in a malloc'd array in
  200. *NAMELIST. Returns the number of entries selected, or -1 on error.
  201. This function is a cancellation point and therefore not marked with
  202. __THROW. */
  203. # ifndef __USE_FILE_OFFSET64
  204. extern int scandir (const char *__restrict __dir,
  205. struct dirent ***__restrict __namelist,
  206. int (*__selector) (const struct dirent *),
  207. int (*__cmp) (const struct dirent **,
  208. const struct dirent **))
  209. __nonnull ((1, 2));
  210. # else
  211. # ifdef __REDIRECT
  212. extern int __REDIRECT (scandir,
  213. (const char *__restrict __dir,
  214. struct dirent ***__restrict __namelist,
  215. int (*__selector) (const struct dirent *),
  216. int (*__cmp) (const struct dirent **,
  217. const struct dirent **)),
  218. scandir64) __nonnull ((1, 2));
  219. # else
  220. # define scandir scandir64
  221. # endif
  222. # endif
  223. # if defined __USE_GNU && defined __USE_LARGEFILE64
  224. /* This function is like `scandir' but it uses the 64bit dirent structure.
  225. Please note that the CMP function must now work with struct dirent64 **. */
  226. extern int scandir64 (const char *__restrict __dir,
  227. struct dirent64 ***__restrict __namelist,
  228. int (*__selector) (const struct dirent64 *),
  229. int (*__cmp) (const struct dirent64 **,
  230. const struct dirent64 **))
  231. __nonnull ((1, 2));
  232. # endif
  233. # ifdef __USE_GNU
  234. /* Similar to `scandir' but a relative DIR name is interpreted relative
  235. to the directory for which DFD is a descriptor.
  236. This function is a cancellation point and therefore not marked with
  237. __THROW. */
  238. # ifndef __USE_FILE_OFFSET64
  239. extern int scandirat (int __dfd, const char *__restrict __dir,
  240. struct dirent ***__restrict __namelist,
  241. int (*__selector) (const struct dirent *),
  242. int (*__cmp) (const struct dirent **,
  243. const struct dirent **))
  244. __nonnull ((2, 3));
  245. # else
  246. # ifdef __REDIRECT
  247. extern int __REDIRECT (scandirat,
  248. (int __dfd, const char *__restrict __dir,
  249. struct dirent ***__restrict __namelist,
  250. int (*__selector) (const struct dirent *),
  251. int (*__cmp) (const struct dirent **,
  252. const struct dirent **)),
  253. scandirat64) __nonnull ((2, 3));
  254. # else
  255. # define scandirat scandirat64
  256. # endif
  257. # endif
  258. /* This function is like `scandir' but it uses the 64bit dirent structure.
  259. Please note that the CMP function must now work with struct dirent64 **. */
  260. extern int scandirat64 (int __dfd, const char *__restrict __dir,
  261. struct dirent64 ***__restrict __namelist,
  262. int (*__selector) (const struct dirent64 *),
  263. int (*__cmp) (const struct dirent64 **,
  264. const struct dirent64 **))
  265. __nonnull ((2, 3));
  266. # endif
  267. /* Function to compare two `struct dirent's alphabetically. */
  268. # ifndef __USE_FILE_OFFSET64
  269. extern int alphasort (const struct dirent **__e1,
  270. const struct dirent **__e2)
  271. __THROW __attribute_pure__ __nonnull ((1, 2));
  272. # else
  273. # ifdef __REDIRECT
  274. extern int __REDIRECT_NTH (alphasort,
  275. (const struct dirent **__e1,
  276. const struct dirent **__e2),
  277. alphasort64) __attribute_pure__ __nonnull ((1, 2));
  278. # else
  279. # define alphasort alphasort64
  280. # endif
  281. # endif
  282. # if defined __USE_GNU && defined __USE_LARGEFILE64
  283. extern int alphasort64 (const struct dirent64 **__e1,
  284. const struct dirent64 **__e2)
  285. __THROW __attribute_pure__ __nonnull ((1, 2));
  286. # endif
  287. #endif /* Use XPG7. */
  288. #ifdef __USE_MISC
  289. /* Read directory entries from FD into BUF, reading at most NBYTES.
  290. Reading starts at offset *BASEP, and *BASEP is updated with the new
  291. position after reading. Returns the number of bytes read; zero when at
  292. end of directory; or -1 for errors. */
  293. # ifndef __USE_FILE_OFFSET64
  294. extern __ssize_t getdirentries (int __fd, char *__restrict __buf,
  295. size_t __nbytes,
  296. __off_t *__restrict __basep)
  297. __THROW __nonnull ((2, 4));
  298. # else
  299. # ifdef __REDIRECT
  300. extern __ssize_t __REDIRECT_NTH (getdirentries,
  301. (int __fd, char *__restrict __buf,
  302. size_t __nbytes,
  303. __off64_t *__restrict __basep),
  304. getdirentries64) __nonnull ((2, 4));
  305. # else
  306. # define getdirentries getdirentries64
  307. # endif
  308. # endif
  309. # ifdef __USE_LARGEFILE64
  310. extern __ssize_t getdirentries64 (int __fd, char *__restrict __buf,
  311. size_t __nbytes,
  312. __off64_t *__restrict __basep)
  313. __THROW __nonnull ((2, 4));
  314. # endif
  315. #endif /* Use misc. */
  316. #ifdef __USE_GNU
  317. /* Function to compare two `struct dirent's by name & version. */
  318. # ifndef __USE_FILE_OFFSET64
  319. extern int versionsort (const struct dirent **__e1,
  320. const struct dirent **__e2)
  321. __THROW __attribute_pure__ __nonnull ((1, 2));
  322. # else
  323. # ifdef __REDIRECT
  324. extern int __REDIRECT_NTH (versionsort,
  325. (const struct dirent **__e1,
  326. const struct dirent **__e2),
  327. versionsort64)
  328. __attribute_pure__ __nonnull ((1, 2));
  329. # else
  330. # define versionsort versionsort64
  331. # endif
  332. # endif
  333. # ifdef __USE_LARGEFILE64
  334. extern int versionsort64 (const struct dirent64 **__e1,
  335. const struct dirent64 **__e2)
  336. __THROW __attribute_pure__ __nonnull ((1, 2));
  337. # endif
  338. #endif /* Use GNU. */
  339. __END_DECLS
  340. #endif /* dirent.h */