dirent.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. dirent.h
  8. Abstract:
  9. This header contains definitions for enumerating the contents of file
  10. system directories.
  11. Author:
  12. Evan Green 11-Mar-2013
  13. --*/
  14. #ifndef _DIRENT_H
  15. #define _DIRENT_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <limits.h>
  20. #include <sys/types.h>
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. //
  28. // Define file types found in the directory entry structure.
  29. //
  30. //
  31. // Unknown file type. Use stat to inquire.
  32. //
  33. #define DT_UNKNOWN 0
  34. //
  35. // FIFO pipe object
  36. //
  37. #define DT_FIFO 1
  38. //
  39. // Character device
  40. //
  41. #define DT_CHR 2
  42. //
  43. // Regular directory
  44. //
  45. #define DT_DIR 4
  46. //
  47. // Block device
  48. //
  49. #define DT_BLK 6
  50. //
  51. // Regular file
  52. //
  53. #define DT_REG 8
  54. //
  55. // Symbolic link
  56. //
  57. #define DT_LNK 10
  58. //
  59. // Socket
  60. //
  61. #define DT_SOCK 12
  62. //
  63. // Whiteout entry. The definition is provided for historical reasons. This
  64. // value is never returned by the kernel.
  65. //
  66. #define DT_WHT 14
  67. //
  68. // ------------------------------------------------------ Data Type Definitions
  69. //
  70. //
  71. // Define a type used to represent an open directory stream.
  72. //
  73. typedef struct _DIR DIR;
  74. /*++
  75. Structure Description:
  76. This structure stores information about about a directory entry.
  77. Members:
  78. d_ino - Stores the file serial number for the entry.
  79. d_off - Stores the opaque offset of the next directory entry structure.
  80. This value should not be inspected, as it is unpredictable. It should
  81. only be used to save and restore a location within a directory.
  82. d_reclen - Stores the size in bytes of the entire entry, including this
  83. structure, the name string, and the null terminator on the name.
  84. d_type - Stores the file type of the entry. See DT_* definitions.
  85. d_name - Stores the null terminated name of the directory.
  86. --*/
  87. struct dirent {
  88. ino_t d_ino;
  89. off_t d_off;
  90. unsigned short d_reclen;
  91. unsigned char d_type;
  92. char d_name[NAME_MAX];
  93. };
  94. //
  95. // -------------------------------------------------------------------- Globals
  96. //
  97. //
  98. // -------------------------------------------------------- Function Prototypes
  99. //
  100. LIBC_API
  101. DIR *
  102. opendir (
  103. const char *DirectoryName
  104. );
  105. /*++
  106. Routine Description:
  107. This routine opens a directory for reading.
  108. Arguments:
  109. DirectoryName - Supplies a pointer to a null terminated string containing
  110. the name of the directory to open.
  111. Return Value:
  112. Returns a pointer to the directory on success.
  113. NULL on failure.
  114. --*/
  115. LIBC_API
  116. DIR *
  117. fdopendir (
  118. int FileDescriptor
  119. );
  120. /*++
  121. Routine Description:
  122. This routine opens a directory based on an already open file descriptor to
  123. a directory.
  124. Arguments:
  125. FileDescriptor - Supplies a pointer to the open handle to the directory.
  126. Return Value:
  127. 0 on success.
  128. -1 on failure, and more details will be provided in errno.
  129. --*/
  130. LIBC_API
  131. int
  132. closedir (
  133. DIR *Directory
  134. );
  135. /*++
  136. Routine Description:
  137. This routine closes an open directory.
  138. Arguments:
  139. Directory - Supplies a pointer to the structure returned by the open
  140. directory function.
  141. Return Value:
  142. 0 on success.
  143. -1 on failure, and more details will be provided in errno.
  144. --*/
  145. LIBC_API
  146. int
  147. readdir_r (
  148. DIR *Directory,
  149. struct dirent *Buffer,
  150. struct dirent **Result
  151. );
  152. /*++
  153. Routine Description:
  154. This routine reads from a directly in a reentrant manner.
  155. Arguments:
  156. Directory - Supplies a pointer to the structure returned by the open
  157. directory function.
  158. Buffer - Supplies the buffer where the next directory entry will be
  159. returned.
  160. Result - Supplies a pointer that will either be set to the Buffer pointer
  161. if there are more entries, or NULL if there are no more entries in the
  162. directory.
  163. Return Value:
  164. 0 on success.
  165. Returns an error number on failure.
  166. --*/
  167. LIBC_API
  168. struct dirent *
  169. readdir (
  170. DIR *Directory
  171. );
  172. /*++
  173. Routine Description:
  174. This routine reads the next directory entry from the open directory
  175. stream.
  176. Arguments:
  177. Directory - Supplies a pointer to the structure returned by the open
  178. directory function.
  179. Return Value:
  180. Returns a pointer to the next directory entry on success.
  181. NULL on failure or when the end of the directory is reached. On failure,
  182. errno is set. If the end of the directory is reached, errno is not
  183. changed.
  184. --*/
  185. LIBC_API
  186. void
  187. seekdir (
  188. DIR *Directory,
  189. long Location
  190. );
  191. /*++
  192. Routine Description:
  193. This routine seeks directory to the given location. The location must have
  194. been returned from a previous call to telldir, otherwise the results are
  195. undefined.
  196. Arguments:
  197. Directory - Supplies a pointer to the structure returned by the open
  198. directory function.
  199. Location - Supplies the location within the directory to seek to as given
  200. by the telldir function.
  201. Return Value:
  202. None. No errors are defined.
  203. --*/
  204. LIBC_API
  205. long
  206. telldir (
  207. DIR *Directory
  208. );
  209. /*++
  210. Routine Description:
  211. This routine returns the current position within a directory. This position
  212. can be seeked to later (in fact, the return value from this function is the
  213. only valid parameter to pass to seekdir).
  214. Arguments:
  215. Directory - Supplies a pointer to the structure returned by the open
  216. directory function.
  217. Return Value:
  218. Returns the current location of the specified directory stream.
  219. --*/
  220. LIBC_API
  221. void
  222. rewinddir (
  223. DIR *Directory
  224. );
  225. /*++
  226. Routine Description:
  227. This routine rewinds a directory back to the beginning.
  228. Arguments:
  229. Directory - Supplies a pointer to the structure returned by the open
  230. directory function.
  231. Return Value:
  232. None.
  233. --*/
  234. LIBC_API
  235. int
  236. dirfd (
  237. DIR *Directory
  238. );
  239. /*++
  240. Routine Description:
  241. This routine returns the file descriptor backing the given directory.
  242. Arguments:
  243. Directory - Supplies a pointer to the structure returned by the open
  244. directory function.
  245. Return Value:
  246. None.
  247. --*/
  248. LIBC_API
  249. int
  250. alphasort (
  251. const struct dirent **Left,
  252. const struct dirent **Right
  253. );
  254. /*++
  255. Routine Description:
  256. This routine can be used as the comparison function for the scandir
  257. function. It will compare directory names in alphabetical order.
  258. Arguments:
  259. Left - Supplies a pointer to a pointer to the left directory entry to
  260. compare.
  261. Right - Supplies a pointer to a pointer to the right directory entry to
  262. compare.
  263. Return Value:
  264. < 0 if Left < Right.
  265. 0 if Left == Right.
  266. > 0 if Left > Right.
  267. --*/
  268. LIBC_API
  269. int
  270. scandir (
  271. const char *DirectoryPath,
  272. struct dirent ***NameList,
  273. int (*SelectFunction)(const struct dirent *),
  274. int (*CompareFunction)(const struct dirent **, const struct dirent **)
  275. );
  276. /*++
  277. Routine Description:
  278. This routine scans the given directory, and calls the select function for
  279. each entry. If the select function returns non-zero, then the entry is
  280. added to the list of entries to return. If the compare function is
  281. non-zero the resulting entries are sorted via qsort.
  282. Arguments:
  283. DirectoryPath - Supplies a pointer to a null-terminated string containing
  284. the directory path to scan.
  285. NameList - Supplies a pointer where an array of pointers to selected
  286. directory entries will be returned on success. The caller is
  287. responsible for freeing both the returned array and each individual
  288. element in the array.
  289. SelectFunction - Supplies an optional pointer to a function used to
  290. determine whether or not a particular entry should end up in the
  291. final list. If the select function returns non-zero, then the entry
  292. is added to the final list. If this parameter is NULL, then all
  293. entries are selected.
  294. CompareFunction - Supplies an optional pointer to a function used to
  295. compare two directory entries during the final sorting process. If this
  296. is NULL, then the order of the names in the name list is unspecified.
  297. Return Value:
  298. Returns the number of entries returned in the array on success.
  299. -1 on failure.
  300. --*/
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif