Fossil SCM

upgrade dirent.h to latest available version (1.11)

jan.nijtmans 2012-08-30 19:29 trunk
Commit fb07d8693efc99aad3e4a1f659850c4c071e7f71
2 files changed +2 -2 +202 -55
+2 -2
--- src/file.c
+++ src/file.c
@@ -49,11 +49,11 @@
4949
# define stat _stati64
5050
#endif
5151
/*
5252
** On Windows S_ISLNK always returns FALSE.
5353
*/
54
-#if defined(_WIN32)
54
+#if !defined(S_ISLNK)
5555
# define S_ISLNK(x) (0)
5656
#endif
5757
static int fileStatValid = 0;
5858
static struct stat fileStat;
5959
@@ -222,11 +222,11 @@
222222
** - PERM_REG for all other cases (regular file, directory, fifo, etc).
223223
*/
224224
int file_wd_perm(const char *zFilename){
225225
if( getStat(zFilename, 1) ) return PERM_REG;
226226
#if defined(_WIN32)
227
-# if defined(__DMC__) || defined(_MSC_VER)
227
+# ifndef S_IXUSR
228228
# define S_IXUSR _S_IEXEC
229229
# endif
230230
if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
231231
return PERM_EXE;
232232
else
233233
--- src/file.c
+++ src/file.c
@@ -49,11 +49,11 @@
49 # define stat _stati64
50 #endif
51 /*
52 ** On Windows S_ISLNK always returns FALSE.
53 */
54 #if defined(_WIN32)
55 # define S_ISLNK(x) (0)
56 #endif
57 static int fileStatValid = 0;
58 static struct stat fileStat;
59
@@ -222,11 +222,11 @@
222 ** - PERM_REG for all other cases (regular file, directory, fifo, etc).
223 */
224 int file_wd_perm(const char *zFilename){
225 if( getStat(zFilename, 1) ) return PERM_REG;
226 #if defined(_WIN32)
227 # if defined(__DMC__) || defined(_MSC_VER)
228 # define S_IXUSR _S_IEXEC
229 # endif
230 if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
231 return PERM_EXE;
232 else
233
--- src/file.c
+++ src/file.c
@@ -49,11 +49,11 @@
49 # define stat _stati64
50 #endif
51 /*
52 ** On Windows S_ISLNK always returns FALSE.
53 */
54 #if !defined(S_ISLNK)
55 # define S_ISLNK(x) (0)
56 #endif
57 static int fileStatValid = 0;
58 static struct stat fileStat;
59
@@ -222,11 +222,11 @@
222 ** - PERM_REG for all other cases (regular file, directory, fifo, etc).
223 */
224 int file_wd_perm(const char *zFilename){
225 if( getStat(zFilename, 1) ) return PERM_REG;
226 #if defined(_WIN32)
227 # ifndef S_IXUSR
228 # define S_IXUSR _S_IEXEC
229 # endif
230 if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
231 return PERM_EXE;
232 else
233
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,10 +20,32 @@
2020
* IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
2121
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2222
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2323
* OTHER DEALINGS IN THE SOFTWARE.
2424
*
25
+ * Aug 30, 2012, Jan Nijtmans
26
+ * Remove rewinddir() (not necessary for fossil)
27
+ * Replace everything with its wide-character variant.
28
+ *
29
+ * Mar 15, 2011, Toni Ronkko
30
+ * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
31
+ *
32
+ * Aug 11, 2010, Toni Ronkko
33
+ * Added d_type and d_namlen fields to dirent structure. The former is
34
+ * especially useful for determining whether directory entry represents a
35
+ * file or a directory. For more information, see
36
+ * http://www.delorie.com/gnu/docs/glibc/libc_270.html
37
+ *
38
+ * Aug 11, 2010, Toni Ronkko
39
+ * Improved conformance to the standards. For example, errno is now set
40
+ * properly on failure and assert() is never used. Thanks to Peter Brockam
41
+ * for suggestions.
42
+ *
43
+ * Aug 11, 2010, Toni Ronkko
44
+ * Fixed a bug in rewinddir(): when using relative directory names, change
45
+ * of working directory no longer causes rewinddir() to fail.
46
+ *
2547
* Dec 15, 2009, John Cunningham
2648
* Added rewinddir member function
2749
*
2850
* Jan 18, 2008, Toni Ronkko
2951
* Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
@@ -56,42 +78,121 @@
5678
* First version.
5779
*****************************************************************************/
5880
#ifndef DIRENT_H
5981
#define DIRENT_H
6082
83
+#define WIN32_LEAN_AND_MEAN
6184
#include <windows.h>
6285
#include <string.h>
63
-#include <assert.h>
86
+#include <stdlib.h>
87
+#include <sys/types.h>
88
+#include <sys/stat.h>
89
+#include <errno.h>
90
+
91
+/* Entries missing from MSVC 6.0 */
92
+#if !defined(FILE_ATTRIBUTE_DEVICE)
93
+# define FILE_ATTRIBUTE_DEVICE 0x40
94
+#endif
95
+
96
+/* File type and permission flags for stat() */
97
+#if defined(_MSC_VER) && !defined(S_IREAD)
98
+# define S_IFMT _S_IFMT /* file type mask */
99
+# define S_IFDIR _S_IFDIR /* directory */
100
+# define S_IFCHR _S_IFCHR /* character device */
101
+# define S_IFFIFO _S_IFFIFO /* pipe */
102
+# define S_IFREG _S_IFREG /* regular file */
103
+# define S_IREAD _S_IREAD /* read permission */
104
+# define S_IWRITE _S_IWRITE /* write permission */
105
+# define S_IEXEC _S_IEXEC /* execute permission */
106
+#endif
107
+#define S_IFBLK 0 /* block device */
108
+#define S_IFLNK 0 /* link */
109
+#define S_IFSOCK 0 /* socket */
110
+
111
+#if defined(_MSC_VER)
112
+# define S_IRUSR S_IREAD /* read, user */
113
+# define S_IWUSR S_IWRITE /* write, user */
114
+# define S_IXUSR 0 /* execute, user */
115
+# define S_IRGRP 0 /* read, group */
116
+# define S_IWGRP 0 /* write, group */
117
+# define S_IXGRP 0 /* execute, group */
118
+# define S_IROTH 0 /* read, others */
119
+# define S_IWOTH 0 /* write, others */
120
+# define S_IXOTH 0 /* execute, others */
121
+#endif
122
+
123
+/* Indicates that d_type field is available in dirent structure */
124
+#define _DIRENT_HAVE_D_TYPE
125
+
126
+/* File type flags for d_type */
127
+#define DT_UNKNOWN 0
128
+#define DT_REG S_IFREG
129
+#define DT_DIR S_IFDIR
130
+#define DT_FIFO S_IFFIFO
131
+#define DT_SOCK S_IFSOCK
132
+#define DT_CHR S_IFCHR
133
+#define DT_BLK S_IFBLK
134
+
135
+/* Macros for converting between st_mode and d_type */
136
+#define IFTODT(mode) ((mode) & S_IFMT)
137
+#define DTTOIF(type) (type)
138
+
139
+/*
140
+ * File type macros. Note that block devices, sockets and links cannot be
141
+ * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
142
+ * only defined for compatibility. These macros should always return false
143
+ * on Windows.
144
+ */
145
+#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
146
+#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
147
+#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
148
+#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
149
+#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
150
+#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
151
+#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
152
+
153
+#ifdef __cplusplus
154
+extern "C" {
155
+#endif
64156
65157
66158
typedef struct _wdirent
67159
{
68
- wchar_t d_name[MAX_PATH + 1]; /* current dir entry (unicode char string) */
69
- WIN32_FIND_DATAW data; /* file attributes */
70
-} _wdirent;
160
+ wchar_t d_name[MAX_PATH + 1]; /* File name */
161
+ size_t d_namlen; /* Length of name without \0 */
162
+ int d_type; /* File type */
163
+} _wdirent;
71164
72165
73166
typedef struct _WDIR
74167
{
75
- _wdirent current; /* Current directory entry */
76
- int cached; /* Indicates un-processed entry in memory */
77
- HANDLE search_handle; /* File search handle */
78
- wchar_t patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
168
+ _wdirent curentry; /* Current directory entry */
169
+ WIN32_FIND_DATAW find_data; /* Private file data */
170
+ int cached; /* True if data is valid */
171
+ HANDLE search_handle; /* Win32 search handle */
172
+ wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
79173
} _WDIR;
80174
81175
82176
/* Forward declarations */
83
-static _WDIR *_wopendir (const wchar_t *dirname);
84
-static struct _wdirent *_wreaddir (_WDIR *dirp);
85
-static int _wclosedir (_WDIR *dirp);
177
+static _WDIR *_wopendir(const wchar_t *dirname);
178
+static struct _wdirent *_wreaddir(_WDIR *dirp);
179
+static int _wclosedir(_WDIR *dirp);
86180
87181
88182
/* Use the new safe string functions introduced in Visual Studio 2005 */
89183
#if defined(_MSC_VER) && _MSC_VER >= 1400
90
-# define STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
184
+# define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
185
+#else
186
+# define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
187
+#endif
188
+
189
+/* Set errno variable */
190
+#if defined(_MSC_VER)
191
+#define DIRENT_SET_ERRNO(x) _set_errno (x)
91192
#else
92
-# define STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
193
+#define DIRENT_SET_ERRNO(x) (errno = (x))
93194
#endif
94195
95196
96197
/*****************************************************************************
97198
* Open directory stream DIRNAME for read and return a pointer to the
@@ -99,40 +200,63 @@
99200
* entries.
100201
*/
101202
static _WDIR *_wopendir(const wchar_t *dirname)
102203
{
103204
_WDIR *dirp;
104
- assert (dirname != NULL);
105
- assert (wcslen (dirname) < MAX_PATH);
205
+
206
+ /* ensure that the resulting search pattern will be a valid file name */
207
+ if (dirname == NULL) {
208
+ DIRENT_SET_ERRNO (ENOENT);
209
+ return NULL;
210
+ }
211
+ if (wcslen (dirname) + 3 >= MAX_PATH) {
212
+ DIRENT_SET_ERRNO (ENAMETOOLONG);
213
+ return NULL;
214
+ }
106215
107
- /* construct new _WDIR structure */
216
+ /* construct new DIR structure */
108217
dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
109218
if (dirp != NULL) {
110
- wchar_t *p;
111
-
112
- /* take directory name... */
113
- STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
114
- dirp->patt[MAX_PATH] = '\0';
115
-
116
- /* ... and append search pattern to it */
117
- p = wcschr (dirp->patt, '\0');
118
- if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
119
- *p++ = '\\';
120
- }
121
- *p++ = '*';
122
- *p = '\0';
123
-
124
- /* open stream and retrieve first file */
125
- dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->current.data);
126
- if (dirp->search_handle == INVALID_HANDLE_VALUE) {
127
- /* invalid search pattern? */
128
- free (dirp);
129
- return NULL;
130
- }
131
-
132
- /* there is an un-processed directory entry in memory now */
133
- dirp->cached = 1;
219
+ int error;
220
+
221
+ /*
222
+ * Convert relative directory name to an absolute one. This
223
+ * allows rewinddir() to function correctly when the current working
224
+ * directory is changed between opendir() and rewinddir().
225
+ */
226
+ if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
227
+ wchar_t *p;
228
+
229
+ /* append the search pattern "\\*\0" to the directory name */
230
+ p = wcschr (dirp->patt, '\0');
231
+ if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
232
+ *p++ = '\\';
233
+ }
234
+ *p++ = '*';
235
+ *p = '\0';
236
+
237
+ /* open directory stream and retrieve the first entry */
238
+ dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
239
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
240
+ /* a directory entry is now waiting in memory */
241
+ dirp->cached = 1;
242
+ error = 0;
243
+ } else {
244
+ /* search pattern is not a directory name? */
245
+ DIRENT_SET_ERRNO (ENOENT);
246
+ error = 1;
247
+ }
248
+ } else {
249
+ /* buffer too small */
250
+ DIRENT_SET_ERRNO (ENOMEM);
251
+ error = 1;
252
+ }
253
+
254
+ if (error) {
255
+ free (dirp);
256
+ dirp = NULL;
257
+ }
134258
}
135259
136260
return dirp;
137261
}
138262
@@ -144,38 +268,53 @@
144268
* sub-directories, pseudo-directories "." and "..", but also volume labels,
145269
* hidden files and system files may be returned.
146270
*/
147271
static struct _wdirent *_wreaddir(_WDIR *dirp)
148272
{
149
- assert (dirp != NULL);
150
-
151
- if (dirp->search_handle == INVALID_HANDLE_VALUE) {
152
- /* directory stream was opened/rewound incorrectly or ended normally */
273
+ DWORD attr;
274
+ if (dirp == NULL) {
275
+ /* directory stream did not open */
276
+ DIRENT_SET_ERRNO (EBADF);
153277
return NULL;
154278
}
155279
156280
/* get next directory entry */
157281
if (dirp->cached != 0) {
158282
/* a valid directory entry already in memory */
159283
dirp->cached = 0;
160284
} else {
161
- /* read next directory entry from disk */
162
- if (FindNextFileW (dirp->search_handle, &dirp->current.data) == FALSE) {
163
- /* the very last file has been processed or an error occured */
285
+ /* get the next directory entry from stream */
286
+ if (dirp->search_handle == INVALID_HANDLE_VALUE) {
287
+ return NULL;
288
+ }
289
+ if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
290
+ /* the very last entry has been processed or an error occured */
164291
FindClose (dirp->search_handle);
165292
dirp->search_handle = INVALID_HANDLE_VALUE;
166293
return NULL;
167294
}
168295
}
169296
170
- /* copy as a multibyte character string */
171
- STRNCPY ( dirp->current.d_name,
172
- dirp->current.data.cFileName,
173
- sizeof(dirp->current.d_name) );
174
- dirp->current.d_name[MAX_PATH] = '\0';
297
+ /* copy as a unicode character string */
298
+ DIRENT_STRNCPY ( dirp->curentry.d_name,
299
+ dirp->find_data.cFileName,
300
+ sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
301
+ dirp->curentry.d_name[MAX_PATH] = '\0';
302
+
303
+ /* compute the length of name */
304
+ dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
175305
176
- return &dirp->current;
306
+ /* determine file type */
307
+ attr = dirp->find_data.dwFileAttributes;
308
+ if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
309
+ dirp->curentry.d_type = DT_CHR;
310
+ } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
311
+ dirp->curentry.d_type = DT_DIR;
312
+ } else {
313
+ dirp->curentry.d_type = DT_REG;
314
+ }
315
+ return &dirp->curentry;
177316
}
178317
179318
180319
/*****************************************************************************
181320
* Close directory stream opened by opendir() function. Close of the
@@ -182,19 +321,27 @@
182321
* directory stream invalidates the DIR structure as well as any previously
183322
* read directory entry.
184323
*/
185324
static int _wclosedir(_WDIR *dirp)
186325
{
187
- assert (dirp != NULL);
326
+ if (dirp == NULL) {
327
+ /* invalid directory stream */
328
+ DIRENT_SET_ERRNO (EBADF);
329
+ return -1;
330
+ }
188331
189332
/* release search handle */
190333
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
191334
FindClose (dirp->search_handle);
192335
dirp->search_handle = INVALID_HANDLE_VALUE;
193336
}
194337
195
- /* release directory handle */
338
+ /* release directory structure */
196339
free (dirp);
197340
return 0;
198341
}
199342
343
+
344
+#ifdef __cplusplus
345
+}
346
+#endif
200347
#endif /*DIRENT_H*/
201348
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,10 +20,32 @@
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 * Dec 15, 2009, John Cunningham
26 * Added rewinddir member function
27 *
28 * Jan 18, 2008, Toni Ronkko
29 * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
@@ -56,42 +78,121 @@
56 * First version.
57 *****************************************************************************/
58 #ifndef DIRENT_H
59 #define DIRENT_H
60
 
