dirent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*****************************************************************************
  2. * dirent.h - dirent API for Microsoft Visual Studio
  3. *
  4. * Copyright (C) 2006 Toni Ronkko
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * ``Software''), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Mar 15, 2011, Toni Ronkko
  26. * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
  27. *
  28. * Aug 11, 2010, Toni Ronkko
  29. * Added d_type and d_namlen fields to dirent structure. The former is
  30. * especially useful for determining whether directory entry represents a
  31. * file or a directory. For more information, see
  32. * http://www.delorie.com/gnu/docs/glibc/libc_270.html
  33. *
  34. * Aug 11, 2010, Toni Ronkko
  35. * Improved conformance to the standards. For example, errno is now set
  36. * properly on failure and assert() is never used. Thanks to Peter Brockam
  37. * for suggestions.
  38. *
  39. * Aug 11, 2010, Toni Ronkko
  40. * Fixed a bug in rewinddir(): when using relative directory names, change
  41. * of working directory no longer causes rewinddir() to fail.
  42. *
  43. * Dec 15, 2009, John Cunningham
  44. * Added rewinddir member function
  45. *
  46. * Jan 18, 2008, Toni Ronkko
  47. * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
  48. * between multi-byte and unicode representations. This makes the
  49. * code simpler and also allows the code to be compiled under MingW. Thanks
  50. * to Azriel Fasten for the suggestion.
  51. *
  52. * Mar 4, 2007, Toni Ronkko
  53. * Bug fix: due to the strncpy_s() function this file only compiled in
  54. * Visual Studio 2005. Using the new string functions only when the
  55. * compiler version allows.
  56. *
  57. * Nov 2, 2006, Toni Ronkko
  58. * Major update: removed support for Watcom C, MS-DOS and Turbo C to
  59. * simplify the file, updated the code to compile cleanly on Visual
  60. * Studio 2005 with both unicode and multi-byte character strings,
  61. * removed rewinddir() as it had a bug.
  62. *
  63. * Aug 20, 2006, Toni Ronkko
  64. * Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
  65. * comments by removing SGML tags.
  66. *
  67. * May 14 2002, Toni Ronkko
  68. * Embedded the function definitions directly to the header so that no
  69. * source modules need to be included in the Visual Studio project. Removed
  70. * all the dependencies to other projects so that this very header can be
  71. * used independently.
  72. *
  73. * May 28 1998, Toni Ronkko
  74. * First version.
  75. *****************************************************************************/
  76. #ifndef DIRENT_H
  77. #define DIRENT_H
  78. #define WIN32_LEAN_AND_MEAN
  79. #include <windows.h>
  80. #include <string.h>
  81. #include <stdlib.h>
  82. #include <sys/types.h>
  83. #include <sys/stat.h>
  84. #include <errno.h>
  85. /* Entries missing from MSVC 6.0 */
  86. #if !defined(FILE_ATTRIBUTE_DEVICE)
  87. # define FILE_ATTRIBUTE_DEVICE 0x40
  88. #endif
  89. /* File type and permission flags for stat() */
  90. #if defined(_MSC_VER) && !defined(S_IREAD)
  91. # define S_IFMT _S_IFMT /* file type mask */
  92. # define S_IFDIR _S_IFDIR /* directory */
  93. # define S_IFCHR _S_IFCHR /* character device */
  94. # define S_IFFIFO _S_IFFIFO /* pipe */
  95. # define S_IFREG _S_IFREG /* regular file */
  96. # define S_IREAD _S_IREAD /* read permission */
  97. # define S_IWRITE _S_IWRITE /* write permission */
  98. # define S_IEXEC _S_IEXEC /* execute permission */
  99. #endif
  100. #define S_IFBLK 0 /* block device */
  101. #define S_IFLNK 0 /* link */
  102. #define S_IFSOCK 0 /* socket */
  103. #if defined(_MSC_VER)
  104. # define S_IRUSR S_IREAD /* read, user */
  105. # define S_IWUSR S_IWRITE /* write, user */
  106. # define S_IXUSR 0 /* execute, user */
  107. # define S_IRGRP 0 /* read, group */
  108. # define S_IWGRP 0 /* write, group */
  109. # define S_IXGRP 0 /* execute, group */
  110. # define S_IROTH 0 /* read, others */
  111. # define S_IWOTH 0 /* write, others */
  112. # define S_IXOTH 0 /* execute, others */
  113. #endif
  114. /* Indicates that d_type field is available in dirent structure */
  115. #define _DIRENT_HAVE_D_TYPE
  116. /* File type flags for d_type */
  117. #define DT_UNKNOWN 0
  118. #define DT_REG S_IFREG
  119. #define DT_DIR S_IFDIR
  120. #define DT_FIFO S_IFFIFO
  121. #define DT_SOCK S_IFSOCK
  122. #define DT_CHR S_IFCHR
  123. #define DT_BLK S_IFBLK
  124. /* Macros for converting between st_mode and d_type */
  125. #define IFTODT(mode) ((mode) & S_IFMT)
  126. #define DTTOIF(type) (type)
  127. /*
  128. * File type macros. Note that block devices, sockets and links cannot be
  129. * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
  130. * only defined for compatibility. These macros should always return false
  131. * on Windows.
  132. */
  133. #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
  134. #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  135. #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
  136. #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
  137. #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
  138. #ifdef __cplusplus
  139. extern "C" {
  140. #endif
  141. typedef struct dirent
  142. {
  143. char d_name[MAX_PATH + 1]; /* File name */
  144. size_t d_namlen; /* Length of name without \0 */
  145. int d_type; /* File type */
  146. } dirent;
  147. typedef struct DIR
  148. {
  149. dirent curentry; /* Current directory entry */
  150. WIN32_FIND_DATAA find_data; /* Private file data */
  151. int cached; /* True if data is valid */
  152. HANDLE search_handle; /* Win32 search handle */
  153. char patt[MAX_PATH + 3]; /* Initial directory name */
  154. } DIR;
  155. /* Forward declarations */
  156. static DIR *opendir(const char *dirname);
  157. static struct dirent *readdir(DIR *dirp);
  158. static int closedir(DIR *dirp);
  159. static void rewinddir(DIR* dirp);
  160. /* Use the new safe string functions introduced in Visual Studio 2005 */
  161. #if defined(_MSC_VER) && _MSC_VER >= 1400
  162. # define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
  163. #else
  164. # define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size))
  165. #endif
  166. /* Set errno variable */
  167. #if defined(_MSC_VER)
  168. #define DIRENT_SET_ERRNO(x) _set_errno (x)
  169. #else
  170. #define DIRENT_SET_ERRNO(x) (errno = (x))
  171. #endif
  172. /*****************************************************************************
  173. * Open directory stream DIRNAME for read and return a pointer to the
  174. * internal working area that is used to retrieve individual directory
  175. * entries.
  176. */
  177. static DIR *opendir(const char *dirname)
  178. {
  179. DIR *dirp;
  180. /* ensure that the resulting search pattern will be a valid file name */
  181. if (dirname == NULL) {
  182. DIRENT_SET_ERRNO (ENOENT);
  183. return NULL;
  184. }
  185. if (strlen (dirname) + 3 >= MAX_PATH) {
  186. DIRENT_SET_ERRNO (ENAMETOOLONG);
  187. return NULL;
  188. }
  189. /* construct new DIR structure */
  190. dirp = (DIR*) malloc (sizeof (struct DIR));
  191. if (dirp != NULL) {
  192. int error;
  193. /*
  194. * Convert relative directory name to an absolute one. This
  195. * allows rewinddir() to function correctly when the current working
  196. * directory is changed between opendir() and rewinddir().
  197. */
  198. if (GetFullPathNameA (dirname, MAX_PATH, dirp->patt, NULL)) {
  199. char *p;
  200. /* append the search pattern "\\*\0" to the directory name */
  201. p = strchr (dirp->patt, '\0');
  202. if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
  203. *p++ = '\\';
  204. }
  205. *p++ = '*';
  206. *p = '\0';
  207. /* open directory stream and retrieve the first entry */
  208. dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
  209. if (dirp->search_handle != INVALID_HANDLE_VALUE) {
  210. /* a directory entry is now waiting in memory */
  211. dirp->cached = 1;
  212. error = 0;
  213. } else {
  214. /* search pattern is not a directory name? */
  215. DIRENT_SET_ERRNO (ENOENT);
  216. error = 1;
  217. }
  218. } else {
  219. /* buffer too small */
  220. DIRENT_SET_ERRNO (ENOMEM);
  221. error = 1;
  222. }
  223. if (error) {
  224. free (dirp);
  225. dirp = NULL;
  226. }
  227. }
  228. return dirp;
  229. }
  230. /*****************************************************************************
  231. * Read a directory entry, and return a pointer to a dirent structure
  232. * containing the name of the entry in d_name field. Individual directory
  233. * entries returned by this very function include regular files,
  234. * sub-directories, pseudo-directories "." and "..", but also volume labels,
  235. * hidden files and system files may be returned.
  236. */
  237. static struct dirent *readdir(DIR *dirp)
  238. {
  239. DWORD attr;
  240. if (dirp == NULL) {
  241. /* directory stream did not open */
  242. DIRENT_SET_ERRNO (EBADF);
  243. return NULL;
  244. }
  245. /* get next directory entry */
  246. if (dirp->cached != 0) {
  247. /* a valid directory entry already in memory */
  248. dirp->cached = 0;
  249. } else {
  250. /* get the next directory entry from stream */
  251. if (dirp->search_handle == INVALID_HANDLE_VALUE) {
  252. return NULL;
  253. }
  254. if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
  255. /* the very last entry has been processed or an error occured */
  256. FindClose (dirp->search_handle);
  257. dirp->search_handle = INVALID_HANDLE_VALUE;
  258. return NULL;
  259. }
  260. }
  261. /* copy as a multibyte character string */
  262. DIRENT_STRNCPY ( dirp->curentry.d_name,
  263. dirp->find_data.cFileName,
  264. sizeof(dirp->curentry.d_name) );
  265. dirp->curentry.d_name[MAX_PATH] = '\0';
  266. /* compute the length of name */
  267. dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
  268. /* determine file type */
  269. attr = dirp->find_data.dwFileAttributes;
  270. if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
  271. dirp->curentry.d_type = DT_CHR;
  272. } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
  273. dirp->curentry.d_type = DT_DIR;
  274. } else {
  275. dirp->curentry.d_type = DT_REG;
  276. }
  277. return &dirp->curentry;
  278. }
  279. /*****************************************************************************
  280. * Close directory stream opened by opendir() function. Close of the
  281. * directory stream invalidates the DIR structure as well as any previously
  282. * read directory entry.
  283. */
  284. static int closedir(DIR *dirp)
  285. {
  286. if (dirp == NULL) {
  287. /* invalid directory stream */
  288. DIRENT_SET_ERRNO (EBADF);
  289. return -1;
  290. }
  291. /* release search handle */
  292. if (dirp->search_handle != INVALID_HANDLE_VALUE) {
  293. FindClose (dirp->search_handle);
  294. dirp->search_handle = INVALID_HANDLE_VALUE;
  295. }
  296. /* release directory structure */
  297. free (dirp);
  298. return 0;
  299. }
  300. /*****************************************************************************
  301. * Resets the position of the directory stream to which dirp refers to the
  302. * beginning of the directory. It also causes the directory stream to refer
  303. * to the current state of the corresponding directory, as a call to opendir()
  304. * would have done. If dirp does not refer to a directory stream, the effect
  305. * is undefined.
  306. */
  307. static void rewinddir(DIR* dirp)
  308. {
  309. if (dirp != NULL) {
  310. /* release search handle */
  311. if (dirp->search_handle != INVALID_HANDLE_VALUE) {
  312. FindClose (dirp->search_handle);
  313. }
  314. /* open new search handle and retrieve the first entry */
  315. dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
  316. if (dirp->search_handle != INVALID_HANDLE_VALUE) {
  317. /* a directory entry is now waiting in memory */
  318. dirp->cached = 1;
  319. } else {
  320. /* failed to re-open directory: no directory entry in memory */
  321. dirp->cached = 0;
  322. }
  323. }
  324. }
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif /*DIRENT_H*/