*** /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);
  }
  
