1 /**
2  * @file dirent.h
3  * Copyright 2012, 2013 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _DIRENT_H_
25 #define _DIRENT_H_
26 #pragma GCC system_header
27 #include <_mingw.h>
28 
29 #include <io.h>
30 
31 #ifndef RC_INVOKED
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct dirent
38 {
39 	long		d_ino;		/* Always zero. */
40 	unsigned short	d_reclen;	/* Always zero. */
41 	unsigned short	d_namlen;	/* Length of name in d_name. */
42 
43 	/* The following exactly mimic the layout of _finddata_t ...
44 	 */
45 	unsigned	d_type;		/* File attributes */
46 	time_t		d_time_create;
47 	time_t		d_time_access;	/* always midnight local time */
48 	time_t		d_time_write;
49 	_fsize_t	d_size;
50 	/*
51 	 * ...so that we may map a union of _finddata_t at the
52 	 * location of d_type (corresponding to _finddata_t.attrib),
53 	 * and thus map this directly to the _findfirst/_findnext
54 	 * returned field.
55 	 */
56 	char		d_name[FILENAME_MAX]; /* File name. */
57 };
58 
59 /*
60  * This opaque data type represents the private structure
61  * through which a directory stream is referenced.
62  */
63 typedef union __dirstream_t DIR;
64 
65 DIR* __cdecl __MINGW_NOTHROW opendir (const char*);
66 struct dirent* __cdecl __MINGW_NOTHROW readdir (DIR*);
67 int __cdecl __MINGW_NOTHROW closedir (DIR*);
68 void __cdecl __MINGW_NOTHROW rewinddir (DIR*);
69 long __cdecl __MINGW_NOTHROW telldir (DIR*);
70 void __cdecl __MINGW_NOTHROW seekdir (DIR*, long);
71 
72 
73 /* wide char versions */
74 
75 struct _wdirent
76 {
77 	long		d_ino;		/* Always zero. */
78 	unsigned short	d_reclen;	/* Always zero. */
79 	unsigned short	d_namlen;	/* Length of name in d_name. */
80 
81 	/* The following exactly mimic the layout of _wfinddata_t ...
82 	 */
83 	unsigned	d_type;		/* File attributes */
84     	time_t		d_time_create;	/* -1 for FAT file systems */
85     	time_t		d_time_access;	/* -1 for FAT file systems */
86     	time_t		d_time_write;
87     	_fsize_t	d_size;
88 	/*
89 	 * ...so that we may map a union of _wfinddata_t at the
90 	 * location of d_type (corresponding to _wfinddata_t.attrib),
91 	 * and thus map this directly to the _wfindfirst/_wfindnext
92 	 * returned field.
93 	 */
94 	wchar_t		d_name[FILENAME_MAX]; /* File name. */
95 };
96 
97 /*
98  * This opaque data type represents the private structure
99  * through which a wide directory stream is referenced.
100  */
101 typedef union __wdirstream_t _WDIR;
102 
103 _WDIR* __cdecl __MINGW_NOTHROW _wopendir (const wchar_t*);
104 struct _wdirent*  __cdecl __MINGW_NOTHROW _wreaddir (_WDIR*);
105 int __cdecl __MINGW_NOTHROW _wclosedir (_WDIR*);
106 void __cdecl __MINGW_NOTHROW _wrewinddir (_WDIR*);
107 long __cdecl __MINGW_NOTHROW _wtelldir (_WDIR*);
108 void __cdecl __MINGW_NOTHROW _wseekdir (_WDIR*, long);
109 
110 
111 #ifdef	__cplusplus
112 }
113 #endif
114 
115 #if defined(_BSD_SOURCE) || defined(_WIN32)
116 /*
117  * BSD-ish systems define manifest constants for the d_type field;
118  * although probably only DT_REG and DT_DIR are useful on Win32, we
119  * try to map them as best we can from the _finddata.attrib field.
120  *
121  * The relevant Microsoft manifest values are:
122  *
123  *   _A_NORMAL	(0x0000)	normal file: best fit for DT_REG
124  *   _A_RDONLY	(0x0001)	read-only: no BSD d_type equivalent
125  *   _A_HIDDEN	(0x0002)	hidden entity: no BSD equivalent
126  *   _A_SYSTEM	(0x0004)	system entity: no BSD equivalent
127  *   _A_VOLID	(0x0008)	volume label: no BSD equivalent
128  *   _A_SUBDIR	(0x0010)	directory: best fit for DT_DIR
129  *   _A_ARCH	(0x0020)	"dirty": no BSD equivalent
130  *
131  * Thus, we may immediately define:
132  */
133 #define DT_REG   	_A_NORMAL
134 #define DT_DIR   	_A_SUBDIR
135 
136 /* The remaining BSD d_type manifest values have no Win32 equivalents;
137  * we will define them artificially, and then we will ensure that our
138  * opendir()/readdir() implementation will never assign them; (we will
139  * substitute DT_UNKNOWN, but it would be unwise to simply make these
140  * equivalent to that, since an application is likely to simply check
141  * for d_type equal to any one of these defined types, and thus could
142  * mistakenly identify DT_UNKNOWN as being of the tested type):
143  */
144 #define DT_BLK   	(((_A_SUBDIR) << 4) | DT_UNKNOWN)
145 #define DT_CHR   	(((_A_SUBDIR) << 5) | DT_UNKNOWN)
146 #define DT_FIFO  	(((_A_SUBDIR) << 6) | DT_UNKNOWN)
147 #define DT_LNK   	(((_A_SUBDIR) << 7) | DT_UNKNOWN)
148 #define DT_SOCK  	(((_A_SUBDIR) << 8) | DT_UNKNOWN)
149 
150 /* No file system entity can ever be simultaneously a volume label
151  * and a directory; we will exploit this to unambiguously define:
152  */
153 #define DT_UNKNOWN	(_A_VOLID | _A_SUBDIR)
154 
155 #endif  /* _BSD_SOURCE */
156 #endif	/* ! RC_INVOKED */
157 
158 #endif	/* !defined _DIRENT_H_ */
159