Browse code

win32 fixes

git-svn: trunk@2189

Tomasz Kojm authored on 2006/08/12 01:11:18
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Aug 11 18:09:00 CEST 2006 (tk)
2
+----------------------------------
3
+  * libclamav/others.c: apply win32 support patch from NJH
4
+
1 5
 Fri Aug 11 16:09:46 CEST 2006 (tk)
2 6
 ----------------------------------
3 7
   * libclamav/vba_extract.c: revert incorrect patch from Sat Aug  5 21:10:32
... ...
@@ -27,19 +27,35 @@
27 27
 #include <string.h>
28 28
 #include <stdlib.h>
29 29
 #include <ctype.h>
30
+#ifdef	HAVE_UNISTD_H
30 31
 #include <unistd.h>
32
+#endif
31 33
 #include <sys/types.h>
32 34
 #include <sys/stat.h>
35
+#ifndef	C_WINDOWS
33 36
 #include <sys/wait.h>
34 37
 #include <sys/time.h>
35 38
 #include <dirent.h>
39
+#endif
36 40
 #include <time.h>
37 41
 #include <fcntl.h>
42
+#ifndef	C_WINDOWS
38 43
 #include <pwd.h>
44
+#endif
39 45
 #include <errno.h>
40
-#include <target.h>
46
+#include "target.h"
47
+#ifndef	C_WINDOWS
41 48
 #include <sys/time.h>
49
+#endif
50
+#ifdef	HAVE_SYS_PARAM_H
42 51
 #include <sys/param.h>
52
+#endif
53
+#ifdef	HAVE_MALLOC_H
54
+#include <malloc.h>
55
+#endif
56
+#if	defined(_MSC_VER) && defined(_DEBUG)
57
+#include <crtdbg.h>
58
+#endif
43 59
 
44 60
 #ifdef CL_THREAD_SAFE
45 61
 #  include <pthread.h>
... ...
@@ -114,7 +130,7 @@ void cli_dbgmsg(const char *str, ...)
114 114
     if(cli_debug_flag) {
115 115
 	    va_list args;
116 116
 	    int sz = sizeof("LibClamAV debug: ") - 1;
117
-	    char buff[256];
117
+	    char buff[BUFSIZ];
118 118
 
119 119
 	memcpy(buff, "LibClamAV debug: ", sz);
120 120
 	va_start(args, str);
... ...
@@ -315,7 +331,11 @@ void *cli_malloc(size_t size)
315 315
 	return NULL;
316 316
     }
317 317
 
318
+#if defined(_MSC_VER) && defined(_DEBUG)
319
+    alloc = _malloc_dbg(size, _NORMAL_BLOCK, __FILE__, __LINE__);
320
+#else
318 321
     alloc = malloc(size);
322
+#endif
319 323
 
320 324
     if(!alloc) {
321 325
 	cli_errmsg("cli_malloc(): Can't allocate memory (%u bytes).\n", size);
... ...
@@ -335,7 +355,11 @@ void *cli_calloc(size_t nmemb, size_t size)
335 335
 	return NULL;
336 336
     }
337 337
 
338
+#if defined(_MSC_VER) && defined(_DEBUG)
339
+    alloc = _calloc_dbg(nmemb, size, _NORMAL_BLOCK, __FILE__, __LINE__);
340
+#else
338 341
     alloc = calloc(nmemb, size);
342
+#endif
339 343
 
340 344
     if(!alloc) {
341 345
 	cli_errmsg("cli_calloc(): Can't allocate memory (%u bytes).\n", nmemb * size);
... ...
@@ -436,7 +460,11 @@ static char *cli_gentempname(const char *dir)
436 436
 	return NULL;
437 437
     }
438 438
 
439
-    sprintf(name, "%s/clamav-", mdir);
439
+#ifdef	C_WINDOWS
440
+	sprintf(name, "%s\\clamav-", mdir);
441
+#else
442
+	sprintf(name, "%s/clamav-", mdir);
443
+#endif
440 444
     strncat(name, tmp, 32);
441 445
     free(tmp);
442 446
 
... ...
@@ -498,6 +526,80 @@ char *cli_gentempstream(const char *dir, FILE **fs)
498 498
     return(name);
499 499
 }
500 500
 
