Fossil SCM

Always use wide-character Win32 APIs for dirent.h.

mistachkin 2012-09-23 23:52 trunk
Commit ea621d75efeadb6ac8e9840b7b7a4c03e54120e6
+6 -15
--- src/file.c
+++ src/file.c
@@ -1008,25 +1008,16 @@
10081008
/*
10091009
** Portable unicode implementation of opendir()
10101010
*/
10111011
#if INTERFACE
10121012
1013
-#if defined(_WIN32)
1014
-# include <dirent.h>
1015
-# define FOSSIL_DIR _WDIR
1016
-# define fossil_dirent _wdirent
1017
-# define fossil_opendir _wopendir
1018
-# define fossil_readdir _wreaddir
1019
-# define fossil_closedir _wclosedir
1020
-#else
1021
-# include <dirent.h>
1022
-# define FOSSIL_DIR DIR
1023
-# define fossil_dirent dirent
1024
-# define fossil_opendir opendir
1025
-# define fossil_readdir readdir
1026
-# define fossil_closedir closedir
1027
-#endif
1013
+#include <dirent.h>
1014
+#define FOSSIL_DIR DIR
1015
+#define fossil_dirent dirent
1016
+#define fossil_opendir opendir
1017
+#define fossil_readdir readdir
1018
+#define fossil_closedir closedir
10281019
10291020
#endif /* INTERFACE */
10301021
10311022
10321023
10331024
--- src/file.c
+++ src/file.c
@@ -1008,25 +1008,16 @@
1008 /*
1009 ** Portable unicode implementation of opendir()
1010 */
1011 #if INTERFACE
1012
1013 #if defined(_WIN32)
1014 # include <dirent.h>
1015 # define FOSSIL_DIR _WDIR
1016 # define fossil_dirent _wdirent
1017 # define fossil_opendir _wopendir
1018 # define fossil_readdir _wreaddir
1019 # define fossil_closedir _wclosedir
1020 #else
1021 # include <dirent.h>
1022 # define FOSSIL_DIR DIR
1023 # define fossil_dirent dirent
1024 # define fossil_opendir opendir
1025 # define fossil_readdir readdir
1026 # define fossil_closedir closedir
1027 #endif
1028
1029 #endif /* INTERFACE */
1030
1031
1032
1033
--- src/file.c
+++ src/file.c
@@ -1008,25 +1008,16 @@
1008 /*
1009 ** Portable unicode implementation of opendir()
1010 */
1011 #if INTERFACE
1012
1013 #include <dirent.h>
1014 #define FOSSIL_DIR DIR
1015 #define fossil_dirent dirent
1016 #define fossil_opendir opendir
1017 #define fossil_readdir readdir
1018 #define fossil_closedir closedir
 
 
 
 
 
 
 
 
 
