Browse code

win32 dirent

aCaB authored on 2009/10/13 06:35:32
Showing 5 changed files
... ...
@@ -216,7 +216,7 @@
216 216
 /* #undef HAVE_INTTYPES_H */
217 217
 
218 218
 /* in_addr_t is defined */
219
-#define HAVE_IN_ADDR_T 1
219
+/* #define HAVE_IN_ADDR_T */
220 220
 
221 221
 /* in_port_t is defined */
222 222
 #define HAVE_IN_PORT_T 1
... ...
@@ -346,7 +346,7 @@
346 346
 #define HAVE_STDLIB_H 1
347 347
 
348 348
 /* Define to 1 if you have the `strcasestr' function. */
349
-#define HAVE_STRCASESTR 1
349
+/* #define HAVE_STRCASESTR */
350 350
 
351 351
 /* Define to 1 if you have the `strerror_r' function. */
352 352
 #define HAVE_STRERROR_R 1
... ...
@@ -18,9 +18,77 @@
18 18
  *  MA 02110-1301, USA.
19 19
  */
20 20
 
21
+#include <errno.h>
22
+#include "others.h"
21 23
 #include "dirent.h"
24
+#include "shared/misc.h"
22 25
 
23 26
 DIR *opendir(const char *name) {
27
+	DIR *d;
28
+	DWORD attrs;
24 29
 
30
+	if(!(d = cli_malloc(sizeof(*d)))) {
31
+		errno = ENOMEM;
32
+		return NULL;
33
+	}
34
+	if(!(MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, d->entry, sizeof(d->entry) / sizeof(d->entry[0])))) {
35
+		free(d);
36
+		errno = (GetLastError() == ERROR_INSUFFICIENT_BUFFER) ? ENAMETOOLONG : ENOENT;
37
+		return NULL;
38
+	}
39
+	/* FIXME: this should be UNC'd */
40
+	if((attrs = GetFileAttributesW(d->entry)) == INVALID_FILE_ATTRIBUTES) {
41
+		free(d);
42
+		errno = ENOENT;
43
+		return NULL;
44
+	}
45
+	if(!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
46
+		free(d);
47
+		errno = ENOTDIR;
48
+		return NULL;
49
+	}
50
+	d->dh = INVALID_HANDLE_VALUE;
51
+	return d;
52
+}
25 53
 
54
+struct dirent *readdir(DIR *dirp) {
55
+	while(1) {
56
+		BOOL cant_convert;
57
+		if(dirp->dh == INVALID_HANDLE_VALUE) {
58
+			if((dirp->dh = FindFirstFileW(dirp->entry, &dirp->wfd)) == INVALID_HANDLE_VALUE) {
59
+				errno = ENOENT;
60
+				return NULL;
61
+			}
62
+		} else {
63
+			if(!(FindNextFileW(dirp->dh, &dirp->wfd))) {
64
+				errno = (GetLastError() == ERROR_NO_MORE_FILES) ? 0 : ENOENT;
65
+				return NULL;
66
+			}
67
+		}
68
+		if(!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, dirp->wfd.cFileName, -1, dirp->ent.d_name, sizeof(dirp->ent.d_name), NULL, &cant_convert) ||
69
+			cant_convert || 
70
+			!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, dirp->wfd.cAlternateFileName, -1, dirp->ent.d_name, sizeof(dirp->ent.d_name), NULL, &cant_convert) ||
71
+			cant_convert ||
72
+			!dirp->ent.d_name[0]) {
73
+			/* FIXME: WARN HERE ! */
74
+			continue;
75
+		}
76
+		dirp->ent.d_ino = dirp->wfd.ftCreationTime.dwLowDateTime ^ dirp->wfd.nFileSizeLow;
77
+		if(!dirp->ent.d_ino) dirp->ent.d_ino = 0x1337;
78
+		dirp->ent.d_type = (dirp->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
79
+		break;
80
+	}
81
+	return &dirp->ent;
82
+}
83
+
84
+void rewinddir(DIR *dirp) {
85
+	if(dirp->dh != INVALID_HANDLE_VALUE)
86
+		FindClose(dirp->dh);
87
+	dirp->dh = INVALID_HANDLE_VALUE;
88
+}
89
+
90
+int closedir(DIR *dirp) {
91
+	rewinddir(dirp);
92
+	free(dirp);
93
+	return 0;
26 94
 }
