2
2
* Compilation: gcc -Wall ex1.c -o ex1 -lclamav
4
* Copyright (C) 2007 - 2009 Sourcefire, Inc.
5
* Author: Tomasz Kojm <tkojm@clamav.net>
4
* Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
7
6
* This program is free software; you can redistribute it and/or modify
8
7
* it under the terms of the GNU General Public License as published by
43
42
unsigned int sigs = 0;
45
44
const char *virname;
46
struct cl_engine *engine;
45
struct cl_engine *engine = NULL;
46
struct cl_limits limits;
50
50
printf("Usage: %s file\n", argv[0]);
54
54
if((fd = open(argv[1], O_RDONLY)) == -1) {
55
55
printf("Can't open file %s\n", argv[1]);
59
if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
60
printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
64
if(!(engine = cl_engine_new())) {
65
printf("Can't create new engine\n");
69
59
/* load all available databases from default directory */
70
if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) {
60
if((ret = cl_load(cl_retdbdir(), &engine, &sigs, CL_DB_STDOPT))) {
71
61
printf("cl_load: %s\n", cl_strerror(ret));
73
cl_engine_free(engine);
77
printf("Loaded %u signatures.\n", sigs);
66
printf("Loaded %d signatures.\n", sigs);
80
if((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
69
if((ret = cl_build(engine))) {
81
70
printf("Database initialization error: %s\n", cl_strerror(ret));;
82
cl_engine_free(engine);
76
/* set up archive limits */
77
memset(&limits, 0, sizeof(struct cl_limits));
78
limits.maxfiles = 1000; /* max files */
79
limits.maxfilesize = 10 * 1048576; /* maximum size of archived/compressed
80
* file (files exceeding this limit
83
limits.maxreclevel = 5; /* maximum recursion level for archives */
84
limits.maxmailrec = 64; /* maximum recursion level for mail files */
85
limits.maxratio = 200; /* maximum compression ratio */
87
87
/* scan file descriptor */
88
if((ret = cl_scandesc(fd, &virname, &size, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
88
if((ret = cl_scandesc(fd, &virname, &size, engine, &limits, CL_SCAN_STDOPT)) == CL_VIRUS) {
89
89
printf("Virus detected: %s\n", virname);
91
91
if(ret == CL_CLEAN) {
92
92
printf("No virus detected.\n");
94
94
printf("Error: %s\n", cl_strerror(ret));
95
cl_engine_free(engine);
103
cl_engine_free(engine);
105
102
/* calculate size of scanned data */
106
103
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
107
104
printf("Data scanned: %2.2Lf MB\n", mb);
109
return ret == CL_VIRUS ? 1 : 0;
109
exit(ret == CL_VIRUS ? 1 : 0);