Mon, 27 Jul 2020 17:19:56 +0200
adds option to compute individual sums
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2018 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. */ #include "scanner.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; char *ext; int st_mode; filelist_t *next; }; static bool testSuffix(char* filename, string_list_t* list) { bool ret = false; int tokenlen, fnamelen = strlen(filename); for (int t = 0 ; t < list->count ; t++) { tokenlen = strlen(list->items[t]); if (fnamelen >= tokenlen && tokenlen > 0) { if (strncmp(filename+fnamelen-tokenlen, list->items[t], tokenlen) == 0) { ret = true; break; } } } return ret; } static void addLinesPerExtension(scanresult_ext_t* result, char* ext, int lines) { if (!result) return; if (!ext) ext = "w/o"; for (int i = 0 ; i < result->count ; i++) { if (strcasecmp(result->extensions[i], ext) == 0) { result->lines[i] += lines; return; } } if (result->count == result->capacity) { int newcap = result->capacity+8; char** extarr = realloc(result->extensions, newcap*sizeof(char*)); int* linesarr = realloc(result->lines, newcap*sizeof(int)); if (!extarr || !linesarr) { fprintf(stderr, "Memory allocation error.\n"); abort(); } result->extensions = extarr; result->lines = linesarr; result->capacity = newcap; } result->extensions[result->count] = strdup(ext); result->lines[result->count] = lines; result->count++; } scanresult_t* new_scanresult_t(settings_t* settings) { scanresult_t* result = calloc(1, sizeof(scanresult_t)); if (settings->individual_sums) { result->ext = calloc(1, sizeof(scanresult_ext_t)); } return result; } void destroy_scanresult_t(scanresult_t* result) { if (result->ext) { if (result->ext->count > 0) { for (int i = 0 ; i < result->ext->count ; i++) { free(result->ext->extensions[i]); } free(result->ext->extensions); free(result->ext->lines); } free(result->ext); } free(result); } static 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) { fprintf(stderr, "%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; /* Obtain file extension */ newentry->ext = strrchr(newentry->displayname, '.'); /* 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; } void scanDirectory(scanner_t scanner, settings_t* settings, string_list_t* output, scanresult_t* result) { result->lines = 0; int a; 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(); scanresult_t recresult; recresult.ext = result->ext; scanDirectory( (scanner_t) {filelist->filename, scanner.spaces+1}, settings, recoutput, &recresult); result->lines += recresult.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, "", recresult.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 */ int 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 { addLinesPerExtension(result->ext, filelist->ext, lines); result->lines += 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); } }