examples/ex1.c
b151ef55
 /*
  *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav
  *
36f2038b
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 #include <stdio.h>
1065f138
 #include <stdlib.h>
36f2038b
 #include <string.h>
b151ef55
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <clamav.h>
 
 int main(int argc, char **argv)
 {
d99b1840
 	int fd, ret;
 	unsigned int sigs = 0;
 	unsigned long int blocks = 0;
b151ef55
 	long double mb;
36f2038b
 	const char *virname;
b151ef55
 	struct cl_node *root = NULL;
 	struct cl_limits limits;
 
36f2038b
 
b151ef55
     if(argc != 2) {
 	printf("Usage: %s file\n", argv[0]);
 	exit(2);
     }
 
36f2038b
     if((fd = open(argv[1], O_RDONLY)) == -1) {
 	printf("Can't open file %s\n", argv[1]);
 	exit(2);
     }
 
     /* load all available databases from default directory */
b151ef55
 
d99b1840
     if((ret = cl_loaddbdir(cl_retdbdir(), &root, &sigs))) {
b151ef55
 	printf("cl_loaddbdir: %s\n", cl_perror(ret));
36f2038b
 	close(fd);
b151ef55
 	exit(2);
     }
 
d99b1840
     printf("Loaded %d signatures.\n", sigs);
b151ef55
 
f91f55e0
     /* build engine */
     if((ret = cl_build(root))) {
e8217f5a
 	printf("Database initialization error: %s\n", cl_strerror(ret));;
f91f55e0
 	cl_free(root);
36f2038b
 	close(fd);
b151ef55
 	exit(2);
     }
 
59970c62
     /* set up archive limits */
cf899a29
     memset(&limits, 0, sizeof(struct cl_limits));
b151ef55
     limits.maxfiles = 1000; /* max files */
     limits.maxfilesize = 10 * 1048576; /* maximal archived file size == 10 Mb */
36f2038b
     limits.maxreclevel = 5; /* maximal recursion level */
     limits.maxratio = 200; /* maximal compression ratio */
     limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
b151ef55
 
b89b325e
     /* scan descriptor */
d99b1840
     if((ret = cl_scandesc(fd, &virname, &blocks, root, &limits, CL_SCAN_STDOPT)) == CL_VIRUS)
36f2038b
 	printf("Virus detected: %s\n", virname);
b151ef55
     else {
 	printf("No virus detected.\n");
 	if(ret != CL_CLEAN)
 	    printf("Error: %s\n", cl_perror(ret));
     }
b89b325e
     close(fd);
b151ef55
 
d99b1840
     mb = blocks * (CL_COUNT_PRECISION / 1024) / 1024.0;
b151ef55
     printf("Data scanned: %2.2Lf Mb\n", mb);
 
f91f55e0
     cl_free(root);
b151ef55
     exit(ret == CL_VIRUS ? 1 : 0);
 }