*** /home/njh/src/clamav-devel/trunk/./clamscan/clamscan.c	2008-02-15 09:45:55.000000000 +0000
--- ./clamscan/clamscan.c	2008-02-15 11:55:10.000000000 +0000
***************
*** 260,265 ****
--- 260,268 ----
  	return 72;
      }
  #endif
+ #ifdef        _DEBUG
+ 	_CrtDumpMemoryLeaks();
+ #endif
  
      return ret;
  }
*** /home/njh/src/clamav-devel/trunk/./clamscan/manager.h	2007-12-06 18:32:26.000000000 +0000
--- ./clamscan/manager.h	2007-04-21 09:44:22.000000000 +0100
***************
*** 34,37 ****
--- 34,42 ----
  
  int scanfile(const char *filename, struct cl_engine *engine, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, unsigned int options);
  
+ #ifdef	_DEBUG
+ /* breaks mspack/qtmd.c :-( */
+ #define	free(p)	_free_dbg(p, _NORMAL_BLOCK)
+ #endif
+ 
  #endif
*** /home/njh/src/clamav-devel/trunk/./clamscan/others.c	2008-02-19 14:17:17.000000000 +0000
--- ./clamscan/others.c	2008-02-19 14:17:56.000000000 +0000
***************
*** 77,83 ****
  /* FIXME: Handle users correctly */
  int checkaccess(const char *path, const char *username, int mode)
  {
!     return !_access(path, mode);
  }
  #else
  int checkaccess(const char *path, const char *username, int mode)
--- 77,83 ----
  /* FIXME: Handle users correctly */
  int checkaccess(const char *path, const char *username, int mode)
  {
! 	return _access(path, mode) == 0;
  }
  #else
  int checkaccess(const char *path, const char *username, int mode)
*** /home/njh/src/clamav-devel/trunk/./freshclam/manager.c	2008-02-25 15:41:07.000000000 +0000
--- ./freshclam/manager.c	2008-02-25 15:43:03.000000000 +0000
***************
*** 49,55 ****
--- 49,57 ----
  #include <time.h>
  #include <fcntl.h>
  #include <sys/stat.h>
+ #ifndef	C_WINDOWS
  #include <dirent.h>
+ #endif
  #include <errno.h>
  #include <zlib.h>
  
***************
*** 212,218 ****
  		break;
  	}
          logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, herr);
! 	close(socketfd);
  	return -1;
      }
  
--- 214,220 ----
  		break;
  	}
          logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, herr);
! 	closesocket(socketfd);
  	return -1;
      }
  
***************
*** 244,250 ****
  	if(connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) == -1) {
  #endif
  	    logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr);
! 	    close(socketfd);
  	    if((socketfd = getclientsock(localip)) == -1)
  		return -1;
  
--- 246,252 ----
  	if(connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) == -1) {
  #endif
  	    logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr);
! 	    closesocket(socketfd);
  	    if((socketfd = getclientsock(localip)) == -1)
  		return -1;
  
***************
*** 255,261 ****
  	}
      }
  
!     close(socketfd);
      return -2;
  }
  
--- 257,263 ----
  	}
      }
  
!     closesocket(socketfd);
      return -2;
  }
  
***************
*** 918,924 ****
      }
  
      while((dent = readdir(dir))) {
! #ifndef C_INTERIX
  	if(dent->d_ino)
  #endif
  	{
--- 920,926 ----
      }
  
      while((dent = readdir(dir))) {
! #if	(!defined(C_INTERIX)) && (!defined(C_WINDOWS))
  	if(dent->d_ino)
  #endif
  	{
*** /home/njh/src/clamav-devel/trunk/./freshclam/freshclam.c	2008-01-23 11:58:35.000000000 +0000
--- ./freshclam/freshclam.c	2008-01-23 11:58:02.000000000 +0000
***************
*** 419,424 ****
--- 419,429 ----
  #endif
  
      /* change the current working directory */
+     if(opt_check(opt, "datadir"))
+ 	newdir = opt_arg(opt, "datadir");
+     else
+ 	newdir = cfgopt(copt, "DatabaseDirectory")->strarg;
+ 
      if(chdir(newdir)) {
  	logg("Can't change dir to %s\n", newdir);
  	opt_free(opt);
***************
*** 493,499 ****
  		opt_free(opt);
  		freecfg(copt);
  		return 70; /* FIXME */
! 	    }
              foreground = 0;
  	    mprintf_disabled = 1;
          }
--- 498,504 ----
  		opt_free(opt);
  		freecfg(copt);
  		return 70; /* FIXME */
! 	   }
              foreground = 0;
  	    mprintf_disabled = 1;
          }
