Changeset 1351

Show
Ignore:
Timestamp:
08/11/07 17:22:25 (1 year ago)
Author:
till
Message:

- munmapping fixed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeytrap/trunk/tools/ngram.c

    r1114 r1351  
    11/* ngram.c 
    2  * Copyright (C) 2006 Tillmann Werner <tillmann.werner@gmx.de> 
     2 * Copyright (C) 2006-2007 Tillmann Werner <tillmann.werner@gmx.de> 
    33 * 
    44 * This file is free software; as a special exception the author gives 
     
    7676 
    7777static int avl_height(pos p) { 
    78         if(p == NULL) return(-1); 
    79         return(p->height); 
     78        return(p == NULL ? -1 : p->height); 
    8079} 
    8180 
     
    200199 
    201200int main(int argc, char *argv[]) { 
    202         int fd, bytes_read, n
    203         struct stat fs1, fs2
    204         u_char *content1, *content2
     201        int fd, bytes_read, n, i
     202        struct stat fs[2]
     203        u_char *content[2]
    205204        float dotproduct, len1, len2, result; 
    206205        avl_tree ngtree; 
     
    213212        result          = 0; 
    214213        bytes_read      = 0; 
    215         content1      = NULL; 
    216         content2      = NULL; 
     214        content[0]    = NULL; 
     215        content[1]    = NULL; 
    217216 
    218217        if (argc < 4) { 
     
    224223 
    225224 
    226         /* map first file */ 
    227         if ((fd = open(argv[2], O_RDONLY)) == -1) { 
    228                 fprintf(stderr, "Error - Unable to open file: %s.\n", strerror(errno)); 
    229                 exit(1); 
    230         } 
    231         if (fstat(fd, &fs1) != 0) { 
    232                 fprintf(stderr, "Error - Unable to get file size: %s.\n", strerror(errno)); 
    233                 exit(1); 
    234         } 
    235         if (fs1.st_size < 1) { 
    236                 fprintf(stdout, "File %s is empty.\n", argv[2]); 
    237                 exit(0); 
    238         } 
    239         if ((content1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 
    240                 fprintf(stderr, "Error - Unable to map file into memory: %s.\n", strerror(errno)); 
    241                 exit(1); 
    242         } 
    243         close(fd); 
    244  
    245  
    246         /* map second file */ 
    247         if ((fd = open(argv[3], O_RDONLY)) == -1) { 
    248                 fprintf(stderr, "Error - Unable to open file: %s.\n", strerror(errno)); 
    249                 exit(1); 
    250         } 
    251         if (fstat(fd, &fs2) != 0) { 
    252                 fprintf(stderr, "Error - Unable to get file size: %s.\n", strerror(errno)); 
    253                 exit(1); 
    254         } 
    255         if (fs2.st_size < 1) { 
    256                 fprintf(stdout, "File is empty.\n"); 
    257                 exit(0); 
    258         } 
    259         if ((content2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 
    260                 fprintf(stderr, "Error - Unable to map file into memory: %s.\n", strerror(errno)); 
    261                 exit(1); 
    262         } 
    263         close(fd); 
     225        /* mmap files */ 
     226        for (i=0; i<2; i++) { 
     227                if ((fd = open(argv[i+2], O_RDONLY)) == -1) { 
     228                        fprintf(stderr, "Error - Unable to open file: %s.\n", strerror(errno)); 
     229                        exit(EXIT_FAILURE); 
     230                } 
     231                if (fstat(fd, &fs[i]) != 0) { 
     232                        fprintf(stderr, "Error - Unable to get file size: %s.\n", strerror(errno)); 
     233                        exit(EXIT_FAILURE); 
     234                } 
     235                if (fs[i].st_size < 1) { 
     236                        fprintf(stdout, "File %s is empty.\n", argv[i+2]); 
     237                        exit(EXIT_SUCCESS); 
     238                } 
     239                if ((content[i] = mmap(0, fs[i].st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { 
     240                        fprintf(stderr, "Error - Unable to map file into memory: %s.\n", strerror(errno)); 
     241                        exit(EXIT_FAILURE); 
     242                } 
     243                close(fd); 
     244        } 
    264245 
    265246 
    266247        /* calculate ngrams */ 
    267         ngtree = calc_ngrams(n, content1, content2, fs1.st_size, fs2.st_size); 
     248        ngtree = calc_ngrams(n, content[0], content[1], fs[0].st_size, fs[1].st_size); 
    268249 
    269250        /* calculate vector lenghts and delete tree */  
     
    279260        fprintf(stdout, "Similarity: %.2f%%.\n", result); 
    280261 
    281         if ((munmap(content1, fs1.st_size) != 0) || (munmap(content1, fs1.st_size) != 0)) { 
     262        if ((munmap(content[0], fs[0].st_size) != 0) || (munmap(content[1], fs[1].st_size) != 0)) { 
    282263                fprintf(stderr, "Unmapping files failed: %s.\n", strerror(errno)); 
    283264                exit(1);