examples/ex1.c
b151ef55
 /*
  *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav
  *
7bd32316
  *  Copyright (C) 2002 - 2006 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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
b151ef55
  */
 
 #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>
 
7bd32316
 /*
  * Exit codes:
  *  0: clean
  *  1: infected
  *  2: error
  */
 
b151ef55
 int main(int argc, char **argv)
 {
7bd32316
 	int fd, ret;
b151ef55
 	unsigned long int size = 0;
7bd32316
 	unsigned int sigs = 0;
b151ef55
 	long double mb;
36f2038b
 	const char *virname;
7bd32316
 	struct cl_engine *engine = NULL;
b151ef55
 	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 */
7bd32316
     if((ret = cl_load(cl_retdbdir(), &engine, &sigs, CL_DB_STDOPT))) {
23b69871
 	printf("cl_load: %s\n", cl_strerror(ret));
36f2038b
 	close(fd);
b151ef55
 	exit(2);
     }
 
7bd32316
     printf("Loaded %d signatures.\n", sigs);
b151ef55
 
f91f55e0
     /* build engine */
7bd32316
     if((ret = cl_build(engine))) {
e8217f5a
 	printf("Database initialization error: %s\n", cl_strerror(ret));;
7bd32316
 	cl_free(engine);
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 */
7bd32316
     limits.maxfilesize = 10 * 1048576; /* maximum size of archived/compressed
 					* file (files exceeding this limit
 					* will be ignored)
 					*/
     limits.maxreclevel = 5; /* maximum recursion level */
     limits.maxratio = 200; /* maximum compression ratio */
b151ef55
 
7bd32316
     /* scan file descriptor */
     if((ret = cl_scandesc(fd, &virname, &size, engine, &limits, CL_SCAN_STDOPT)) == CL_VIRUS) {
36f2038b
 	printf("Virus detected: %s\n", virname);
7bd32316
     } else {
 	if(ret == CL_CLEAN) {
 	    printf("No virus detected.\n");
 	} else {
 	    printf("Error: %s\n", cl_strerror(ret));
 	    cl_free(engine);
23b69871
 	    close(fd);
7bd32316
 	    exit(2);
 	}
b151ef55
     }
b89b325e
     close(fd);
b151ef55
 
7bd32316
     /* calculate size of scanned data */
b151ef55
     mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
7bd32316
     printf("Data scanned: %2.2Lf MB\n", mb);
 
     /* free memory */
     cl_free(engine);
b151ef55
 
     exit(ret == CL_VIRUS ? 1 : 0);
 }