Fossil SCM

re-enable unicode commandline for MSVC build (previous commit accidently removed that) <p>New version of dirent.h, which supports both MBCS and UNICODE

jan.nijtmans 2012-09-13 07:12 UTC trunk
Commit 71d46aba2eab32f3183e80488b3d5bdee43aa219
--- 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) $(MSCDEF) $(SSL) $(INCL)
908
+TCC = $(CC) -c $(CFLAGS) -DUNICODE -D_UNICODE $(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) $(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) -DUNICODE -D_UNICODE $(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) $(MSCDEF) $(SSL) $(INCL)
39
+TCC = $(CC) -c $(CFLAGS) -DUNICODE -D_UNICODE $(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) $(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) -DUNICODE -D_UNICODE $(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/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +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
- * Aug 30, 2012, Jan Nijtmans
26
- * Remove rewinddir() (not necessary for fossil)
27
- * Replace everything with its wide-character variant.
25
+ * Sept 12, 2012, Jan Nijtmans
26
+ * Switchable wide-character variant.
2827
*
2928
* Mar 15, 2011, Toni Ronkko
3029
* Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
3130
*
3231
* Aug 11, 2010, Toni Ronkko
@@ -85,10 +84,11 @@
8584
#include <string.h>
8685
#include <stdlib.h>
8786
#include <sys/types.h>
8887
#include <sys/stat.h>
8988
#include <errno.h>
89
+#include <tchar.h>
9090
9191
/* Entries missing from MSVC 6.0 */
9292
#if !defined(FILE_ATTRIBUTE_DEVICE)
9393
# define FILE_ATTRIBUTE_DEVICE 0x40
9494
#endif
@@ -147,45 +147,54 @@
147147
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
148148
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
149149
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
150150
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
151151
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
152
+
153
+#ifdef UNICODE
154
+# define dirent _wdirent
155
+# define opendir _wopendir
156
+# define readdir _wreaddir
157
+# define closedir _wclosedir
158
+# define rewinddir _wrewinddir
159
+# define DIR _WDIR
160
+#endif
152161
153162
#ifdef __cplusplus
154163
extern "C" {
155164
#endif
156165
157
-
158
-typedef struct _wdirent
166
+typedef struct dirent
159167
{
160
- wchar_t d_name[MAX_PATH + 1]; /* File name */
168
+ TCHAR d_name[MAX_PATH + 1]; /* File name */
161169
size_t d_namlen; /* Length of name without \0 */
162170
int d_type; /* File type */
163
-} _wdirent;
171
+} dirent;
164172
165173
166
-typedef struct _WDIR
174
+typedef struct DIR
167175
{
168
- _wdirent curentry; /* Current directory entry */
169
- WIN32_FIND_DATAW find_data; /* Private file data */
176
+ dirent curentry; /* Current directory entry */
177
+ WIN32_FIND_DATA find_data; /* Private file data */
170178
int cached; /* True if data is valid */
171179
HANDLE search_handle; /* Win32 search handle */
172
- wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
173
-} _WDIR;
180
+ TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
181
+} DIR;
174182
175183
176184
/* Forward declarations */
177
-static _WDIR *_wopendir(const wchar_t *dirname);
178
-static struct _wdirent *_wreaddir(_WDIR *dirp);
179
-static int _wclosedir(_WDIR *dirp);
185
+static DIR *opendir(const TCHAR *dirname);
186
+static struct dirent *readdir(DIR *dirp);
187
+static int closedir(DIR *dirp);
188
+static void rewinddir(DIR* dirp);
180189
181190
182191
/* Use the new safe string functions introduced in Visual Studio 2005 */
183192
#if defined(_MSC_VER) && _MSC_VER >= 1400
184
-# define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
193
+# define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
185194
#else
186
-# define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
195
+# define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
187196
#endif
188197
189198
/* Set errno variable */
190199
#if defined(_MSC_VER)
191200
#define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -197,47 +206,47 @@
197206
/*****************************************************************************
198207
* Open directory stream DIRNAME for read and return a pointer to the
199208
* internal working area that is used to retrieve individual directory
200209
* entries.
201210
*/
202
-static _WDIR *_wopendir(const wchar_t *dirname)
211
+static DIR *opendir(const TCHAR *dirname)
203212
{
204
- _WDIR *dirp;
213
+ DIR *dirp;
205214
206215
/* ensure that the resulting search pattern will be a valid file name */
207216
if (dirname == NULL) {
208217
DIRENT_SET_ERRNO (ENOENT);
209218
return NULL;
210219
}
211
- if (wcslen (dirname) + 3 >= MAX_PATH) {
220
+ if (_tcslen (dirname) + 3 >= MAX_PATH) {
212221
DIRENT_SET_ERRNO (ENAMETOOLONG);
213222
return NULL;
214223
}
215224
216225
/* construct new DIR structure */
217
- dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
226
+ dirp = (DIR*) malloc (sizeof (struct DIR));
218227
if (dirp != NULL) {
219228
int error;
220229
221230
/*
222231
* Convert relative directory name to an absolute one. This
223232
* allows rewinddir() to function correctly when the current working
224233
* directory is changed between opendir() and rewinddir().
225234
*/
226
- if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
227
- wchar_t *p;
235
+ if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
236
+ TCHAR *p;
228237
229238
/* append the search pattern "\\*\0" to the directory name */
230
- p = wcschr (dirp->patt, '\0');
239
+ p = _tcschr (dirp->patt, '\0');
231240
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
232241
*p++ = '\\';
233242
}
234243
*p++ = '*';
235244
*p = '\0';
236245
237246
/* open directory stream and retrieve the first entry */
238
- dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
247
+ dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
239248
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
240249
/* a directory entry is now waiting in memory */
241250
dirp->cached = 1;
242251
error = 0;
243252
} else {
@@ -266,11 +275,11 @@
266275
* containing the name of the entry in d_name field. Individual directory
267276
* entries returned by this very function include regular files,
268277
* sub-directories, pseudo-directories "." and "..", but also volume labels,
269278
* hidden files and system files may be returned.
270279
*/
271
-static struct _wdirent *_wreaddir(_WDIR *dirp)
280
+static struct dirent *readdir(DIR *dirp)
272281
{
273282
DWORD attr;
274283
if (dirp == NULL) {
275284
/* directory stream did not open */
276285
DIRENT_SET_ERRNO (EBADF);
@@ -284,26 +293,26 @@
284293
} else {
285294
/* get the next directory entry from stream */
286295
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
287296
return NULL;
288297
}
289
- if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
298
+ if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
290299
/* the very last entry has been processed or an error occured */
291300
FindClose (dirp->search_handle);
292301
dirp->search_handle = INVALID_HANDLE_VALUE;
293302
return NULL;
294303
}
295304
}
296305
297
- /* copy as a unicode character string */
306
+ /* copy as a multibyte/unicode character string */
298307
DIRENT_STRNCPY ( dirp->curentry.d_name,
299308
dirp->find_data.cFileName,
300
- sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
309
+ sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
301310
dirp->curentry.d_name[MAX_PATH] = '\0';
302311
303312
/* compute the length of name */
304
- dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
313
+ dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
305314
306315
/* determine file type */
307316
attr = dirp->find_data.dwFileAttributes;
308317
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
309318
dirp->curentry.d_type = DT_CHR;
@@ -319,11 +328,11 @@
319328
/*****************************************************************************
320329
* Close directory stream opened by opendir() function. Close of the
321330
* directory stream invalidates the DIR structure as well as any previously
322331
* read directory entry.
323332
*/
324
-static int _wclosedir(_WDIR *dirp)
333
+static int closedir(DIR *dirp)
325334
{
326335
if (dirp == NULL) {
327336
/* invalid directory stream */
328337
DIRENT_SET_ERRNO (EBADF);
329338
return -1;
@@ -338,10 +347,44 @@
338347
/* release directory structure */
339348
free (dirp);
340349
return 0;
341350
}
342351
352
+/*****************************************************************************
353
+ * Resets the position of the directory stream to which dirp refers to the
354
+ * beginning of the directory. It also causes the directory stream to refer
355
+ * to the current state of the corresponding directory, as a call to opendir()
356
+ * would have done. If dirp does not refer to a directory stream, the effect
357
+ * is undefined.
358
+ */
359
+static void rewinddir(DIR* dirp)
360
+{
361
+ if (dirp != NULL) {
362
+ /* release search handle */
363
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
364
+ FindClose (dirp->search_handle);
365
+ }
366
+
367
+ /* open new search handle and retrieve the first entry */
368
+ dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
369
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
370
+ /* a directory entry is now waiting in memory */
371
+ dirp->cached = 1;
372
+ } else {
373
+ /* failed to re-open directory: no directory entry in memory */
374
+ dirp->cached = 0;
375
+ }
376
+ }
377
+}
378
+
379
+#ifdef UNICODE
380
+# undef dirent
381
+# undef opendir
382
+# undef readdir
383
+# undef closedir
384
+# undef DIR
385
+#endif
343386
344387
#ifdef __cplusplus
345388
}
346389
#endif
347390
#endif /*DIRENT_H*/
348391
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +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 * 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
@@ -85,10 +84,11 @@
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
@@ -147,45 +147,54 @@
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)
@@ -197,47 +206,47 @@
197 /*****************************************************************************
198 * Open directory stream DIRNAME for read and return a pointer to the
199 * internal working area that is used to retrieve individual directory
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 {
@@ -266,11 +275,11 @@
266 * containing the name of the entry in d_name field. Individual directory
267 * entries returned by this very function include regular files,
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);
@@ -284,26 +293,26 @@
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;
@@ -319,11 +328,11 @@
319 /*****************************************************************************
320 * Close directory stream opened by opendir() function. Close of the
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;
@@ -338,10 +347,44 @@
338 /* release directory structure */
339 free (dirp);
340 return 0;
341 }
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
344 #ifdef __cplusplus
345 }
346 #endif
347 #endif /*DIRENT_H*/
348
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +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 12, 2012, Jan Nijtmans
26 * Switchable wide-character variant.
 