61 #include <windows.h>
62 #include <string.h>
63 #include <assert.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
65
66 typedef struct _wdirent
67 {
68 wchar_t d_name[MAX_PATH + 1]; /* current dir entry (unicode char string) */
69 WIN32_FIND_DATAW data; /* file attributes */
70 } _wdirent;
 
71
72
73 typedef struct _WDIR
74 {
75 _wdirent current; /* Current directory entry */
76 int cached; /* Indicates un-processed entry in memory */
77 HANDLE search_handle; /* File search handle */
78 wchar_t patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
 
79 } _WDIR;
80
81
82 /* Forward declarations */
83 static _WDIR *_wopendir (const wchar_t *dirname);
84 static struct _wdirent *_wreaddir (_WDIR *dirp);
85 static int _wclosedir (_WDIR *dirp);
86
87
88 /* Use the new safe string functions introduced in Visual Studio 2005 */
89 #if defined(_MSC_VER) && _MSC_VER >= 1400
90 # define STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
 
 
 
 
 
 
 
91 #else
92 # define STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
93 #endif
94
95
96 /*****************************************************************************
97 * Open directory stream DIRNAME for read and return a pointer to the
@@ -99,40 +200,63 @@
99 * entries.
100 */
101 static _WDIR *_wopendir(const wchar_t *dirname)
102 {
103 _WDIR *dirp;
104 assert (dirname != NULL);
105 assert (wcslen (dirname) < MAX_PATH);
 
 
 
 
 
 
 
 
106
107 /* construct new _WDIR structure */
108 dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
109 if (dirp != NULL) {
110 wchar_t *p;
111
112 /* take directory name... */
113 STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
114 dirp->patt[MAX_PATH] = '\0';
115
116 /* ... and append search pattern to it */
117 p = wcschr (dirp->patt, '\0');
118 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
119 *p++ = '\\';
120 }
121 *p++ = '*';
122 *p = '\0';
123
124 /* open stream and retrieve first file */
125 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->current.data);
126 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
127 /* invalid search pattern? */
128 free (dirp);
129 return NULL;
130 }
131
132 /* there is an un-processed directory entry in memory now */
133 dirp->cached = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134 }
135
136 return dirp;
137 }
138
@@ -144,38 +268,53 @@
144 * sub-directories, pseudo-directories "." and "..", but also volume labels,
145 * hidden files and system files may be returned.
146 */
147 static struct _wdirent *_wreaddir(_WDIR *dirp)
148 {
149 assert (dirp != NULL);
150
151 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
152 /* directory stream was opened/rewound incorrectly or ended normally */
153 return NULL;
154 }
155
156 /* get next directory entry */
157 if (dirp->cached != 0) {
158 /* a valid directory entry already in memory */
159 dirp->cached = 0;
160 } else {
161 /* read next directory entry from disk */
162 if (FindNextFileW (dirp->search_handle, &dirp->current.data) == FALSE) {
163 /* the very last file has been processed or an error occured */
 
 
 
164 FindClose (dirp->search_handle);
165 dirp->search_handle = INVALID_HANDLE_VALUE;
166 return NULL;
167 }
168 }
169
170 /* copy as a multibyte character string */
171 STRNCPY ( dirp->current.d_name,
172 dirp->current.data.cFileName,
173 sizeof(dirp->current.d_name) );
174 dirp->current.d_name[MAX_PATH] = '\0';
 
 
 