*** /home/njh/src/clamav-devel/trunk/./clamd/scanner.c	2008-02-15 09:45:21.000000000 +0000
--- ./clamd/scanner.c	2008-02-15 09:50:04.000000000 +0000
***************
*** 440,452 ****
      if(!bound && !portscan) {
  	logg("!ScanStream: Can't find any free port.\n");
  	mdprintf(odesc, "Can't find any free port. ERROR\n");
! 	close(sockfd);
  	return -1;
      } else {
  	listen(sockfd, 1);
  	if(mdprintf(odesc, "PORT %u\n", port) <= 0) {
  	    logg("!ScanStream: error transmitting port.\n");
! 	    close(sockfd);
  	    return -1;
  	}
      }
--- 440,452 ----
      if(!bound && !portscan) {
  	logg("!ScanStream: Can't find any free port.\n");
  	mdprintf(odesc, "Can't find any free port. ERROR\n");
! 	closesocket(sockfd);
  	return -1;
      } else {
  	listen(sockfd, 1);
  	if(mdprintf(odesc, "PORT %u\n", port) <= 0) {
  	    logg("!ScanStream: error transmitting port.\n");
! 	    closesocket(sockfd);
  	    return -1;
  	}
      }
***************
*** 455,471 ****
  	case 0: /* timeout */
  	    mdprintf(odesc, "Accept timeout. ERROR\n");
  	    logg("!ScanStream %u: accept timeout.\n", port);
! 	    close(sockfd);
  	    return -1;
  	case -1:
  	    mdprintf(odesc, "Accept poll. ERROR\n");
  	    logg("!ScanStream %u: accept poll failed.\n", port);
! 	    close(sockfd);
  	    return -1;
      }
  
      if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
! 	close(sockfd);
  	mdprintf(odesc, "accept() ERROR\n");
  	logg("!ScanStream %u: accept() failed.\n", port);
  	return -1;
--- 455,471 ----
  	case 0: /* timeout */
  	    mdprintf(odesc, "Accept timeout. ERROR\n");
  	    logg("!ScanStream %u: accept timeout.\n", port);
! 	    closesocket(sockfd);
  	    return -1;
  	case -1:
  	    mdprintf(odesc, "Accept poll. ERROR\n");
  	    logg("!ScanStream %u: accept poll failed.\n", port);
! 	    closesocket(sockfd);
  	    return -1;
      }
  
      if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
! 	closesocket(sockfd);
  	mdprintf(odesc, "accept() ERROR\n");
  	logg("!ScanStream %u: accept() failed.\n", port);
  	return -1;
