e3aaff8e |
/*
* Compilation: gcc -Wall ex1.c -o ex1 -lclamav
* |
afb48b28 |
* Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
*
* 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. |
e3aaff8e |
*/
#include <stdio.h> |
a0977bfe |
#include <stdlib.h> |
afb48b28 |
#include <string.h> |
e3aaff8e |
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <clamav.h>
int main(int argc, char **argv)
{ |
6c3c779d |
int fd, ret, no = 0; |
e3aaff8e |
unsigned long int size = 0;
long double mb; |
afb48b28 |
const char *virname; |
e3aaff8e |
struct cl_node *root = NULL;
struct cl_limits limits;
|
afb48b28 |
|
e3aaff8e |
if(argc != 2) {
printf("Usage: %s file\n", argv[0]);
exit(2);
}
|
afb48b28 |
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 */ |
e3aaff8e |
if((ret = cl_loaddbdir(cl_retdbdir(), &root, &no))) {
printf("cl_loaddbdir: %s\n", cl_perror(ret)); |
afb48b28 |
close(fd); |
e3aaff8e |
exit(2);
}
|
9f6cc5a3 |
printf("Loaded %d signatures.\n", no); |
e3aaff8e |
|
8000d078 |
/* build engine */
if((ret = cl_build(root))) { |
2d70a403 |
printf("Database initialization error: %s\n", cl_strerror(ret));; |
8000d078 |
cl_free(root); |
afb48b28 |
close(fd); |
e3aaff8e |
exit(2);
}
|
9f6cc5a3 |
/* set up archive limits */ |
a6945b5d |
memset(&limits, 0, sizeof(struct cl_limits)); |
e3aaff8e |
limits.maxfiles = 1000; /* max files */
limits.maxfilesize = 10 * 1048576; /* maximal archived file size == 10 Mb */ |
afb48b28 |
limits.maxreclevel = 5; /* maximal recursion level */
limits.maxratio = 200; /* maximal compression ratio */
limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */ |
e3aaff8e |
|
64d54968 |
/* scan descriptor */ |
3805ebcb |
if((ret = cl_scandesc(fd, &virname, &size, root, &limits, CL_SCAN_STDOPT)) == CL_VIRUS) |
afb48b28 |
printf("Virus detected: %s\n", virname); |
e3aaff8e |
else {
printf("No virus detected.\n");
if(ret != CL_CLEAN)
printf("Error: %s\n", cl_perror(ret));
} |
64d54968 |
close(fd); |
e3aaff8e |
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
printf("Data scanned: %2.2Lf Mb\n", mb);
|
8000d078 |
cl_free(root); |
e3aaff8e |
exit(ret == CL_VIRUS ? 1 : 0);
} |