e3aaff8e |
/*
* Compilation: gcc -Wall ex1.c -o ex1 -lclamav
* |
e7f5f537 |
* Copyright (C) 2013-2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
52cddcbc |
* Copyright (C) 2007-2013 Sourcefire, Inc. |
50b8f5d6 |
* Author: 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>
|
122bc02e |
/*
* Exit codes:
* 0: clean
* 1: infected
* 2: error
*/
|
e3aaff8e |
int main(int argc, char **argv)
{ |
72fd33c8 |
int fd, ret;
unsigned long int size = 0;
unsigned int sigs = 0;
long double mb;
const char *virname; |
7e7663ab |
const char *filename; |
72fd33c8 |
struct cl_engine *engine; |
d7979d4f |
struct cl_scan_options options; |
e3aaff8e |
|
72fd33c8 |
if (argc != 2) {
printf("Usage: %s file\n", argv[0]);
return 2; |
e3aaff8e |
}
|
7e7663ab |
filename = argv[1];
|
72fd33c8 |
if ((fd = open(argv[1], O_RDONLY)) == -1) {
printf("Can't open file %s\n", argv[1]);
return 2; |
1005682a |
}
|
72fd33c8 |
if ((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
return 2; |
af900995 |
}
|
72fd33c8 |
if (!(engine = cl_engine_new())) {
printf("Can't create new engine\n");
return 2; |
afb48b28 |
}
|
20a3dc42 |
/* Example version macro usage to determine if new feature is available */
#if defined(LIBCLAMAV_VERSION_NUM) && (LIBCLAMAV_VERSION_NUM >= 0x090400)
/* Example feature usage lowering max scan time to 15 seconds. */
cl_engine_set_num(engine, CL_ENGINE_MAX_SCANTIME, 15000);
#endif
|
afb48b28 |
/* load all available databases from default directory */ |
72fd33c8 |
if ((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT)) != CL_SUCCESS) {
printf("cl_load: %s\n", cl_strerror(ret));
close(fd); |
1005682a |
cl_engine_free(engine); |
72fd33c8 |
return 2; |
e3aaff8e |
}
|
1005682a |
printf("Loaded %u signatures.\n", sigs); |
e3aaff8e |
|
8000d078 |
/* build engine */ |
72fd33c8 |
if ((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("Database initialization error: %s\n", cl_strerror(ret)); |
1005682a |
cl_engine_free(engine); |
72fd33c8 |
close(fd);
return 2; |
e3aaff8e |
}
|
122bc02e |
/* scan file descriptor */ |
d7979d4f |
memset(&options, 0, sizeof(struct cl_scan_options)); |
72fd33c8 |
options.parse |= ~0; /* enable all parsers */ |
d7979d4f |
options.general |= CL_SCAN_GENERAL_HEURISTICS; /* enable heuristic alert options */
|
72fd33c8 |
if ((ret = cl_scandesc(fd, filename, &virname, &size, engine, &options)) == CL_VIRUS) {
printf("Virus detected: %s\n", virname); |
122bc02e |
} else { |
72fd33c8 |
if (ret == CL_CLEAN) {
printf("No virus detected.\n");
} else {
printf("Error: %s\n", cl_strerror(ret));
cl_engine_free(engine);
close(fd);
return 2;
} |
e3aaff8e |
} |
64d54968 |
close(fd); |
e3aaff8e |
|
1005682a |
/* free memory */
cl_engine_free(engine);
|
122bc02e |
/* calculate size of scanned data */ |
e3aaff8e |
mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0; |
122bc02e |
printf("Data scanned: %2.2Lf MB\n", mb);
|
1005682a |
return ret == CL_VIRUS ? 1 : 0; |
e3aaff8e |
} |