1019
1020 #endif /* INTERFACE */
1021
1022
1023
1024
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -903,11 +903,11 @@
903903
904904
INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
905905
906906
CFLAGS = -nologo -MT -O2
907907
BCC = $(CC) $(CFLAGS)
908
-TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
908
+TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
909909
LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
910910
LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
911911
}
912912
regsub -all {[-]D} $SQLITE_OPTIONS {/D} MSC_SQLITE_OPTIONS
913913
writeln "SQLITE_OPTIONS = $MSC_SQLITE_OPTIONS\n"
914914
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -903,11 +903,11 @@
903
904 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
905
906 CFLAGS = -nologo -MT -O2
907 BCC = $(CC) $(CFLAGS)
908 TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
909 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
910 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
911 }
912 regsub -all {[-]D} $SQLITE_OPTIONS {/D} MSC_SQLITE_OPTIONS
913 writeln "SQLITE_OPTIONS = $MSC_SQLITE_OPTIONS\n"
914
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -903,11 +903,11 @@
903
904 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
905
906 CFLAGS = -nologo -MT -O2
907 BCC = $(CC) $(CFLAGS)
908 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
909 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
910 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
911 }
912 regsub -all {[-]D} $SQLITE_OPTIONS {/D} MSC_SQLITE_OPTIONS
913 writeln "SQLITE_OPTIONS = $MSC_SQLITE_OPTIONS\n"
914
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
3434
3535
INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
3636
3737
CFLAGS = -nologo -MT -O2
3838
BCC = $(CC) $(CFLAGS)
39
-TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
39
+TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
4040
LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
4141
LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
4242
4343
SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
4444
4545
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
34
35 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
36
37 CFLAGS = -nologo -MT -O2
38 BCC = $(CC) $(CFLAGS)
39 TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
40 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
41 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
42
43 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
44
45
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
34
35 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
36
37 CFLAGS = -nologo -MT -O2
38 BCC = $(CC) $(CFLAGS)
39 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
40 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
41 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
42
43 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
44
45
--- win/Makefile.msc.mistachkin
+++ win/Makefile.msc.mistachkin
@@ -31,11 +31,11 @@
3131
3232
INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(ZINCDIR)
3333
3434
CFLAGS = -nologo -MT -O2
3535
BCC = $(CC) $(CFLAGS)
36
-TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
36
+TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
3737
LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
3838
LIBDIR = -LIBPATH:$(ZLIBDIR)
3939
4040
SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
4141
4242
--- win/Makefile.msc.mistachkin
+++ win/Makefile.msc.mistachkin
@@ -31,11 +31,11 @@
31
32 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(ZINCDIR)
33
34 CFLAGS = -nologo -MT -O2
35 BCC = $(CC) $(CFLAGS)
36 TCC = $(CC) -c $(CFLAGS) -DDIRENT_UNICODE=1 $(MSCDEF) $(SSL) $(INCL)
37 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
38 LIBDIR = -LIBPATH:$(ZLIBDIR)
39
40 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
41
42
--- win/Makefile.msc.mistachkin
+++ win/Makefile.msc.mistachkin
@@ -31,11 +31,11 @@
31
32 INCL = -I. -I$(SRCDIR) -I$B\win\include -I$(ZINCDIR)
33
34 CFLAGS = -nologo -MT -O2
35 BCC = $(CC) $(CFLAGS)
36 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
37 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
38 LIBDIR = -LIBPATH:$(ZLIBDIR)
39
40 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
41
42
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,12 +20,12 @@
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
- * Sept 22, 2012, Joe Mistachkin
26
- * Check for DIRENT_UNICODE define in addition to UNICODE.
25
+ * Sept 23, 2012, Joe Mistachkin
26
+ * Modified to use wide-character APIs.
2727
*
2828
* Sept 12, 2012, Jan Nijtmans
2929
* Switchable wide-character variant.
3030
*
3131
* Mar 15, 2011, Toni Ronkko
@@ -87,11 +87,10 @@
8787
#include <string.h>
8888
#include <stdlib.h>
8989
#include <sys/types.h>
9090
#include <sys/stat.h>
9191
#include <errno.h>
92
-#include <tchar.h>
9392
9493
/* Entries missing from MSVC 6.0 */
9594
#if !defined(FILE_ATTRIBUTE_DEVICE)
9695
# define FILE_ATTRIBUTE_DEVICE 0x40
9796
#endif
@@ -151,53 +150,44 @@
151150
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
152151
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
153152
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
154153
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
155154
156
-#if defined(UNICODE) || defined(DIRENT_UNICODE)
157
-# define dirent _wdirent
158
-# define opendir _wopendir
159
-# define readdir _wreaddir
160
-# define closedir _wclosedir
161
-# define rewinddir _wrewinddir
162
-# define DIR _WDIR
163
-#endif
164
-
165155
#ifdef __cplusplus
166156
extern "C" {
167157
#endif
168158
169159
typedef struct dirent
170160
{
171
- TCHAR d_name[MAX_PATH + 1]; /* File name */
161
+ WCHAR d_name[MAX_PATH + 1]; /* File name */
172162
size_t d_namlen; /* Length of name without \0 */
173163
int d_type; /* File type */
174164
} dirent;
175165
176166
177167
typedef struct DIR
178168
{
179169
dirent curentry; /* Current directory entry */
180
- WIN32_FIND_DATA find_data; /* Private file data */
170
+ WIN32_FIND_DATAW find_data; /* Private file data */
181171
int cached; /* True if data is valid */
182172
HANDLE search_handle; /* Win32 search handle */
183
- TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
173
+ WCHAR patt[MAX_PATH + 3]; /* Initial directory name */
184174
} DIR;
185175
186176
187177
/* Forward declarations */
188
-static DIR *opendir(const TCHAR *dirname);
178
+static DIR *opendir(const WCHAR *dirname);
189179
static struct dirent *readdir(DIR *dirp);
190180
static int closedir(DIR *dirp);
191181
static void rewinddir(DIR* dirp);
192182
193183
194184
/* Use the new safe string functions introduced in Visual Studio 2005 */
195185
#if defined(_MSC_VER) && _MSC_VER >= 1400
196
-# define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
186
+# define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
197187
#else
198
-# define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
188
+# define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
199189
#endif
200190
201191
/* Set errno variable */
202192
#if defined(_MSC_VER)
203193
#define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -209,20 +199,20 @@
209199
/*****************************************************************************
210200
* Open directory stream DIRNAME for read and return a pointer to the
211201
* internal working area that is used to retrieve individual directory
212202
* entries.
213203
*/
214
-static DIR *opendir(const TCHAR *dirname)
204
+static DIR *opendir(const WCHAR *dirname)
215205
{
216206
DIR *dirp;
217207
218208
/* ensure that the resulting search pattern will be a valid file name */
219209
if (dirname == NULL) {
220210
DIRENT_SET_ERRNO (ENOENT);
221211
return NULL;
222212
}
223
- if (_tcslen (dirname) + 3 >= MAX_PATH) {
213
+ if (wcslen (dirname) + 3 >= MAX_PATH) {
224214
DIRENT_SET_ERRNO (ENAMETOOLONG);
225215
return NULL;
226216
}
227217
228218
/* construct new DIR structure */
@@ -233,23 +223,23 @@
233223
/*
234224
* Convert relative directory name to an absolute one. This
235225
* allows rewinddir() to function correctly when the current working
236226
* directory is changed between opendir() and rewinddir().
237227
*/
238
- if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
239
- TCHAR *p;
228
+ if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
229
+ WCHAR *p;
240230
241231
/* append the search pattern "\\*\0" to the directory name */
242
- p = _tcschr (dirp->patt, '\0');
232
+ p = wcschr (dirp->patt, '\0');
243233
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
244234
*p++ = '\\';
245235
}
246236
*p++ = '*';
247237
*p = '\0';
248238
249239
/* open directory stream and retrieve the first entry */
250
- dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
240
+ dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
251241
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
252242
/* a directory entry is now waiting in memory */
253243
dirp->cached = 1;
254244
error = 0;
255245
} else {
@@ -296,11 +286,11 @@
296286
} else {
297287
/* get the next directory entry from stream */
298288
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
299289
return NULL;
300290
}
301
- if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
291
+ if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
302292
/* the very last entry has been processed or an error occured */
303293
FindClose (dirp->search_handle);
304294
dirp->search_handle = INVALID_HANDLE_VALUE;
305295
return NULL;
306296
}
@@ -311,11 +301,11 @@
311301
dirp->find_data.cFileName,
312302
sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
313303
dirp->curentry.d_name[MAX_PATH] = '\0';
314304
315305
/* compute the length of name */
316
- dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
306
+ dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
317307
318308
/* determine file type */
319309
attr = dirp->find_data.dwFileAttributes;
320310
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
321311
dirp->curentry.d_type = DT_CHR;
@@ -366,11 +356,11 @@
366356
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
367357
FindClose (dirp->search_handle);
368358
}
369359
370360
/* open new search handle and retrieve the first entry */
371
- dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
361
+ dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
372362
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
373363
/* a directory entry is now waiting in memory */
374364
dirp->cached = 1;
375365
} else {
376366
/* failed to re-open directory: no directory entry in memory */
@@ -377,17 +367,9 @@
377367
dirp->cached = 0;
378368
}
379369
}
380370
}
381371
382
-#ifdef UNICODE
383
-# undef dirent
384
-# undef opendir
385
-# undef readdir
386
-# undef closedir
387
-# undef DIR
388
-#endif
389
-
390372
#ifdef __cplusplus
391373
}
392374
#endif
393375
#endif /*DIRENT_H*/
394376
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,12 +20,12 @@
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 * Sept 22, 2012, Joe Mistachkin
26 * Check for DIRENT_UNICODE define in addition to UNICODE.
27 *
28 * Sept 12, 2012, Jan Nijtmans
29 * Switchable wide-character variant.
30 *
31 * Mar 15, 2011, Toni Ronkko
@@ -87,11 +87,10 @@
87 #include <string.h>
88 #include <stdlib.h>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <errno.h>
92 #include <tchar.h>
93
94 /* Entries missing from MSVC 6.0 */
95 #if !defined(FILE_ATTRIBUTE_DEVICE)
96 # define FILE_ATTRIBUTE_DEVICE 0x40
97 #endif
@@ -151,53 +150,44 @@
151 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
152 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
153 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
154 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
155
156 #if defined(UNICODE) || defined(DIRENT_UNICODE)
157 # define dirent _wdirent
158 # define opendir _wopendir
159 # define readdir _wreaddir
160 # define closedir _wclosedir
161 # define rewinddir _wrewinddir
162 # define DIR _WDIR
163 #endif
164
165 #ifdef __cplusplus
166 extern "C" {
167 #endif
168
169 typedef struct dirent
170 {
171 TCHAR d_name[MAX_PATH + 1]; /* File name */
172 size_t d_namlen; /* Length of name without \0 */
173 int d_type; /* File type */
174 } dirent;
175
176
177 typedef struct DIR
178 {
179 dirent curentry; /* Current directory entry */
180 WIN32_FIND_DATA find_data; /* Private file data */
181 int cached; /* True if data is valid */
182 HANDLE search_handle; /* Win32 search handle */
183 TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
184 } DIR;
185
186
187 /* Forward declarations */
188 static DIR *opendir(const TCHAR *dirname);
189 static struct dirent *readdir(DIR *dirp);
190 static int closedir(DIR *dirp);
191 static void rewinddir(DIR* dirp);
192
193
194 /* Use the new safe string functions introduced in Visual Studio 2005 */
195 #if defined(_MSC_VER) && _MSC_VER >= 1400
196 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
197 #else
198 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
199 #endif
200
201 /* Set errno variable */
202 #if defined(_MSC_VER)
203 #define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -209,20 +199,20 @@
209 /*****************************************************************************
210 * Open directory stream DIRNAME for read and return a pointer to the
211 * internal working area that is used to retrieve individual directory
212 * entries.
213 */
214 static DIR *opendir(const TCHAR *dirname)
215 {
216 DIR *dirp;
217
218 /* ensure that the resulting search pattern will be a valid file name */
219 if (dirname == NULL) {
220 DIRENT_SET_ERRNO (ENOENT);
221 return NULL;
222 }
223 if (_tcslen (dirname) + 3 >= MAX_PATH) {
224 DIRENT_SET_ERRNO (ENAMETOOLONG);
225 return NULL;
226 }
227
228 /* construct new DIR structure */
@@ -233,23 +223,23 @@
233 /*
234 * Convert relative directory name to an absolute one. This
235 * allows rewinddir() to function correctly when the current working
236 * directory is changed between opendir() and rewinddir().
237 */
238 if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
239 TCHAR *p;
240
241 /* append the search pattern "\\*\0" to the directory name */
242 p = _tcschr (dirp->patt, '\0');
243 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
244 *p++ = '\\';
245 }
246 *p++ = '*';
247 *p = '\0';
248
249 /* open directory stream and retrieve the first entry */
250 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
251 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
252 /* a directory entry is now waiting in memory */
253 dirp->cached = 1;
254 error = 0;
255 } else {
@@ -296,11 +286,11 @@
296 } else {
297 /* get the next directory entry from stream */
298 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
299 return NULL;
300 }
301 if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
302 /* the very last entry has been processed or an error occured */
303 FindClose (dirp->search_handle);
304 dirp->search_handle = INVALID_HANDLE_VALUE;
305 return NULL;
306 }
@@ -311,11 +301,11 @@
311 dirp->find_data.cFileName,
312 sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
313 dirp->curentry.d_name[MAX_PATH] = '\0';
314
315 /* compute the length of name */
316 dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
317
318 /* determine file type */
319 attr = dirp->find_data.dwFileAttributes;
320 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
321 dirp->curentry.d_type = DT_CHR;
@@ -366,11 +356,11 @@
366 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
367 FindClose (dirp->search_handle);
368 }
369
370 /* open new search handle and retrieve the first entry */
371 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
372 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
373 /* a directory entry is now waiting in memory */
374 dirp->cached = 1;
375 } else {
376 /* failed to re-open directory: no directory entry in memory */
@@ -377,17 +367,9 @@
377 dirp->cached = 0;
378 }
379 }
380 }
381
382 #ifdef UNICODE
383 # undef dirent
384 # undef opendir
385 # undef readdir
386 # undef closedir
387 # undef DIR
388 #endif
389
390 #ifdef __cplusplus
391 }
392 #endif
393 #endif /*DIRENT_H*/
394
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,12 +20,12 @@
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 * Sept 23, 2012, Joe Mistachkin
26 * Modified to use wide-character APIs.
27 *
28 * Sept 12, 2012, Jan Nijtmans
29 * Switchable wide-character variant.
30 *
31 * Mar 15, 2011, Toni Ronkko
@@ -87,11 +87,10 @@
87 #include <string.h>
88 #include <stdlib.h>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <errno.h>
 