27 *
28 * Mar 15, 2011, Toni Ronkko
29 * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
30 *
31 * Aug 11, 2010, Toni Ronkko
@@ -85,10 +84,11 @@
84 #include <string.h>
85 #include <stdlib.h>
86 #include <sys/types.h>
87 #include <sys/stat.h>
88 #include <errno.h>
89 #include <tchar.h>
90
91 /* Entries missing from MSVC 6.0 */
92 #if !defined(FILE_ATTRIBUTE_DEVICE)
93 # define FILE_ATTRIBUTE_DEVICE 0x40
94 #endif
@@ -147,45 +147,54 @@
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 UNICODE
154 # define dirent _wdirent
155 # define opendir _wopendir
156 # define readdir _wreaddir
157 # define closedir _wclosedir
158 # define rewinddir _wrewinddir
159 # define DIR _WDIR
160 #endif
161
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165
166 typedef struct dirent
 
167 {
168 TCHAR d_name[MAX_PATH + 1]; /* File name */
169 size_t d_namlen; /* Length of name without \0 */
170 int d_type; /* File type */
171 } dirent;
172
173
174 typedef struct DIR
175 {
176 dirent curentry; /* Current directory entry */
177 WIN32_FIND_DATA find_data; /* Private file data */
178 int cached; /* True if data is valid */
179 HANDLE search_handle; /* Win32 search handle */
180 TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
181 } DIR;
182
183
184 /* Forward declarations */
185 static DIR *opendir(const TCHAR *dirname);
186 static struct dirent *readdir(DIR *dirp);
187 static int closedir(DIR *dirp);
188 static void rewinddir(DIR* dirp);
189
190
191 /* Use the new safe string functions introduced in Visual Studio 2005 */
192 #if defined(_MSC_VER) && _MSC_VER >= 1400
193 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
194 #else
195 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
196 #endif
197
198 /* Set errno variable */
199 #if defined(_MSC_VER)
200 #define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -197,47 +206,47 @@
206 /*****************************************************************************
207 * Open directory stream DIRNAME for read and return a pointer to the
208 * internal working area that is used to retrieve individual directory
209 * entries.
210 */
211 static DIR *opendir(const TCHAR *dirname)
212 {
213 DIR *dirp;
214
215 /* ensure that the resulting search pattern will be a valid file name */
216 if (dirname == NULL) {
217 DIRENT_SET_ERRNO (ENOENT);
218 return NULL;
219 }
220 if (_tcslen (dirname) + 3 >= MAX_PATH) {
221 DIRENT_SET_ERRNO (ENAMETOOLONG);
222 return NULL;
223 }
224
225 /* construct new DIR structure */
226 dirp = (DIR*) malloc (sizeof (struct DIR));
227 if (dirp != NULL) {
228 int error;
229
230 /*
231 * Convert relative directory name to an absolute one. This
232 * allows rewinddir() to function correctly when the current working
233 * directory is changed between opendir() and rewinddir().
234 */
235 if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
236 TCHAR *p;
237
238 /* append the search pattern "\\*\0" to the directory name */
239 p = _tcschr (dirp->patt, '\0');
240 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
241 *p++ = '\\';
242 }
243 *p++ = '*';
244 *p = '\0';
245
246 /* open directory stream and retrieve the first entry */
247 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
248 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
249 /* a directory entry is now waiting in memory */
250 dirp->cached = 1;
251 error = 0;
252 } else {
@@ -266,11 +275,11 @@
275 * containing the name of the entry in d_name field. Individual directory
276 * entries returned by this very function include regular files,
277 * sub-directories, pseudo-directories "." and "..", but also volume labels,
278 * hidden files and system files may be returned.
279 */
280 static struct dirent *readdir(DIR *dirp)
281 {
282 DWORD attr;
283 if (dirp == NULL) {
284 /* directory stream did not open */
285 DIRENT_SET_ERRNO (EBADF);
@@ -284,26 +293,26 @@
293 } else {
294 /* get the next directory entry from stream */
295 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
296 return NULL;
297 }
298 if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
299 /* the very last entry has been processed or an error occured */
300 FindClose (dirp->search_handle);
301 dirp->search_handle = INVALID_HANDLE_VALUE;
302 return NULL;
303 }
304 }
305
306 /* copy as a multibyte/unicode character string */
307 DIRENT_STRNCPY ( dirp->curentry.d_name,
308 dirp->find_data.cFileName,
309 sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
310 dirp->curentry.d_name[MAX_PATH] = '\0';
311
312 /* compute the length of name */
313 dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
314
315 /* determine file type */
316 attr = dirp->find_data.dwFileAttributes;
317 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
318 dirp->curentry.d_type = DT_CHR;
@@ -319,11 +328,11 @@
328 /*****************************************************************************
329 * Close directory stream opened by opendir() function. Close of the
330 * directory stream invalidates the DIR structure as well as any previously
331 * read directory entry.
332 */
333 static int closedir(DIR *dirp)
334 {
335 if (dirp == NULL) {
336 /* invalid directory stream */
337 DIRENT_SET_ERRNO (EBADF);
338 return -1;
@@ -338,10 +347,44 @@
347 /* release directory structure */
348 free (dirp);
349 return 0;
350 }
351
352 /*****************************************************************************
353 * Resets the position of the directory stream to which dirp refers to the
354 * beginning of the directory. It also causes the directory stream to refer
355 * to the current state of the corresponding directory, as a call to opendir()
356 * would have done. If dirp does not refer to a directory stream, the effect
357 * is undefined.
358 */
359 static void rewinddir(DIR* dirp)
360 {
361 if (dirp != NULL) {
362 /* release search handle */
363 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
364 FindClose (dirp->search_handle);
365 }
366
367 /* open new search handle and retrieve the first entry */
368 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
369 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
370 /* a directory entry is now waiting in memory */
371 dirp->cached = 1;
372 } else {
373 /* failed to re-open directory: no directory entry in memory */
374 dirp->cached = 0;
375 }
376 }
377 }
378
379 #ifdef UNICODE
380 # undef dirent
381 # undef opendir
382 # undef readdir
383 # undef closedir
384 # undef DIR
385 #endif
386
387 #ifdef __cplusplus
388 }
389 #endif
390 #endif /*DIRENT_H*/
391

Keyboard Shortcuts

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