dirent.h 11 KB

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