175
176 return &dirp->current;
 
 
 
 
 
 
 
 
 
177 }
178
179
180 /*****************************************************************************
181 * Close directory stream opened by opendir() function. Close of the
@@ -182,19 +321,27 @@
182 * directory stream invalidates the DIR structure as well as any previously
183 * read directory entry.
184 */
185 static int _wclosedir(_WDIR *dirp)
186 {
187 assert (dirp != NULL);
 
 
 
 
188
189 /* release search handle */
190 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
191 FindClose (dirp->search_handle);
192 dirp->search_handle = INVALID_HANDLE_VALUE;
193 }
194
195 /* release directory handle */
196 free (dirp);
197 return 0;
198 }
199
 
 
 
 
200 #endif /*DIRENT_H*/
201
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,10 +20,32 @@
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 * Aug 30, 2012, Jan Nijtmans
26 * Remove rewinddir() (not necessary for fossil)
27 * Replace everything with its wide-character variant.
28 *
29 * Mar 15, 2011, Toni Ronkko
30 * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
31 *
32 * Aug 11, 2010, Toni Ronkko
33 * Added d_type and d_namlen fields to dirent structure. The former is
34 * especially useful for determining whether directory entry represents a
35 * file or a directory. For more information, see
36 * http://www.delorie.com/gnu/docs/glibc/libc_270.html
37 *
38 * Aug 11, 2010, Toni Ronkko
39 * Improved conformance to the standards. For example, errno is now set
40 * properly on failure and assert() is never used. Thanks to Peter Brockam
41 * for suggestions.
42 *
43 * Aug 11, 2010, Toni Ronkko
44 * Fixed a bug in rewinddir(): when using relative directory names, change
45 * of working directory no longer causes rewinddir() to fail.
46 *
47 * Dec 15, 2009, John Cunningham
48 * Added rewinddir member function
49 *
50 * Jan 18, 2008, Toni Ronkko
51 * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
@@ -56,42 +78,121 @@
78 * First version.
79 *****************************************************************************/
80 #ifndef DIRENT_H
81 #define DIRENT_H
82
83 #define WIN32_LEAN_AND_MEAN
84 #include <windows.h>
85 #include <string.h>
86 #include <stdlib.h>
87 #include <sys/types.h>
88 #include <sys/stat.h>
89 #include <errno.h>
90
91 /* Entries missing from MSVC 6.0 */
92 #if !defined(FILE_ATTRIBUTE_DEVICE)
93 # define FILE_ATTRIBUTE_DEVICE 0x40
94 #endif
95
96 /* File type and permission flags for stat() */
97 #if defined(_MSC_VER) && !defined(S_IREAD)
98 # define S_IFMT _S_IFMT /* file type mask */
99 # define S_IFDIR _S_IFDIR /* directory */
100 # define S_IFCHR _S_IFCHR /* character device */
101 # define S_IFFIFO _S_IFFIFO /* pipe */
102 # define S_IFREG _S_IFREG /* regular file */
103 # define S_IREAD _S_IREAD /* read permission */
104 # define S_IWRITE _S_IWRITE /* write permission */
105 # define S_IEXEC _S_IEXEC /* execute permission */
106 #endif
107 #define S_IFBLK 0 /* block device */
108 #define S_IFLNK 0 /* link */
109 #define S_IFSOCK 0 /* socket */
110
111 #if defined(_MSC_VER)
112 # define S_IRUSR S_IREAD /* read, user */
113 # define S_IWUSR S_IWRITE /* write, user */
114 # define S_IXUSR 0 /* execute, user */
115 # define S_IRGRP 0 /* read, group */
116 # define S_IWGRP 0 /* write, group */
117 # define S_IXGRP 0 /* execute, group */
118 # define S_IROTH 0 /* read, others */
119 # define S_IWOTH 0 /* write, others */
120 # define S_IXOTH 0 /* execute, others */
121 #endif
122
123 /* Indicates that d_type field is available in dirent structure */
124 #define _DIRENT_HAVE_D_TYPE
125
126 /* File type flags for d_type */
127 #define DT_UNKNOWN 0
128 #define DT_REG S_IFREG
129 #define DT_DIR S_IFDIR
130 #define DT_FIFO S_IFFIFO
131 #define DT_SOCK S_IFSOCK
132 #define DT_CHR S_IFCHR
133 #define DT_BLK S_IFBLK
134
135 /* Macros for converting between st_mode and d_type */
136 #define IFTODT(mode) ((mode) & S_IFMT)
137 #define DTTOIF(type) (type)
138
139 /*
140 * File type macros. Note that block devices, sockets and links cannot be
141 * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
142 * only defined for compatibility. These macros should always return false
143 * on Windows.
144 */
145 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
146 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
147 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
148 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
149 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
150 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
151 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
152
153 #ifdef __cplusplus
154 extern "C" {
155 #endif
156
157
158 typedef struct _wdirent
159 {
160 wchar_t d_name[MAX_PATH + 1]; /* File name */
161 size_t d_namlen; /* Length of name without \0 */
162 int d_type; /* File type */
163 } _wdirent;
164
165
166 typedef struct _WDIR
167 {
168 _wdirent curentry; /* Current directory entry */
169 WIN32_FIND_DATAW find_data; /* Private file data */
170 int cached; /* True if data is valid */
171 HANDLE search_handle; /* Win32 search handle */
172 wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
173 } _WDIR;
174
175
176 /* Forward declarations */
177 static _WDIR *_wopendir(const wchar_t *dirname);
178 static struct _wdirent *_wreaddir(_WDIR *dirp);
179 static int _wclosedir(_WDIR *dirp);
180
181
182 /* Use the new safe string functions introduced in Visual Studio 2005 */
183 #if defined(_MSC_VER) && _MSC_VER >= 1400
184 # define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
185 #else
186 # define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
187 #endif
188
189 /* Set errno variable */
190 #if defined(_MSC_VER)
191 #define DIRENT_SET_ERRNO(x) _set_errno (x)
192 #else
193 #define DIRENT_SET_ERRNO(x) (errno = (x))
194 #endif
195
196
197 /*****************************************************************************
198 * Open directory stream DIRNAME for read and return a pointer to the
@@ -99,40 +200,63 @@
200 * entries.
201 */
202 static _WDIR *_wopendir(const wchar_t *dirname)
203 {
204 _WDIR *dirp;
205
206 /* ensure that the resulting search pattern will be a valid file name */
207 if (dirname == NULL) {
208 DIRENT_SET_ERRNO (ENOENT);
209 return NULL;
210 }
211 if (wcslen (dirname) + 3 >= MAX_PATH) {
212 DIRENT_SET_ERRNO (ENAMETOOLONG);
213 return NULL;
214 }
215
216 /* construct new DIR structure */
217 dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
218 if (dirp != NULL) {
219 int error;
220
221 /*
222 * Convert relative directory name to an absolute one. This
223 * allows rewinddir() to function correctly when the current working
224 * directory is changed between opendir() and rewinddir().
225 */
226 if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
227 wchar_t *p;
228
229 /* append the search pattern "\\*\0" to the directory name */
230 p = wcschr (dirp->patt, '\0');
231 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
232 *p++ = '\\';
233 }
234 *p++ = '*';
235 *p = '\0';
236
237 /* open directory stream and retrieve the first entry */
238 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
239 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
240 /* a directory entry is now waiting in memory */
241 dirp->cached = 1;
242 error = 0;
243 } else {
244 /* search pattern is not a directory name? */
245 DIRENT_SET_ERRNO (ENOENT);
246 error = 1;
247 }
248 } else {
249 /* buffer too small */
250 DIRENT_SET_ERRNO (ENOMEM);
251 error = 1;
252 }
253
254 if (error) {
255 free (dirp);
256 dirp = NULL;
257 }
258 }
259
260 return dirp;
261 }
262
@@ -144,38 +268,53 @@
268 * sub-directories, pseudo-directories "." and "..", but also volume labels,
269 * hidden files and system files may be returned.
270 */
271 static struct _wdirent *_wreaddir(_WDIR *dirp)
272 {
273 DWORD attr;
274 if (dirp == NULL) {
275 /* directory stream did not open */
276 DIRENT_SET_ERRNO (EBADF);
277 return NULL;
278 }
279
280 /* get next directory entry */
281 if (dirp->cached != 0) {
282 /* a valid directory entry already in memory */
283 dirp->cached = 0;
284 } else {
285 /* get the next directory entry from stream */
286 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
287 return NULL;
288 }
289 if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
290 /* the very last entry has been processed or an error occured */
291 FindClose (dirp->search_handle);
292 dirp->search_handle = INVALID_HANDLE_VALUE;
293 return NULL;
294 }
295 }
296
297 /* copy as a unicode character string */
298 DIRENT_STRNCPY ( dirp->curentry.d_name,
299 dirp->find_data.cFileName,
300 sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
301 dirp->curentry.d_name[MAX_PATH] = '\0';
302
303 /* compute the length of name */
304 dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
305
306 /* determine file type */
307 attr = dirp->find_data.dwFileAttributes;
308 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
309 dirp->curentry.d_type = DT_CHR;
310 } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
311 dirp->curentry.d_type = DT_DIR;
312 } else {
313 dirp->curentry.d_type = DT_REG;
314 }
315 return &dirp->curentry;
316 }
317
318
319 /*****************************************************************************
320 * Close directory stream opened by opendir() function. Close of the
@@ -182,19 +321,27 @@
321 * directory stream invalidates the DIR structure as well as any previously
322 * read directory entry.
323 */
324 static int _wclosedir(_WDIR *dirp)
325 {
326 if (dirp == NULL) {
327 /* invalid directory stream */
328 DIRENT_SET_ERRNO (EBADF);
329 return -1;
330 }
331
332 /* release search handle */
333 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
334 FindClose (dirp->search_handle);
335 dirp->search_handle = INVALID_HANDLE_VALUE;
336 }
337
338 /* release directory structure */
339 free (dirp);
340 return 0;
341 }
342
343
344 #ifdef __cplusplus
345 }
346 #endif
347 #endif /*DIRENT_H*/
348

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button