501
+#ifdef	C_WINDOWS
502
+/*
503
+ * Windows doesn't allow you to delete a directory while it is still open
504
+ */
505
+int
506
+cli_rmdirs(const char *name)
507
+{
508
+	int rc;
509
+	struct stat statb;	
510
+	DIR *dd;
511
+	struct dirent *dent;
512
+#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
513
+	union {
514
+	    struct dirent d;
515
+	    char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
516
+	} result;
517
+#endif
518
+
519
+
520
+    if(stat(name, &statb) < 0) {
521
+	cli_warnmsg("Can't locate %s: %s\n", name, strerror(errno));
522
+	return -1;
523
+    }
524
+
525
+    if(!S_ISDIR(statb.st_mode)) {
526
+	if(unlink(name) < 0) {
527
+	    cli_warnmsg("Can't remove %s: %s\n", name, strerror(errno));
528
+	    return -1;
529
+	}
530
+	return 0;
531
+    }
532
+
533
+    if((dd = opendir(name)) == NULL)
534
+	return -1;
535
+
536
+    rc = 0;
537
+
538
+#ifdef HAVE_READDIR_R_3
539
+    while((readdir_r(dd, &result.d, &dent) == 0) && dent) {
540
+#elif defined(HAVE_READDIR_R_2)
541
+    while((dent = (struct dirent *)readdir_r(dd, &result.d)) != NULL) {
542
+#else
543
+    while((dent = readdir(dd)) != NULL) {
544
+#endif
545
+	    char *fname;
546
+
547
+	if(strcmp(dent->d_name, ".") == 0)
548
+	    continue;
549
+	if(strcmp(dent->d_name, "..") == 0)
550
+	    continue;
551
+
552
+	fname = cli_calloc(strlen(name) + strlen(dent->d_name) + 2, sizeof(char));
553
+	if(fname == NULL) {
554
+	    closedir(dd);
555
+	    return -1;
556
+	}
557
+
558
+	sprintf(fname, "%s\\%s", name, dent->d_name);
559
+	rc = cli_rmdirs(fname);
560
+	free(fname);
561
+	if(rc != 0)
562
+	    break;
563
+    }
564
+
565
+    closedir(dd);
566
+
567
+    if(rmdir(name) < 0) {
568
+	cli_errmsg("Can't remove temporary directory %s: %s\n", name, strerror(errno));
569
+	return -1;
570
+    }
571
+
572
+    return rc;	
573
+}
574
+#else
501 575
 int cli_rmdirs(const char *dirname)
502 576
 {
503 577
 	DIR *dd;
... ...
@@ -529,13 +631,22 @@ int cli_rmdirs(const char *dirname)
529 529
 #else
530 530
 	    while((dent = readdir(dd))) {
531 531
 #endif
532
-#ifndef C_INTERIX
532
+#if	(!defined(C_CYGWIN)) && (!defined(C_INTERIX)) && (!defined(C_WINDOWS))
533 533
 		if(dent->d_ino)
534 534
 #endif
535 535
 		{
536 536
 		    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
537 537
 			fname = cli_calloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
538
+			if(!fname) {
539
+			    closedir(dd);
540
+			    return -1;
541
+			}
542
+
543
+#ifdef	C_WINDOWS
544
+			sprintf(fname, "%s\\%s", dirname, dent->d_name);
545
+#else
538 546
 			sprintf(fname, "%s/%s", dirname, dent->d_name);
547
+#endif
539 548
 
540 549
 			/* stat the file */
541 550
 			if(lstat(fname, &statbuf) != -1) {
... ...
@@ -550,7 +661,8 @@ int cli_rmdirs(const char *dirname)
550 550
 				    cli_rmdirs(fname);
551 551
 				}
552 552
 			    } else
553
-				unlink(fname);
553
+				if(unlink(fname) < 0)
554
+				    cli_warnmsg("Couldn't remove %s: %s\n", fname, strerror(errno));
554 555
 			}
555 556
 
556 557
 			free(fname);
... ...
@@ -559,16 +671,16 @@ int cli_rmdirs(const char *dirname)
559 559
 	    }
560 560
 
561 561
 	    rewinddir(dd);
562
-
563 562
 	}
564 563
 
565 564
     } else { 
566
-	return 53;
565
+	return -1;
567 566
     }
568 567
 
569 568
     closedir(dd);
570 569
     return 0;
571 570
 }
571
+#endif
572 572
 
573 573
 /* Function: readn
574 574
         Try hard to read the requested number of bytes