***************
*** 475,482 ****
  
      if(cli_gentempfd(NULL, &tmpname, &tmpd)) {
  	shutdown(sockfd, 2);
! 	close(sockfd);
! 	close(acceptd);
  	mdprintf(odesc, "cli_gentempfd() failed. ERROR\n");
  	logg("!ScanStream %u: Can't create temporary file.\n", port);
  	return -1;
--- 475,482 ----
  
      if(cli_gentempfd(NULL, &tmpname, &tmpd)) {
  	shutdown(sockfd, 2);
! 	closesocket(sockfd);
! 	closesocket(acceptd);
  	mdprintf(odesc, "cli_gentempfd() failed. ERROR\n");
  	logg("!ScanStream %u: Can't create temporary file.\n", port);
  	return -1;
*** /home/njh/src/clamav-devel/trunk/./clamd/clamd.c	2008-02-18 16:35:00.000000000 +0000
--- ./clamd/clamd.c	2008-02-18 16:37:26.000000000 +0000
***************
*** 35,41 ****
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <time.h>
! #ifndef	C_WINDOWS
  #include <pwd.h>
  #include <grp.h>
  #endif
--- 35,43 ----
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <time.h>
! #ifdef	C_WINDOWS
! #include <direct.h>	/* for chdir */
! #else
  #include <pwd.h>
  #include <grp.h>
  #endif
***************
*** 65,70 ****
--- 67,75 ----
  #include "others.h"
  #include "shared.h"
  
+ #ifndef	C_WINDOWS
+ #define	closesocket(s)	close(s)
+ #endif
  
  short debug_mode = 0, logok = 0;
  short foreground = 0;
***************
*** 86,92 ****
--- 91,99 ----
  {
  	struct cfgstruct *copt;
  	const struct cfgstruct *cpt;
+ #ifndef	C_WINDOWS
          struct passwd *user = NULL;
+ #endif
  	time_t currtime;
  	struct cl_engine *engine = NULL;
  	const char *dbdir, *cfgfile;
***************
*** 281,288 ****
--- 288,297 ----
  
      logg("#clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
  
+ #ifndef	C_WINDOWS
      if(user)
  	logg("#Running as user %s (UID %u, GID %u)\n", user->pw_name, user->pw_uid, user->pw_gid);
+ #endif
  
      if(logg_size)
  	logg("#Log file size limited to %d bytes.\n", logg_size);
***************
*** 366,372 ****
  	    logg_close();
  	    freecfg(copt);
  	    if(tcpsock)
! 		close(lsockets[0]);
  	    return 1;
  	}
  	nlsockets++;
--- 375,381 ----
  	    logg_close();
  	    freecfg(copt);
  	    if(tcpsock)
! 		closesocket(lsockets[0]);
  	    return 1;
  	}
  	nlsockets++;
*** /home/njh/src/clamav-devel/trunk/./clamd/server-th.c	2008-02-25 15:44:06.000000000 +0000
--- ./clamd/server-th.c	2008-02-25 15:45:37.000000000 +0000
***************
*** 573,579 ****
  		client_conn->socketds = socketds;
  		client_conn->nsockets = nsockets;
  		if (!thrmgr_dispatch(thr_pool, client_conn)) {
! 		    close(client_conn->sd);
  		    free(client_conn);
  		    logg("!thread dispatch failed\n");
  		}
--- 573,579 ----
  		client_conn->socketds = socketds;
  		client_conn->nsockets = nsockets;
  		if (!thrmgr_dispatch(thr_pool, client_conn)) {
! 		    closesocket(client_conn->sd);
  		    free(client_conn);
  		    logg("!thread dispatch failed\n");
  		}
***************
*** 581,589 ****
  
  	pthread_mutex_lock(&exit_mutex);
  	if(progexit) {
! 	    if (new_sd >= 0) {
! 		close(new_sd);
! 	    }
  	    pthread_mutex_unlock(&exit_mutex);
  	    break;
  	}
--- 581,592 ----
  
  	pthread_mutex_lock(&exit_mutex);
  	if(progexit) {
! #ifdef	C_WINDOWS
! 	    closesocket(new_sd);
! #else
!   	    if (new_sd >= 0)
! 		closesocket(new_sd);
! #endif
  	    pthread_mutex_unlock(&exit_mutex);
  	    break;
  	}
***************
*** 607,614 ****
  	    engine = reload_db(engine, dboptions, copt, FALSE, &ret);
  	    if(ret) {
  		logg("Terminating because of a fatal error.\n");
! 		if(new_sd >= 0)
! 		    close(new_sd);
  		break;
  	    }
  	    pthread_mutex_lock(&reload_mutex);
--- 610,621 ----
  	    engine = reload_db(engine, dboptions, copt, FALSE, &ret);
  	    if(ret) {
  		logg("Terminating because of a fatal error.\n");
! #ifdef	C_WINDOWS
! 		closesocket(new_sd);
! #else
! 		if (new_sd >= 0)
! 			closesocket(new_sd);
! #endif
  		break;
  	    }
  	    pthread_mutex_lock(&reload_mutex);
*** /home/njh/src/clamav-devel/trunk/./clamd/tcpserver.c	2008-02-18 16:35:00.000000000 +0000
--- ./clamd/tcpserver.c	2008-02-18 16:37:46.000000000 +0000
***************
*** 51,56 ****
--- 51,60 ----
  #include "server.h"
  #include "tcpserver.h"
  
+ #ifndef	C_WINDOWS
+ #define	closesocket(s)	close(s)
+ #endif
+ 
  int tcpserver(const struct cfgstruct *copt)
  {
  	struct sockaddr_in server;
***************
*** 87,93 ****
      if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
  	estr = strerror(errno);
  	logg("!TCP: bind() error: %s\n", estr);
! 	close(sockfd);
  	return -1;
      } else {
  	if(taddr->enabled)
--- 91,97 ----
      if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
  	estr = strerror(errno);
  	logg("!TCP: bind() error: %s\n", estr);
! 	closesocket(sockfd);
  	return -1;
      } else {
  	if(taddr->enabled)
***************
*** 102,108 ****
      if(listen(sockfd, backlog) == -1) {
  	estr = strerror(errno);
  	logg("!TCP: listen() error: %s\n", estr);
! 	close(sockfd);
  	return -1;
      }
  
--- 106,112 ----
      if(listen(sockfd, backlog) == -1) {
  	estr = strerror(errno);
  	logg("!TCP: listen() error: %s\n", estr);
! 	closesocket(sockfd);
  	return -1;
      }
  
*** /home/njh/src/clamav-devel/trunk/./clamdscan/client.c	2008-01-23 16:19:16.000000000 +0000
--- ./clamdscan/client.c	2008-02-25 15:57:20.000000000 +0000
***************
*** 15,36 ****
--- 15,44 ----
   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   *  MA 02110-1301, USA.
   */
+ #ifdef	_MSC_VER
+ #include <winsock.h>
+ #endif
+ 
  
  #if HAVE_CONFIG_H
  #include "clamav-config.h"
  #endif
  
  #include <stdio.h>
+ #ifdef	HAVE_UNISTD_H
  #include <unistd.h>
+ #endif
  #include <string.h>
  #include <sys/types.h>
  #include <sys/stat.h>
+ #ifndef	C_WINDOWS
  #include <sys/socket.h>
  #include <sys/un.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <netdb.h>
  #include <utime.h>
+ #endif
  #include <errno.h>
  
  #ifdef HAVE_SYS_UIO_H
***************
*** 52,60 ****
--- 60,137 ----
  # define SOCKET_INET	AF_INET
  #endif
  
+ #ifndef	C_WINDOWS
+ #define	closesocket(s)	close(s)
+ #endif
+ 
+ /* #define ENABLE_FD_PASSING	    FIXME: Doesn't work yet */
+ 
  void move_infected(const char *filename, const struct optstruct *opt);
  int notremoved = 0, notmoved = 0;
  
+ #ifdef	C_WINDOWS
+ static  int     get_a_line(int sockd, char *buf, size_t len);
+ 
+ static int
+ dsresult(int sockd, const struct optstruct *opt)
+ {
+ 	char buff[BUFSIZ], *pt;
+ 	int infected = 0, waserror = 0;
+ 
+ 	while(get_a_line(sockd, buff, sizeof(buff))) {
+ 		if(strstr(buff, "FOUND\n")) {
+ 			infected++;
+ 			logg("%s", buff);
+ 			if(opt_check(opt, "move") || opt_check(opt, "copy")) {
+ 				/* filename: Virus FOUND */
+ 				if((pt = strrchr(buff, ':'))) {
+ 					*pt = 0;
+ 					move_infected(buff, opt);
+ 				} else
+ 					mprintf("@Broken data format. File not %s.\n", opt_check(opt, "move") ? "moved" : "copied");
+ 			} else if(opt_check(opt, "remove")) {
+ 				if(!(pt = strrchr(buff, ':')))
+ 					mprintf("@Broken data format. File not removed.\n");
+ 				else {
+ 					*pt = 0;
+ 					if(unlink(buff)) {
+ 						mprintf("~%s: Can't remove.\n", buff);
+ 						logg("~%s: Can't remove.\n", buff);
+ 						notremoved++;
+ 					} else {
+ 						mprintf("~%s: Removed.\n", buff);
+ 						logg("~%s: Removed.\n", buff);
+ 					}
+ 				}
+ 			}
+ 		}
+ 
+ 		if(strstr(buff, "ERROR\n")) {
+ 			logg("%s", buff);
+ 			waserror = 1;
+ 		}
+ 	}
+ 
+ 	return infected ? infected : (waserror ? -1 : 0);
+ }
+ 
+ static int
+ get_a_line(int sockd, char *buf, size_t len)
+ {
+ 	char *ptr;
+ 
+ 	for(ptr = buf; ptr < &buf[len]; ptr++) {
+ 		/* FIXME: very inefficient to call recv so many times */
+ 		if(recv(sockd, ptr, sizeof(char), 0) <= 0)
+ 			return 0;
+ 		if(*ptr == '\n') {
+ 			*++ptr = '\0';
+ 			return 1;
+ 		}
+ 	}
+ 	return 1;
+ }
+ #else
  static int dsresult(int sockd, const struct optstruct *opt)
  {
  	int infected = 0, waserror = 0;
***************
*** 64,71 ****
  
  #ifndef C_OS2
      if((fd = fdopen(dup(sockd), "r")) == NULL) {
! #else /* FIXME: accoriding to YD OS/2 does not support dup() for sockets */
!     if((fd = fdopen(sockd, "r")) == NULL) {
  #endif
  	logg("^Can't open descriptor for reading.\n");
  	return -1;
--- 141,148 ----
  
  #ifndef C_OS2
      if((fd = fdopen(dup(sockd), "r")) == NULL) {
! #else /* FIXME: according to YD OS/2 does not support dup() for sockets */
!     if((fd = fdopen(sockd, "rb")) == NULL) {
  #endif
  	logg("^Can't open descriptor for reading.\n");
  	return -1;
***************
*** 90,101 ****
  		} else {
  		    *pt = 0;
  		    if(unlink(buff)) {
! 			mprintf("~%s: Can't remove.\n", buff);
! 			logg("~%s: Can't remove.\n", buff);
  			notremoved++;
  		    } else {
! 			mprintf("~%s: Removed.\n", buff);
! 			logg("~%s: Removed.\n", buff);
  		    }
  		}
  	    }
--- 167,178 ----
  		} else {
  		    *pt = 0;
  		    if(unlink(buff)) {
! 			mprintf("%s: Can't remove.\n", buff);
! 			logg("%s: Can't remove.\n", buff);
  			notremoved++;
  		    } else {
! 			mprintf("%s: Removed.\n", buff);
! 			logg("%s: Removed.\n", buff);
  		    }
  		}
  	    }
***************
*** 113,118 ****
--- 190,196 ----
  
      return infected ? infected : (waserror ? -1 : 0);
  }
+ #endif	/*C_WINDOWS*/
  
  static int dsfile(int sockd, const char *scantype, const char *filename, const struct optstruct *opt)
  {
***************
*** 123,129 ****
      scancmd = malloc(strlen(filename) + 20);
      sprintf(scancmd, "%s %s", scantype, filename);
  
!     if(write(sockd, scancmd, strlen(scancmd)) <= 0) {
  	logg("^Can't write to the socket.\n");
  	free(scancmd);
  	return -1;
--- 201,207 ----
      scancmd = malloc(strlen(filename) + 20);
      sprintf(scancmd, "%s %s", scantype, filename);
  
!     if(send(sockd, scancmd, strlen(scancmd), 0) <= 0) {
  	logg("^Can't write to the socket.\n");
  	free(scancmd);
  	return -1;
***************
*** 148,161 ****
  	char buff[4096], *pt;
  
  
!     if(write(sockd, "STREAM", 6) <= 0) {
  	logg("^Can't write to the socket.\n");
  	return 2;
      }
  
      while(loopw) {
  	memset(buff, 0, sizeof(buff));
! 	if(read(sockd, buff, sizeof(buff)) > 0) {
  	    if((pt = strstr(buff, "PORT"))) {
  		pt += 5;
  		sscanf(pt, "%d", &port);
--- 226,239 ----
  	char buff[4096], *pt;
  
  
!     if(send(sockd, "STREAM", 6, 0) <= 0) {
  	logg("^Can't write to the socket.\n");
  	return 2;
      }
  
      while(loopw) {
  	memset(buff, 0, sizeof(buff));
! 	if(recv(sockd, buff, sizeof(buff), 0) > 0) {
  	    if((pt = strstr(buff, "PORT"))) {
  		pt += 5;
  		sscanf(pt, "%d", &port);
***************
*** 201,207 ****
      }
  
      if(connect(wsockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
! 	close(wsockd);
  	perror("connect()");
  	logg("^Can't connect to clamd [port: %d].\n", port);
  	return -1;
--- 279,285 ----
      }
  
      if(connect(wsockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
! 	closesocket(wsockd);
  	perror("connect()");
  	logg("^Can't connect to clamd [port: %d].\n", port);
  	return -1;
***************
*** 210,220 ****
      while((bread = read(0, buff, sizeof(buff))) > 0) {
  	if(write(wsockd, buff, bread) <= 0) {
  	    logg("^Can't write to the socket.\n");
! 	    close(wsockd);
  	    return -1;
  	}
      }
!     close(wsockd);
  
      memset(buff, 0, sizeof(buff));
      while((bread = read(sockd, buff, sizeof(buff))) > 0) {
--- 288,298 ----
      while((bread = read(0, buff, sizeof(buff))) > 0) {
  	if(write(wsockd, buff, bread) <= 0) {
  	    logg("^Can't write to the socket.\n");
! 	    closesocket(wsockd);
  	    return -1;
  	}
      }
!     closesocket(wsockd);
  
      memset(buff, 0, sizeof(buff));
      while((bread = read(sockd, buff, sizeof(buff))) > 0) {
***************
*** 259,265 ****
--- 337,345 ----
  
  static int dconnect(const struct optstruct *opt)
  {
+ #ifndef	C_WINDOWS
  	struct sockaddr_un server;
+ #endif
  	struct sockaddr_in server2;
  	struct hostent *he;
  	struct cfgstruct *copt;
***************
*** 276,288 ****
--- 356,374 ----
  	return -1;
      }
  
+ #ifndef	C_WINDOWS
      memset((char *) &server, 0, sizeof(server));
+ #endif
      memset((char *) &server2, 0, sizeof(server2));
  
      /* Set default address to connect to */
      server2.sin_addr.s_addr = inet_addr("127.0.0.1");    
  
      if((cpt = cfgopt(copt, "LocalSocket"))->enabled) {
+ #ifdef	C_WINDOWS
+ 	logg("^LocalSocket is not supported under Windows");
+ 	return -1;
+ #else
  
  	server.sun_family = AF_UNIX;
  	strncpy(server.sun_path, cpt->strarg, sizeof(server.sun_path));
***************
*** 301,308 ****
  	    freecfg(copt);
  	    return -1;
  	}
  
!     } else if((cpt = cfgopt(copt, "TCPSocket"))->enabled) {
  
  	if((sockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
  	    perror("socket()");
--- 387,404 ----
  	    freecfg(copt);
  	    return -1;
  	}
+ #endif
+   
+ 	} else if((cpt = cfgopt(copt, "TCPSocket"))->enabled) {
+ #ifdef  C_WINDOWS
+ 	WSADATA wsaData;
  
! 	if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
! 		logg("!Error at WSAStartup(): %d\n", WSAGetLastError());
! 		return -1;
! 	}
! #endif
!   
  
  	if((sockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
  	    perror("socket()");
***************
*** 316,322 ****
  
  	if((cpt = cfgopt(copt, "TCPAddr"))->enabled) {
  	    if ((he = gethostbyname(cpt->strarg)) == 0) {
! 		close(sockd);
  		perror("gethostbyname()");
  		logg("^Can't lookup clamd hostname.\n");
  		freecfg(copt);
--- 412,418 ----
  
  	if((cpt = cfgopt(copt, "TCPAddr"))->enabled) {
  	    if ((he = gethostbyname(cpt->strarg)) == 0) {
! 		closesocket(sockd);
  		perror("gethostbyname()");
  		logg("^Can't lookup clamd hostname.\n");
  		freecfg(copt);
***************
*** 326,332 ****
  	}
  
  	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
! 	    close(sockd);
  	    perror("connect()");
  	    logg("^Can't connect to clamd.\n");
  	    freecfg(copt);
--- 422,428 ----
  	}
  
  	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
! 	    closesocket(sockd);
  	    perror("connect()");
  	    logg("^Can't connect to clamd.\n");
  	    freecfg(copt);
***************
*** 396,402 ****
  	else
  	    errors++;
  
! 	close(sockd);
  
      } else if(!strcmp(opt->filename, "-")) { /* scan data from stdin */
  	if((sockd = dconnect(opt)) < 0)
--- 492,498 ----
  	else
  	    errors++;
  
! 	closesocket(sockd);
  
      } else if(!strcmp(opt->filename, "-")) { /* scan data from stdin */
  	if((sockd = dconnect(opt)) < 0)
***************
*** 407,413 ****
  	else
  	    errors++;
  
! 	close(sockd);
  
      } else {
  	int x;
--- 503,509 ----
  	else
  	    errors++;
  
! 	closesocket(sockd);
  
      } else {
  	int x;
***************
*** 441,447 ****
  			else
  			    errors++;
  
! 			close(sockd);
  			break;
  
  		    default:
--- 537,543 ----
  			else
  			    errors++;
  
! 			closesocket(sockd);
  			break;
  
  		    default:
***************
*** 464,470 ****
--- 560,568 ----
  	struct stat ofstat, mfstat;
  	int n, len, movefilename_size;
  	int moveflag = opt_check(opt, "move");
+ #ifndef	C_WINDOWS
  	struct utimbuf ubuf;
+ #endif
  
  
      if((moveflag && !(movedir = opt_arg(opt, "move"))) ||
***************
*** 551,559 ****
--- 649,660 ----
  	if(chown(movefilename, ofstat.st_uid, ofstat.st_gid) == -1)
  	    logg("^chown() failed for %s: %s\n", movefilename, strerror(errno));
  
+ 
+ #ifndef	C_WINDOWS
  	ubuf.actime = ofstat.st_atime;
  	ubuf.modtime = ofstat.st_mtime;
  	utime(movefilename, &ubuf);
+ #endif
  
  	if(moveflag && unlink(filename)) {
  	    logg("^cannot unlink '%s': %s\n", filename, strerror(errno));
*** /home/njh/src/clamav-devel/trunk/./clamdscan/clamdscan.c	2008-01-21 20:56:26.000000000 +0000
--- ./clamdscan/clamdscan.c	2008-01-21 20:52:50.000000000 +0000
***************
*** 15,30 ****
   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   *  MA 02110-1301, USA.
   */
  
  #if HAVE_CONFIG_H
  #include "clamav-config.h"
  #endif
- 
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <sys/time.h>
  #include <time.h>
  #include <signal.h>
  
--- 15,42 ----
   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   *  MA 02110-1301, USA.
   */
+  
+ #ifdef	_MSC_VER
+ #include <windows.h>
+ #include <winsock.h>
+ #endif
  
  #if HAVE_CONFIG_H
  #include "clamav-config.h"
  #endif
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
+ #ifdef	HAVE_UNISTD_H
  #include <unistd.h>
+ #endif
+ #ifdef	C_WINDOWS
+ #ifdef	CL_THREAD_SAFE
+ #include <pthread.h>
+ #endif
+ #else
  #include <sys/time.h>
+ #endif
  #include <time.h>
  #include <signal.h>
  
***************
*** 61,67 ****
--- 73,81 ----
  {
  	int ds, dms, ret, infected;
  	struct timeval t1, t2;
+ #ifndef	C_WINDOWS
  	struct timezone tz;
+ #endif
  	time_t starttime;
  	struct optstruct *opt;
  	const char *clamdscan_accepted[] = { "help", "version", "verbose", "quiet",
***************
*** 70,75 ****
--- 84,96 ----
  				  "disable-summary", "multiscan", NULL };
  
  
+ #ifdef	C_WINDOWS
+ 	if(!pthread_win32_process_attach_np()) {
+ 		mprintf("!Can't start the win32 pthreads layer\n");
+ 		return 1;
+ 	}
+ #endif
+ 
      opt = opt_parse(argc, argv, clamscan_shortopt, clamscan_longopt, clamdscan_accepted);
      if(!opt) {
  	mprintf("!Can't parse the command line\n");
***************
*** 117,129 ****
      time(&starttime);
      /* ctime() does \n, but I need it once more */
  
!     gettimeofday(&t1, &tz);
  
      ret = client(opt, &infected);
  
      /* TODO: Implement STATUS in clamd */
      if(!opt_check(opt, "disable-summary") && !opt_check(opt, "no-summary")) {
  	gettimeofday(&t2, &tz);
  	ds = t2.tv_sec - t1.tv_sec;
  	dms = t2.tv_usec - t1.tv_usec;
  	ds -= (dms < 0) ? (1):(0);
--- 138,158 ----
      time(&starttime);
      /* ctime() does \n, but I need it once more */
  
! #ifdef	C_WINDOWS
! 	gettimeofday(&t1, NULL);
! #else
! 	gettimeofday(&t1, &tz);
! #endif
  
      ret = client(opt, &infected);
  
      /* TODO: Implement STATUS in clamd */
      if(!opt_check(opt, "disable-summary") && !opt_check(opt, "no-summary")) {
+ #ifdef	C_WINDOWS
+ 	gettimeofday(&t2, NULL);
+ #else
  	gettimeofday(&t2, &tz);
+ #endif
  	ds = t2.tv_sec - t1.tv_sec;
  	dms = t2.tv_usec - t1.tv_usec;
  	ds -= (dms < 0) ? (1):(0);
***************
*** 140,145 ****
--- 169,183 ----
      }
  
      opt_free(opt);
+ 
+ #ifdef	C_WINDOWS
+ 	WSACleanup();
+ 	if(!pthread_win32_process_detach_np()) {
+ 		mprintf("!Can't stop the win32 pthreads layer\n");
+ 		return 1;
+ 	}
+ #endif
+ 
      exit(ret);
  }
  
*** /home/njh/src/clamav-devel/trunk/./libclamunrar_iface/unrar_iface.c	2007-12-26 13:58:50.000000000 +0000
--- ./libclamunrar_iface/unrar_iface.c	2008-02-25 15:48:36.000000000 +0000
***************
*** 542,544 ****
--- 542,564 ----
      free(state->unpack_data);
      free(state->comment_dir);
  }
+ 
+ #ifdef	C_WINDOWS
+ /*
+  * A copy is needed here to avoid a cyclic dependancy libclamunrar_iface<->libclamav
+  * e.g. see the comment in bug 775 about dropping the old internal snprintf
+  * which didn't have this problem and bug 785
+  */
+ #include <stdarg.h>
+ static int
+ snprintf(char *str, size_t size, const char *format, ...)
+ {
+ 	int ret;
+ 
+ 	va_list args;
+ 	va_start(args, format);
+ 	ret = _vsnprintf_s(str, size, _TRUNCATE, format, args);
+ 	va_end(args);
+ 	return ret;
+ }
+ #endif
