Browse code

examples/ex1.c: use new API

git-svn: trunk@4842

Tomasz Kojm authored on 2009/02/20 23:36:53
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Feb 20 15:57:11 CET 2009 (tk)
2
+---------------------------------
3
+ * examples/ex1.c: use new API
4
+
1 5
 Fri Feb 20 15:53:18 EET 2009 (edwin)
2 6
 ------------------------------------
3 7
  * unit_tests/check_clamd.c, unit_tests/check_clamd.sh: Fix
... ...
@@ -1,11 +1,9 @@
1 1
 /*
2 2
  *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav
3 3
  *
4
- *  Copyright (C) 2007 - 2008 Sourcefire, Inc.
4
+ *  Copyright (C) 2007 - 2009 Sourcefire, Inc.
5 5
  *  Author: Tomasz Kojm <tkojm@clamav.net>
6 6
  *
7
- *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
8
- *
9 7
  *  This program is free software; you can redistribute it and/or modify
10 8
  *  it under the terms of the GNU General Public License as published by
11 9
  *  the Free Software Foundation; either version 2 of the License, or
... ...
@@ -45,70 +43,63 @@ int main(int argc, char **argv)
45 45
 	unsigned int sigs = 0;
46 46
 	long double mb;
47 47
 	const char *virname;
48
-	struct cl_engine *engine = NULL;
49
-	struct cl_limits limits;
48
+	struct cl_engine *engine;
50 49
 
51 50
 
52 51
     if(argc != 2) {
53 52
 	printf("Usage: %s file\n", argv[0]);
54
-	exit(2);
53
+	return 2;
55 54
     }
56 55
 
57 56
     if((fd = open(argv[1], O_RDONLY)) == -1) {
58 57
 	printf("Can't open file %s\n", argv[1]);
59
-	exit(2);
58
+	return 2;
59
+    }
60
+
61
+    if(!(engine = cl_engine_new())) {
62
+	printf("Can't initialize antivirus engine\n");
63
+	return 2;
60 64
     }
61 65
 
62 66
     /* load all available databases from default directory */
63
-    if((ret = cl_load(cl_retdbdir(), &engine, &sigs, CL_DB_STDOPT))) {
67
+    if((ret = cl_load(cl_retdbdir(), engine, &sigs, CL_DB_STDOPT))) {
64 68
 	printf("cl_load: %s\n", cl_strerror(ret));
65 69
 	close(fd);
66
-	exit(2);
70
+        cl_engine_free(engine);
71
+	return 2;
67 72
     }
68 73
 
69
-    printf("Loaded %d signatures.\n", sigs);
74
+    printf("Loaded %u signatures.\n", sigs);
70 75
 
71 76
     /* build engine */
72
-    if((ret = cl_build(engine))) {
77
+    if((ret = cl_engine_compile(engine)) != 0) {
73 78
 	printf("Database initialization error: %s\n", cl_strerror(ret));;
74
-	cl_free(engine);
79
+        cl_engine_free(engine);
75 80
 	close(fd);
76
-	exit(2);
81
+	return 2;
77 82
     }
78 83
 
79
-    /* set up archive limits */
80
-    memset(&limits, 0, sizeof(struct cl_limits));
81
-    limits.maxscansize = 100 * 1048576; /* during the scanning of archives this
82
-					 * size (100 MB) will never be exceeded
83
-					 */
84
-    limits.maxfilesize = 10 * 1048576; /* compressed files will only be
85
-					* decompressed and scanned up to this
86
-					* size (10 MB)
87
-					*/
88
-    limits.maxfiles = 10000; /* max files */
89
-    limits.maxreclevel = 16; /* maximum recursion level for archives */
90
-
91 84
     /* scan file descriptor */
92
-    if((ret = cl_scandesc(fd, &virname, &size, engine, &limits, CL_SCAN_STDOPT)) == CL_VIRUS) {
85
+    if((ret = cl_scandesc(fd, &virname, &size, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
93 86
 	printf("Virus detected: %s\n", virname);
94 87
     } else {
95 88
 	if(ret == CL_CLEAN) {
96 89
 	    printf("No virus detected.\n");
97 90
 	} else {
98 91
 	    printf("Error: %s\n", cl_strerror(ret));
99
-	    cl_free(engine);
92
+	    cl_engine_free(engine);
100 93
 	    close(fd);
101
-	    exit(2);
94
+	    return 2;
102 95
 	}
103 96
     }
104 97
     close(fd);
105 98
 
99
+    /* free memory */
100
+    cl_engine_free(engine);
101
+
106 102
     /* calculate size of scanned data */
107 103
     mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
108 104
     printf("Data scanned: %2.2Lf MB\n", mb);
109 105
 
110
-    /* free memory */
111
-    cl_free(engine);
112
-
113
-    exit(ret == CL_VIRUS ? 1 : 0);
106
+    return ret == CL_VIRUS ? 1 : 0;
114 107
 }