27 95
\ No newline at end of file
... ...
@@ -25,19 +25,21 @@
25 25
 #include "clamav-config.h"
26 26
 #endif
27 27
 
28
-#define _DIRENT_HAVE_D_TYPE
28
+//#define _DIRENT_HAVE_D_TYPE
29
+
30
+typedef unsigned long ino_t;
29 31
 
30 32
 typedef struct {
31 33
 	HANDLE dh;
32 34
 	WIN32_FIND_DATAW wfd;
33
-	char entry[MAX_PATH];
35
+	struct dirent {
36
+		ino_t d_ino;	/* inode number */
37
+		unsigned char d_type;	/* type of file */
38
+		char d_name[MAX_PATH];	/* filename */
39
+	} ent;
40
+	wchar_t entry[PATH_MAX];
34 41
 } DIR;
35 42
 
36
-struct dirent {
37
-	char d_ino;			/* inode number */
38
-	unsigned char d_type;	/* type of file */
39
-	char d_name[MAX_PATH];	/* filename */
40
-};
41 43
 
42 44
 enum {
43 45
 	DT_BLOCK,
... ...
@@ -41,7 +41,7 @@
41 41
 			<Tool
42 42
 				Name="VCCLCompilerTool"
43 43
 				Optimization="0"
44
-				AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(SolutionDir)..\libclamav&quot;;&quot;$(SolutionDir)\3rdparty\zlib&quot;;&quot;$(SolutionDir)\3rdparty\pthreads&quot;;&quot;$(SolutionDir)\3rdparty\bzip2&quot;;&quot;$(SolutionDir)..&quot;"
44
+				AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(SolutionDir)..\libclamav&quot;;&quot;$(SolutionDir)\compat&quot;;&quot;$(SolutionDir)\3rdparty\zlib&quot;;&quot;$(SolutionDir)\3rdparty\pthreads&quot;;&quot;$(SolutionDir)\3rdparty\bzip2&quot;;&quot;$(SolutionDir)..\libltdl&quot;;&quot;$(SolutionDir)..&quot;"
45 45
 				PreprocessorDefinitions="WIN32_LEAN_AND_MEAN;HAVE_CONFIG_H"
46 46
 				MinimalRebuild="true"
47 47
 				BasicRuntimeChecks="3"
... ...
@@ -117,7 +117,7 @@
117 117
 				Name="VCCLCompilerTool"
118 118
 				Optimization="2"
119 119
 				EnableIntrinsicFunctions="true"
120
-				AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(SolutionDir)..\libclamav&quot;;&quot;$(SolutionDir)\3rdparty\zlib&quot;;&quot;$(SolutionDir)\3rdparty\pthreads&quot;;&quot;$(SolutionDir)\3rdparty\bzip2&quot;;&quot;$(SolutionDir)..&quot;"
120
+				AdditionalIncludeDirectories="&quot;$(SolutionDir)&quot;;&quot;$(SolutionDir)..\libclamav&quot;;&quot;$(SolutionDir)\compat&quot;;&quot;$(SolutionDir)\3rdparty\zlib&quot;;&quot;$(SolutionDir)\3rdparty\pthreads&quot;;&quot;$(SolutionDir)\3rdparty\bzip2&quot;;&quot;$(SolutionDir)..\libltdl&quot;;&quot;$(SolutionDir)..&quot;"
121 121
 				PreprocessorDefinitions="WIN32_LEAN_AND_MEAN;HAVE_CONFIG_H"
122 122
 				RuntimeLibrary="2"
123 123
 				EnableFunctionLevelLinking="true"
... ...
@@ -181,6 +181,42 @@
181 181
 				>
182 182
 			</File>
183 183
 			<File
184
+				RelativePath="..\libclamav\7z\7zBuf.c"
185
+				>
186
+			</File>
187
+			<File
188
+				RelativePath="..\libclamav\7z\7zCrc.c"
189
+				>
190
+			</File>
191
+			<File
192
+				RelativePath="..\libclamav\7z\Archive\7z\7zDecode.c"
193
+				>
194
+			</File>
195
+			<File
196
+				RelativePath="..\libclamav\7z\Archive\7z\7zExtract.c"
197
+				>
198
+			</File>
199
+			<File
200
+				RelativePath="..\libclamav\7z\7zFile.c"
201
+				>
202
+			</File>
203
+			<File
204
+				RelativePath="..\libclamav\7z\Archive\7z\7zHeader.c"
205
+				>
206
+			</File>
207
+			<File
208
+				RelativePath="..\libclamav\7z\Archive\7z\7zIn.c"
209
+				>
210
+			</File>
211
+			<File
212
+				RelativePath="..\libclamav\7z\Archive\7z\7zItem.c"
213
+				>
214
+			</File>
215
+			<File
216
+				RelativePath="..\libclamav\7z\7zStream.c"
217
+				>
218
+			</File>
219
+			<File
184 220
 				RelativePath="..\libclamav\aspack.c"
185 221
 				>
186 222
 			</File>
... ...
@@ -189,6 +225,10 @@
189 189
 				>
190 190
 			</File>
191 191
 			<File
192
+				RelativePath="..\libclamav\7z\Bcj2.c"
193
+				>
194
+			</File>
195
+			<File
192 196
 				RelativePath="..\libclamav\bignum.c"
193 197
 				>
194 198
 			</File>
... ...
@@ -201,6 +241,18 @@
201 201
 				>
202 202
 			</File>
203 203
 			<File
204
+				RelativePath="..\libclamav\7z\Bra.c"
205
+				>
206
+			</File>
207
+			<File
208
+				RelativePath="..\libclamav\7z\Bra86.c"
209
+				>
210
+			</File>
211
+			<File
212
+				RelativePath="..\libclamav\7z\BraIA64.c"
213
+				>
214
+			</File>
215
+			<File
204 216
 				RelativePath="..\libclamav\bytecode.c"
205 217
 				>
206 218
 			</File>
... ...
@@ -237,6 +289,10 @@
237 237
 				>
238 238
 			</File>
239 239
 			<File
240
+				RelativePath=".\compat\dirent.c"
241
+				>
242
+			</File>
243
+			<File
240 244
 				RelativePath="..\libclamav\disasm.c"
241 245
 				>
242 246
 			</File>
... ...
@@ -301,6 +357,10 @@
301 301
 				>
302 302
 			</File>
303 303
 			<File
304
+				RelativePath="..\libclamav\7z\LzmaDec.c"
305
+				>
306
+			</File>
307
+			<File
304 308
 				RelativePath="..\libclamav\macho.c"
305 309
 				>
306 310
 			</File>
... ...
@@ -333,6 +393,10 @@
333 333
 				>
334 334
 			</File>
335 335
 			<File
336
+				RelativePath="..\shared\misc.c"
337
+				>
338
+			</File>
339
+			<File
336 340
 				RelativePath="..\libclamav\mpool.c"
337 341
 				>
338 342
 			</File>
... ...
@@ -507,6 +571,38 @@
507 507
 				>
508 508
 			</File>
509 509
 			<File
510
+				RelativePath="..\libclamav\7z\7zBuf.h"
511
+				>
512
+			</File>
513
+			<File
514
+				RelativePath="..\libclamav\7z\7zCrc.h"
515
+				>
516
+			</File>
517
+			<File
518
+				RelativePath="..\libclamav\7z\Archive\7z\7zDecode.h"
519
+				>
520
+			</File>
521
+			<File
522
+				RelativePath="..\libclamav\7z\Archive\7z\7zExtract.h"
523
+				>
524
+			</File>
525
+			<File
526
+				RelativePath="..\libclamav\7z\7zFile.h"
527
+				>
528
+			</File>
529
+			<File
530
+				RelativePath="..\libclamav\7z\Archive\7z\7zHeader.h"
531
+				>
532
+			</File>
533
+			<File
534
+				RelativePath="..\libclamav\7z\Archive\7z\7zIn.h"
535
+				>
536
+			</File>
537
+			<File
538
+				RelativePath="..\libclamav\7z\Archive\7z\7zItem.h"
539
+				>
540
+			</File>
541
+			<File
510 542
 				RelativePath="..\libclamav\aspack.h"
511 543
 				>
512 544
 			</File>
... ...
@@ -515,6 +611,10 @@
515 515
 				>
516 516
 			</File>
517 517
 			<File
518
+				RelativePath="..\libclamav\7z\Bcj2.h"
519
+				>
520
+			</File>
521
+			<File
518 522
 				RelativePath="..\libclamav\bignum.h"
519 523
 				>
520 524
 			</File>
... ...
@@ -531,6 +631,10 @@
531 531
 				>
532 532
 			</File>
533 533
 			<File
534
+				RelativePath="..\libclamav\7z\Bra.h"
535
+				>
536
+			</File>
537
+			<File
534 538
 				RelativePath="..\libclamav\bytecode.h"
535 539
 				>
536 540
 			</File>
... ...
@@ -567,6 +671,10 @@
567 567
 				>
568 568
 			</File>
569 569
 			<File
570
+				RelativePath="..\libclamav\7z\CpuArch.h"
571
+				>
572
+			</File>
573
+			<File
570 574
 				RelativePath="..\libclamav\cvd.h"
571 575
 				>
572 576
 			</File>
... ...
@@ -679,6 +787,10 @@
679 679
 				>
680 680
 			</File>
681 681
 			<File
682
+				RelativePath="..\libclamav\7z\LzmaDec.h"
683
+				>
684
+			</File>
685
+			<File
682 686
 				RelativePath="..\libclamav\macho.h"
683 687
 				>
684 688
 			</File>
... ...
@@ -711,6 +823,10 @@
711 711
 				>
712 712
 			</File>
713 713
 			<File
714
+				RelativePath="..\shared\misc.h"
715
+				>
716
+			</File>
717
+			<File
714 718
 				RelativePath="..\libclamav\mpool.h"
715 719
 				>
716 720
 			</File>
... ...
@@ -827,6 +943,10 @@
827 827
 				>
828 828
 			</File>
829 829
 			<File
830
+				RelativePath="..\libclamav\7z\Types.h"
831
+				>
832
+			</File>
833
+			<File
830 834
 				RelativePath="..\libclamav\unarj.h"
831 835
 				>
832 836
 			</File>
... ...
@@ -11,8 +11,12 @@
11 11
 typedef int ssize_t;
12 12
 #define strcasecmp lstrcmpi
13 13
 #define strncasecmp strnicmp
14
+
15
+/* FIXME: this one is b0rked */
14 16
 #define snprintf _snprintf
15 17
 
18
+#define PATH_MAX 32767
19
+
16 20
 #define S_IRUSR S_IREAD
17 21
 #define S_IWUSR S_IWRITE
18 22
 #define S_IRWXU (S_IRUSR|S_IWUSR)
... ...
@@ -23,6 +27,8 @@ typedef int ssize_t;
23 23
 #define R_OK 4
24 24
 #define X_OK R_OK
25 25
 
26
+#define SEARCH_LIBDIR "."
27
+
26 28
 #ifndef MIN
27 29
 #define MIN(a, b)	(((a) < (b)) ? (a) : (b))
28 30
 #endif