92
93 /* Entries missing from MSVC 6.0 */
94 #if !defined(FILE_ATTRIBUTE_DEVICE)
95 # define FILE_ATTRIBUTE_DEVICE 0x40
96 #endif
@@ -151,53 +150,44 @@
150 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
151 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
152 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
153 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
154
 
 
 
 
 
 
 
 
 
155 #ifdef __cplusplus
156 extern "C" {
157 #endif
158
159 typedef struct dirent
160 {
161 WCHAR d_name[MAX_PATH + 1]; /* File name */
162 size_t d_namlen; /* Length of name without \0 */
163 int d_type; /* File type */
164 } dirent;
165
166
167 typedef struct DIR
168 {
169 dirent curentry; /* Current directory entry */
170 WIN32_FIND_DATAW find_data; /* Private file data */
171 int cached; /* True if data is valid */
172 HANDLE search_handle; /* Win32 search handle */
173 WCHAR patt[MAX_PATH + 3]; /* Initial directory name */
174 } DIR;
175
176
177 /* Forward declarations */
178 static DIR *opendir(const WCHAR *dirname);
179 static struct dirent *readdir(DIR *dirp);
180 static int closedir(DIR *dirp);
181 static void rewinddir(DIR* dirp);
182
183
184 /* Use the new safe string functions introduced in Visual Studio 2005 */
185 #if defined(_MSC_VER) && _MSC_VER >= 1400
186 # define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
187 #else
188 # define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
189 #endif
190
191 /* Set errno variable */
192 #if defined(_MSC_VER)
193 #define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -209,20 +199,20 @@
199 /*****************************************************************************
200 * Open directory stream DIRNAME for read and return a pointer to the
201 * internal working area that is used to retrieve individual directory
202 * entries.
203 */
204 static DIR *opendir(const WCHAR *dirname)
205 {
206 DIR *dirp;
207
208 /* ensure that the resulting search pattern will be a valid file name */
209 if (dirname == NULL) {
210 DIRENT_SET_ERRNO (ENOENT);
211 return NULL;
212 }
213 if (wcslen (dirname) + 3 >= MAX_PATH) {
214 DIRENT_SET_ERRNO (ENAMETOOLONG);
215 return NULL;
216 }
217
218 /* construct new DIR structure */
@@ -233,23 +223,23 @@
223 /*
224 * Convert relative directory name to an absolute one. This
225 * allows rewinddir() to function correctly when the current working
226 * directory is changed between opendir() and rewinddir().
227 */
228 if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
229 WCHAR *p;
230
231 /* append the search pattern "\\*\0" to the directory name */
232 p = wcschr (dirp->patt, '\0');
233 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
234 *p++ = '\\';
235 }
236 *p++ = '*';
237 *p = '\0';
238
239 /* open directory stream and retrieve the first entry */
240 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
241 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
242 /* a directory entry is now waiting in memory */
243 dirp->cached = 1;
244 error = 0;
245 } else {
@@ -296,11 +286,11 @@
286 } else {
287 /* get the next directory entry from stream */
288 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
289 return NULL;
290 }
291 if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
292 /* the very last entry has been processed or an error occured */
293 FindClose (dirp->search_handle);
294 dirp->search_handle = INVALID_HANDLE_VALUE;
295 return NULL;
296 }
@@ -311,11 +301,11 @@
301 dirp->find_data.cFileName,
302 sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
303 dirp->curentry.d_name[MAX_PATH] = '\0';
304
305 /* compute the length of name */
306 dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
307
308 /* determine file type */
309 attr = dirp->find_data.dwFileAttributes;
310 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
311 dirp->curentry.d_type = DT_CHR;
@@ -366,11 +356,11 @@
356 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
357 FindClose (dirp->search_handle);
358 }
359
360 /* open new search handle and retrieve the first entry */
361 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
362 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
363 /* a directory entry is now waiting in memory */
364 dirp->cached = 1;
365 } else {
366 /* failed to re-open directory: no directory entry in memory */
@@ -377,17 +367,9 @@
367 dirp->cached = 0;
368 }
369 }
370 }
371
 
 
 
 
 
 
 
 
372 #ifdef __cplusplus
373 }
374 #endif
375 #endif /*DIRENT_H*/
376

Keyboard Shortcuts

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