~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to examples/ex1.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav
3
3
 *
4
 
 *  Copyright (C) 2007 - 2009 Sourcefire, Inc.
5
 
 *  Author: Tomasz Kojm <tkojm@clamav.net>
 
4
 *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
6
5
 *
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;
44
43
        long double mb;
45
44
        const char *virname;
46
 
        struct cl_engine *engine;
 
45
        struct cl_engine *engine = NULL;
 
46
        struct cl_limits limits;
47
47
 
48
48
 
49
49
    if(argc != 2) {
50
50
        printf("Usage: %s file\n", argv[0]);
51
 
        return 2;
 
51
        exit(2);
52
52
    }
53
53
 
54
54
    if((fd = open(argv[1], O_RDONLY)) == -1) {
55
55
        printf("Can't open file %s\n", argv[1]);
56
 
        return 2;
57
 
    }
58
 
 
59
 
    if((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
60
 
        printf("Can't initialize libclamav: %s\n", cl_strerror(ret));
61
 
        return 2;
62
 
    }
63
 
 
64
 
    if(!(engine = cl_engine_new())) {
65
 
        printf("Can't create new engine\n");
66
 
        return 2;
 
56
        exit(2);
67
57
    }
68
58
 
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));
72
62
        close(fd);
73
 
        cl_engine_free(engine);
74
 
        return 2;
 
63
        exit(2);
75
64
    }
76
65
 
77
 
    printf("Loaded %u signatures.\n", sigs);
 
66
    printf("Loaded %d signatures.\n", sigs);
78
67
 
79
68
    /* build engine */
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);
 
71
        cl_free(engine);
83
72
        close(fd);
84
 
        return 2;
 
73
        exit(2);
85
74
    }
86
75
 
 
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
 
81
                                        * will be ignored)
 
82
                                        */
 
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 */
 
86
 
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);
90
90
    } else {
91
91
        if(ret == CL_CLEAN) {
92
92
            printf("No virus detected.\n");
93
93
        } else {
94
94
            printf("Error: %s\n", cl_strerror(ret));
95
 
            cl_engine_free(engine);
 
95
            cl_free(engine);
96
96
            close(fd);
97
 
            return 2;
 
97
            exit(2);
98
98
        }
99
99
    }
100
100
    close(fd);
101
101
 
102
 
    /* free memory */
103
 
    cl_engine_free(engine);
104
 
 
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);
108
105
 
109
 
    return ret == CL_VIRUS ? 1 : 0;
 
106
    /* free memory */
 
107
    cl_free(engine);
 
108
 
 
109
    exit(ret == CL_VIRUS ? 1 : 0);
110
110
}