mirror of https://github.com/Cisco-Talos/clamav
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.9 KiB
111 lines
2.9 KiB
![]()
22 years ago
|
/*
|
||
|
* Compilation: gcc -Wall ex1.c -o ex1 -lclamav
|
||
|
*
|
||
![]()
19 years ago
|
* Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
|
||
![]()
22 years ago
|
*
|
||
|
* 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
|
||
![]()
19 years ago
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
|
* MA 02110-1301, USA.
|
||
![]()
22 years ago
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
![]()
22 years ago
|
#include <stdlib.h>
|
||
![]()
21 years ago
|
#include <string.h>
|
||
![]()
22 years ago
|
#include <unistd.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <clamav.h>
|
||
|
|
||
![]()
19 years ago
|
/*
|
||
|
* Exit codes:
|
||
|
* 0: clean
|
||
|
* 1: infected
|
||
|
* 2: error
|
||
|
*/
|
||
|
|
||
![]()
22 years ago
|
int main(int argc, char **argv)
|
||
|
{
|
||
![]()
19 years ago
|
int fd, ret;
|
||
![]()
22 years ago
|
unsigned long int size = 0;
|
||
![]()
19 years ago
|
unsigned int sigs = 0;
|
||
![]()
22 years ago
|
long double mb;
|
||
![]()
21 years ago
|
const char *virname;
|
||
![]()
19 years ago
|
struct cl_engine *engine = NULL;
|
||
![]()
22 years ago
|
struct cl_limits limits;
|
||
|
|
||
![]()
21 years ago
|
|
||
![]()
22 years ago
|
if(argc != 2) {
|
||
|
printf("Usage: %s file\n", argv[0]);
|
||
|
exit(2);
|
||
|
}
|
||
|
|
||
![]()
21 years ago
|
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 */
|
||
![]()
19 years ago
|
if((ret = cl_load(cl_retdbdir(), &engine, &sigs, CL_DB_STDOPT))) {
|
||
![]()
19 years ago
|
printf("cl_load: %s\n", cl_strerror(ret));
|
||
![]()
21 years ago
|
close(fd);
|
||
![]()
22 years ago
|
exit(2);
|
||
|
}
|
||
|
|
||
![]()
19 years ago
|
printf("Loaded %d signatures.\n", sigs);
|
||
![]()
22 years ago
|
|
||
![]()
21 years ago
|
/* build engine */
|
||
![]()
19 years ago
|
if((ret = cl_build(engine))) {
|
||
![]()
22 years ago
|
printf("Database initialization error: %s\n", cl_strerror(ret));;
|
||
![]()
19 years ago
|
cl_free(engine);
|
||
![]()
21 years ago
|
close(fd);
|
||
![]()
22 years ago
|
exit(2);
|
||
|
}
|
||
|
|
||
![]()
22 years ago
|
/* set up archive limits */
|
||
![]()
22 years ago
|
memset(&limits, 0, sizeof(struct cl_limits));
|
||
![]()
22 years ago
|
limits.maxfiles = 1000; /* max files */
|
||
![]()
19 years ago
|
limits.maxfilesize = 10 * 1048576; /* maximum size of archived/compressed
|
||
|
* file (files exceeding this limit
|
||
|
* will be ignored)
|
||
|
*/
|
||
![]()
19 years ago
|
limits.maxreclevel = 5; /* maximum recursion level for archives */
|
||
|
limits.maxmailrec = 64; /* maximum recursion level for mail files */
|
||
![]()
19 years ago
|
limits.maxratio = 200; /* maximum compression ratio */
|
||
![]()
22 years ago
|
|
||
![]()
19 years ago
|
/* scan file descriptor */
|
||
|
if((ret = cl_scandesc(fd, &virname, &size, engine, &limits, CL_SCAN_STDOPT)) == CL_VIRUS) {
|
||
![]()
21 years ago
|
printf("Virus detected: %s\n", virname);
|
||
![]()
19 years ago
|
} else {
|
||
|
if(ret == CL_CLEAN) {
|
||
|
printf("No virus detected.\n");
|
||
|
} else {
|
||
|
printf("Error: %s\n", cl_strerror(ret));
|
||
|
cl_free(engine);
|
||
![]()
19 years ago
|
close(fd);
|
||
![]()
19 years ago
|
exit(2);
|
||
|
}
|
||
![]()
22 years ago
|
}
|
||
![]()
21 years ago
|
close(fd);
|
||
![]()
22 years ago
|
|
||
![]()
19 years ago
|
/* calculate size of scanned data */
|
||
![]()
22 years ago
|
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
|
||
![]()
19 years ago
|
printf("Data scanned: %2.2Lf MB\n", mb);
|
||
|
|
||
|
/* free memory */
|
||
|
cl_free(engine);
|
||
![]()
22 years ago
|
|
||
|
exit(ret == CL_VIRUS ? 1 : 0);
|
||
|
}
|