dirent.h 11 KB

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