clamav-devel/shared/misc.c
7b304dee
 /*
98ce643b
  *  Copyright (C) 2004 - 2005 Tomasz Kojm <tkojm@clamav.net>
7b304dee
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
7b304dee
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
63abd169
 #include <stdlib.h>
 #include <unistd.h>
7b304dee
 #include <string.h>
7c225c08
 #include <time.h>
63abd169
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
7b304dee
 
 #include "clamav.h"
 #include "cfgparser.h"
 #include "memory.h"
2b259453
 #include "output.h"
 
7b304dee
 
98ce643b
 char *freshdbdir(void)
7b304dee
 {
 	struct cl_cvd *d1, *d2;
98ce643b
 	struct cfgstruct *copt = NULL, *cpt;
7b304dee
 	const char *dbdir;
98ce643b
 	char *retdir;
7b304dee
 
     /* try to find fresh directory */
     dbdir = cl_retdbdir();
81837459
     if((copt = getcfg(CONFDIR"/clamd.conf", 0))) {
 	if((cpt = cfgopt(copt, "DatabaseDirectory"))->enabled || (cpt = cfgopt(copt, "DataDirectory"))->enabled) {
6ee0243f
 	    if(strcmp(dbdir, cpt->strarg)) {
 		    char *daily = (char *) mmalloc(strlen(cpt->strarg) + strlen(dbdir) + 15);
7b304dee
 		sprintf(daily, "%s/daily.cvd", cpt->strarg);
 		if((d1 = cl_cvdhead(daily))) {
6ee0243f
 		    sprintf(daily, "%s/daily.cvd", dbdir);
7b304dee
 		    if((d2 = cl_cvdhead(daily))) {
 			free(daily);
 			if(d1->version > d2->version)
 			    dbdir = cpt->strarg;
 			cl_cvdfree(d2);
 		    } else {
 			free(daily);
 			dbdir = cpt->strarg;
 		    }
 		    cl_cvdfree(d1);
 		} else {
 		    free(daily);
 		}
 	    }
 	}
     }
 
98ce643b
     retdir = strdup(dbdir);
 
     if(copt)
 	freecfg(copt);
 
     return retdir;
7b304dee
 }
 
 void print_version(void)
 {
98ce643b
 	char *dbdir;
2b259453
 	char *path;
7b304dee
 	struct cl_cvd *daily;
 
 
     dbdir = freshdbdir();
98ce643b
     if(!(path = mmalloc(strlen(dbdir) + 11))) {
 	free(dbdir);
7b304dee
 	return;
98ce643b
     }
7b304dee
 
     sprintf(path, "%s/daily.cvd", dbdir);
98ce643b
     free(dbdir);
7b304dee
 
     if((daily = cl_cvdhead(path))) {
7c225c08
 	    time_t t = (time_t) daily->stime;
36d7455d
 	printf("ClamAV "VERSION"/%d/%s", daily->version, ctime(&t));
7c225c08
 	cl_cvdfree(daily);
7b304dee
     } else {
36d7455d
 	printf("ClamAV "VERSION"\n");
7b304dee
     }
 
     free(path);
 }
63abd169
 
 int filecopy(const char *src, const char *dest)
 {
 
 #ifdef C_DARWIN
c8e6cded
 	pid_t pid;
63abd169
 
c8e6cded
     /* On Mac OS X use ditto and copy resource fork, too. */
     switch(pid = fork()) {
 	case -1:
 	    return -1;
 	case 0:
 	    execl("/usr/bin/ditto", "ditto", "--rsrc", src, dest, NULL);
 	    perror("execv(ditto)");
 	    break;
 	default:
 	    wait(NULL);
 	    return 0;
63abd169
     }
 
c8e6cded
     return -1;
63abd169
 
 #else
 	char buffer[FILEBUFF];
 	int s, d, bytes;
 
     if((s = open(src, O_RDONLY)) == -1)
 	return -1;
 
     if((d = open(dest, O_CREAT|O_WRONLY|O_TRUNC)) == -1) {
 	close(s);
 	return -1;
     }
 
     while((bytes = read(s, buffer, FILEBUFF)) > 0)
 	write(d, buffer, bytes);
 
     close(s);
 
     /* njh@bandsman.co.uk: check result of close for NFS file */
     return close(d);
 
 #endif
 
 }
5fabe557
 
 int isnumb(const char *str)
 {
     while(*str) {
 	if(!isdigit(*str))
 	    return 0;
 	str++;
     }
 
     return 1;
 }