src/scanner.c

Mon, 19 Mar 2018 16:36:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Mar 2018 16:36:14 +0100
changeset 54
76d46533b9a9
parent 48
0d2c13c24fd0
child 57
68018eac46c3
permissions
-rw-r--r--

regex parser was not properly reset before each file, sometimes resulting in wrong line counts, when the previous scanned file ended with a match

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     3  * Copyright 2017 Mike Becker. All rights reserved.
     4  * 
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  * 
     8  * 1. Redistributions of source code must retain the above copyright
     9  * notice, this list of conditions and the following disclaimer.
    10  * 
    11  * 2. Redistributions in binary form must reproduce the above copyright
    12  * notice, this list of conditions and the following disclaimer in the
    13  * documentation and/or other materials provided with the distribution.
    14  * 
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    25  *
    26  * scanner.c
    27  *
    28  *  Created on: 23.05.2011
    29  *      Author: Mike
    30  */
    33 #include "scanner.h"
    34 #include "suffix_fnc.h"
    35 #include "bfile_heuristics.h"
    36 #include "regex_parser.h"
    37 #include <sys/stat.h>
    39 typedef struct filelist filelist_t;
    41 struct filelist {
    42   char *displayname;
    43   int displayname_len;
    44   char *filename;
    45   int st_mode;
    46   filelist_t *next;
    47 };
    49 filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
    50     filelist_t* list) {
    52   DIR *dirf;
    53   struct dirent *entry;
    54   struct stat statbuf;
    56   if ((dirf = opendir(scanner.dir)) == NULL) {
    57     printf("%s", scanner.dir);
    58     perror("  Directory access failed");
    59     return 0;
    60   }
    62   while ((entry = readdir(dirf)) != NULL) {
    63     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    65       /* Create new filelist entry */
    66       filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
    67       newentry->next = NULL;
    69       newentry->displayname_len = strlen(entry->d_name);
    70       newentry->displayname = (char*) malloc(newentry->displayname_len+1);
    71       memcpy(newentry->displayname, entry->d_name, newentry->displayname_len);
    72       newentry->displayname[newentry->displayname_len] = 0;
    74       newentry->st_mode = 0;
    76       /* Construct absolute pathname string */
    77       size_t dirnamelen = strlen(scanner.dir);
    78       char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len);
    79       memcpy(filename, scanner.dir, dirnamelen);
    80       filename[dirnamelen] = settings->fileSeparator;
    81       memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
    82       filename[1+dirnamelen+newentry->displayname_len] = 0;
    83       newentry->filename = filename;
    85       /* Check for subdirectory */
    86       if (stat(filename, &statbuf) == 0) {
    87         newentry->st_mode = statbuf.st_mode;
    88       } else {
    89         perror("  Error in stat call");
    90         continue;
    91       }
    93       if (list) {
    94         // create fake root to have a pointer on the true root
    95         filelist_t root;
    96         root.next = list;
    97         filelist_t *parent = &root;
    98         while (parent->next &&
    99             (strcasecmp(parent->next->displayname, newentry->displayname) < 0 ||
   100               (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode))
   101             ) &&
   102             (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode))
   103             ) {
   104           parent = parent->next;
   105         }
   106         newentry->next = parent->next;
   107         parent->next = newentry;
   108         list = root.next;
   109       } else {
   110         list = newentry;
   111       }
   112     }
   113   }
   115   closedir(dirf);
   117   return list;
   118 }
   120 int scanDirectory(scanner_t scanner, settings_t* settings,
   121     string_list_t* output) {
   123   int lines, a;
   124   int lineSum = 0;
   125   bool bfile;
   126   char *outbuf;
   128   filelist_t *filelist = buildFileList(scanner, settings, NULL);
   130   while (filelist != NULL) {
   132     /* Scan subdirectories */
   133     if (!S_ISREG(filelist->st_mode)) {
   134       if (settings->recursive && S_ISDIR(filelist->st_mode)) {
   135         string_list_t *recoutput = new_string_list_t();
   136         lines = scanDirectory(
   137             (scanner_t) {filelist->filename, scanner.spaces+1},
   138             settings, recoutput);
   139         lineSum += lines;
   140         if (!settings->matchesOnly || recoutput->count > 0) {
   141           outbuf = (char*) malloc(81);
   142           snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
   143               filelist->displayname_len+scanner.spaces, filelist->displayname,
   144               60-filelist->displayname_len-scanner.spaces-1, "", lines);
   145           add_string(output, outbuf);
   146           for (int i = 0 ; i < recoutput->count ; i++) {
   147             add_string(output, recoutput->items[i]);
   148           }
   149         }
   150         destroy_string_list_t(recoutput);
   151       } else {
   152         outbuf = (char*) malloc(81);
   153         snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
   154           filelist->displayname);
   155         add_string(output, outbuf);
   156       }
   157     } else {
   158       if ((settings->includeSuffixes->count == 0
   159         || testSuffix(filelist->displayname, settings->includeSuffixes))
   160         && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
   162         /* Count lines */
   163         lines = 0;
   164         bfile = false;
   165         bfile_reset(settings->bfileHeuristics);
   166         regex_parser_reset(settings->regex);
   167         char line_buffer[REGEX_MAX_LINELENGTH];
   168         int line_buffer_offset = 0;
   170         FILE *file = fopen(filelist->filename, "r");
   171         if (file == NULL) {
   172           outbuf = (char*) malloc(81);
   173           snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
   174               filelist->displayname);
   175           add_string(output, outbuf);
   176           perror("  File acces failed");
   177         } else {
   178           do {
   179             a = fgetc(file);
   181             bfile = bfile_check(settings->bfileHeuristics, a);
   183             if (a == 10 || a == EOF) {
   184               line_buffer[line_buffer_offset] = 0;
   185               if (regex_parser_do(settings->regex, line_buffer) == 0) {
   186                 /* Only subtract lines when matching has finished */
   187                 if (!regex_parser_matching(settings->regex)) {
   188                   lines -= settings->regex->matched_lines;
   189                 }
   190               }
   192               line_buffer_offset = 0;
   193               lines++;
   194             } else {
   195               if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
   196                 line_buffer[line_buffer_offset] = a;
   197                 line_buffer_offset++;
   198               } else {
   199                 line_buffer[line_buffer_offset-1] = 0;
   200                 settings->confusing_lnlen = true;
   201               }
   202             }
   203           } while (!bfile && a != EOF);
   204           fclose(file);
   206           /* Print and sum line count */
   207           if (bfile) {
   208             if (!settings->matchesOnly) {
   209               outbuf = (char*) malloc(81);
   210               snprintf(outbuf, 81,
   211                   "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
   212                   filelist->displayname,
   213                   60-filelist->displayname_len-scanner.spaces, "", "binary");
   214               add_string(output, outbuf);
   215             }
   216           } else {
   217             lineSum += lines;
   218             outbuf = (char*) malloc(81);
   219             snprintf(outbuf, 81, "%*s%*s%13d lines\n",
   220                 filelist->displayname_len+scanner.spaces, filelist->displayname,
   221                 60-filelist->displayname_len-scanner.spaces, "", lines);
   222             add_string(output, outbuf);
   223           }
   224         }
   225       } else {
   226         if (!settings->matchesOnly) {
   227           /* Print hint */
   228           outbuf = (char*) malloc(81);
   229           snprintf(outbuf, 81, "%*s%*s%19s\n",
   230               filelist->displayname_len+scanner.spaces, filelist->displayname,
   231               60-filelist->displayname_len-scanner.spaces, "", "no match");
   232           add_string(output, outbuf);
   233         }
   234       }
   235     }
   237     free(filelist->filename);
   238     free(filelist->displayname);
   239     filelist_t *freethis = filelist;
   240     filelist = filelist->next;
   241     free(freethis);
   242   }
   244   return lineSum;
   245 }

mercurial