Mon, 19 Mar 2018 16:36:14 +0100
regex parser was not properly reset before each file, sometimes resulting in wrong line counts, when the previous scanned file ended with a match
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2017 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * scanner.c * * Created on: 23.05.2011 * Author: Mike */ #include "scanner.h" #include "suffix_fnc.h" #include "bfile_heuristics.h" #include "regex_parser.h" #include <sys/stat.h> typedef struct filelist filelist_t; struct filelist { char *displayname; int displayname_len; char *filename; int st_mode; filelist_t *next; }; filelist_t *buildFileList(scanner_t scanner, settings_t* settings, filelist_t* list) { DIR *dirf; struct dirent *entry; struct stat statbuf; if ((dirf = opendir(scanner.dir)) == NULL) { printf("%s", scanner.dir); perror(" Directory access failed"); return 0; } while ((entry = readdir(dirf)) != NULL) { if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { /* Create new filelist entry */ filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); newentry->next = NULL; newentry->displayname_len = strlen(entry->d_name); newentry->displayname = (char*) malloc(newentry->displayname_len+1); memcpy(newentry->displayname, entry->d_name, newentry->displayname_len); newentry->displayname[newentry->displayname_len] = 0; newentry->st_mode = 0; /* Construct absolute pathname string */ size_t dirnamelen = strlen(scanner.dir); char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len); memcpy(filename, scanner.dir, dirnamelen); filename[dirnamelen] = settings->fileSeparator; memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); filename[1+dirnamelen+newentry->displayname_len] = 0; newentry->filename = filename; /* Check for subdirectory */ if (stat(filename, &statbuf) == 0) { newentry->st_mode = statbuf.st_mode; } else { perror(" Error in stat call"); continue; } if (list) { // create fake root to have a pointer on the true root filelist_t root; root.next = list; filelist_t *parent = &root; while (parent->next && (strcasecmp(parent->next->displayname, newentry->displayname) < 0 || (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode)) ) && (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode)) ) { parent = parent->next; } newentry->next = parent->next; parent->next = newentry; list = root.next; } else { list = newentry; } } } closedir(dirf); return list; } int scanDirectory(scanner_t scanner, settings_t* settings, string_list_t* output) { int lines, a; int lineSum = 0; bool bfile; char *outbuf; filelist_t *filelist = buildFileList(scanner, settings, NULL); while (filelist != NULL) { /* Scan subdirectories */ if (!S_ISREG(filelist->st_mode)) { if (settings->recursive && S_ISDIR(filelist->st_mode)) { string_list_t *recoutput = new_string_list_t(); lines = scanDirectory( (scanner_t) {filelist->filename, scanner.spaces+1}, settings, recoutput); lineSum += lines; if (!settings->matchesOnly || recoutput->count > 0) { outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s/%*s%13d lines\n", filelist->displayname_len+scanner.spaces, filelist->displayname, 60-filelist->displayname_len-scanner.spaces-1, "", lines); add_string(output, outbuf); for (int i = 0 ; i < recoutput->count ; i++) { add_string(output, recoutput->items[i]); } } destroy_string_list_t(recoutput); } else { outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces, filelist->displayname); add_string(output, outbuf); } } else { if ((settings->includeSuffixes->count == 0 || testSuffix(filelist->displayname, settings->includeSuffixes)) && !testSuffix(filelist->displayname, settings->excludeSuffixes)) { /* Count lines */ lines = 0; bfile = false; bfile_reset(settings->bfileHeuristics); regex_parser_reset(settings->regex); char line_buffer[REGEX_MAX_LINELENGTH]; int line_buffer_offset = 0; FILE *file = fopen(filelist->filename, "r"); if (file == NULL) { outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces, filelist->displayname); add_string(output, outbuf); perror(" File acces failed"); } else { do { a = fgetc(file); bfile = bfile_check(settings->bfileHeuristics, a); if (a == 10 || a == EOF) { line_buffer[line_buffer_offset] = 0; if (regex_parser_do(settings->regex, line_buffer) == 0) { /* Only subtract lines when matching has finished */ if (!regex_parser_matching(settings->regex)) { lines -= settings->regex->matched_lines; } } line_buffer_offset = 0; lines++; } else { if (line_buffer_offset < REGEX_MAX_LINELENGTH) { line_buffer[line_buffer_offset] = a; line_buffer_offset++; } else { line_buffer[line_buffer_offset-1] = 0; settings->confusing_lnlen = true; } } } while (!bfile && a != EOF); fclose(file); /* Print and sum line count */ if (bfile) { if (!settings->matchesOnly) { outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, filelist->displayname, 60-filelist->displayname_len-scanner.spaces, "", "binary"); add_string(output, outbuf); } } else { lineSum += lines; outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s%*s%13d lines\n", filelist->displayname_len+scanner.spaces, filelist->displayname, 60-filelist->displayname_len-scanner.spaces, "", lines); add_string(output, outbuf); } } } else { if (!settings->matchesOnly) { /* Print hint */ outbuf = (char*) malloc(81); snprintf(outbuf, 81, "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, filelist->displayname, 60-filelist->displayname_len-scanner.spaces, "", "no match"); add_string(output, outbuf); } } } free(filelist->filename); free(filelist->displayname); filelist_t *freethis = filelist; filelist = filelist->next; free(freethis); } return lineSum; }