src/scanner.c

Mon, 27 Jul 2020 17:19:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 17:19:56 +0200
changeset 61
9c8d768f0244
parent 60
69be673a4fd0
child 66
be2084398c37
permissions
-rw-r--r--

adds option to compute individual sums

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     3  * Copyright 2018 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  */
    28 #include "scanner.h"
    29 #include "bfile_heuristics.h"
    30 #include "regex_parser.h"
    31 #include <sys/stat.h>
    33 typedef struct filelist filelist_t;
    35 struct filelist {
    36   char *displayname;
    37   int displayname_len;
    38   char *filename;
    39   char *ext;
    40   int st_mode;
    41   filelist_t *next;
    42 };
    44 static bool testSuffix(char* filename, string_list_t* list) {
    45   bool ret = false;
    46   int tokenlen, fnamelen = strlen(filename);
    47   for (int t = 0 ; t < list->count ; t++) {
    48     tokenlen = strlen(list->items[t]);
    49     if (fnamelen >= tokenlen && tokenlen > 0) {
    50       if (strncmp(filename+fnamelen-tokenlen,
    51                   list->items[t], tokenlen) == 0) {
    52         ret = true;
    53         break;
    54       }
    55     }
    56   }
    57   return ret;
    58 }
    60 static void addLinesPerExtension(scanresult_ext_t* result,
    61         char* ext, int lines) {
    62   if (!result) return;
    64   if (!ext) ext = "w/o";
    66   for (int i = 0 ; i < result->count ; i++) {
    67     if (strcasecmp(result->extensions[i], ext) == 0) {
    68       result->lines[i] += lines;
    69       return;
    70     }
    71   }
    73   if (result->count == result->capacity) {
    74     int newcap = result->capacity+8;
    75     char** extarr = realloc(result->extensions, newcap*sizeof(char*));
    76     int* linesarr = realloc(result->lines, newcap*sizeof(int));
    77     if (!extarr || !linesarr) {
    78       fprintf(stderr, "Memory allocation error.\n");
    79       abort();
    80     }
    81     result->extensions = extarr;
    82     result->lines = linesarr;
    83     result->capacity = newcap;
    84   }
    86   result->extensions[result->count] = strdup(ext);
    87   result->lines[result->count] = lines;
    88   result->count++;
    89 }
    91 scanresult_t* new_scanresult_t(settings_t* settings) {
    92   scanresult_t* result = calloc(1, sizeof(scanresult_t));
    93   if (settings->individual_sums) {
    94     result->ext = calloc(1, sizeof(scanresult_ext_t));
    95   }
    96   return result;
    97 }
    99 void destroy_scanresult_t(scanresult_t* result) {
   100   if (result->ext) {
   101     if (result->ext->count > 0) {
   102       for (int i = 0 ; i < result->ext->count ; i++) {
   103         free(result->ext->extensions[i]);
   104       }
   105       free(result->ext->extensions);
   106       free(result->ext->lines);
   107     }
   108     free(result->ext);
   109   }
   110   free(result);
   111 }
   114 static filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
   115     filelist_t* list) {
   117   DIR *dirf;
   118   struct dirent *entry;
   119   struct stat statbuf;
   121   if ((dirf = opendir(scanner.dir)) == NULL) {
   122     fprintf(stderr, "%s - ", scanner.dir);
   123     perror("Directory access failed");
   124     return 0;
   125   }
   127   while ((entry = readdir(dirf)) != NULL) {
   128     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
   130       /* Create new filelist entry */
   131       filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
   132       newentry->next = NULL;
   134       newentry->displayname_len = strlen(entry->d_name);
   135       newentry->displayname = (char*) malloc(newentry->displayname_len+1);
   136       memcpy(newentry->displayname, entry->d_name, newentry->displayname_len);
   137       newentry->displayname[newentry->displayname_len] = 0;
   139       newentry->st_mode = 0;
   141       /* Construct absolute pathname string */
   142       size_t dirnamelen = strlen(scanner.dir);
   143       char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len);
   144       memcpy(filename, scanner.dir, dirnamelen);
   145       filename[dirnamelen] = settings->fileSeparator;
   146       memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
   147       filename[1+dirnamelen+newentry->displayname_len] = 0;
   148       newentry->filename = filename;
   150       /* Obtain file extension */
   151       newentry->ext = strrchr(newentry->displayname, '.');
   153       /* Check for subdirectory */
   154       if (stat(filename, &statbuf) == 0) {
   155         newentry->st_mode = statbuf.st_mode;
   156       } else {
   157         perror("  Error in stat call");
   158         continue;
   159       }
   161       if (list) {
   162         // create fake root to have a pointer on the true root
   163         filelist_t root;
   164         root.next = list;
   165         filelist_t *parent = &root;
   166         while (parent->next &&
   167             (strcasecmp(parent->next->displayname, newentry->displayname) < 0 ||
   168               (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode))
   169             ) &&
   170             (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode))
   171             ) {
   172           parent = parent->next;
   173         }
   174         newentry->next = parent->next;
   175         parent->next = newentry;
   176         list = root.next;
   177       } else {
   178         list = newentry;
   179       }
   180     }
   181   }
   183   closedir(dirf);
   185   return list;
   186 }
   188 void scanDirectory(scanner_t scanner, settings_t* settings,
   189     string_list_t* output, scanresult_t* result) {
   191   result->lines = 0;
   192   int a;
   193   bool bfile;
   194   char *outbuf;
   196   filelist_t *filelist = buildFileList(scanner, settings, NULL);
   198   while (filelist != NULL) {
   200     /* Scan subdirectories */
   201     if (!S_ISREG(filelist->st_mode)) {
   202       if (settings->recursive && S_ISDIR(filelist->st_mode)) {
   203         string_list_t *recoutput = new_string_list_t();
   204         scanresult_t recresult;
   205         recresult.ext = result->ext;
   206         scanDirectory(
   207             (scanner_t) {filelist->filename, scanner.spaces+1},
   208             settings, recoutput, &recresult);
   209         result->lines += recresult.lines;
   210         if (!settings->matchesOnly || recoutput->count > 0) {
   211           outbuf = (char*) malloc(81);
   212           snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
   213               filelist->displayname_len+scanner.spaces, filelist->displayname,
   214               60-filelist->displayname_len-scanner.spaces-1, "",
   215               recresult.lines);
   216           add_string(output, outbuf);
   217           for (int i = 0 ; i < recoutput->count ; i++) {
   218             add_string(output, recoutput->items[i]);
   219           }
   220         }
   221         destroy_string_list_t(recoutput);
   222       } else {
   223         outbuf = (char*) malloc(81);
   224         snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
   225           filelist->displayname);
   226         add_string(output, outbuf);
   227       }
   228     } else {
   229       if ((settings->includeSuffixes->count == 0
   230         || testSuffix(filelist->displayname, settings->includeSuffixes))
   231         && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
   233         /* Count lines */
   234         int lines = 0;
   235         bfile = false;
   236         bfile_reset(settings->bfileHeuristics);
   237         regex_parser_reset(settings->regex);
   238         char line_buffer[REGEX_MAX_LINELENGTH];
   239         int line_buffer_offset = 0;
   241         FILE *file = fopen(filelist->filename, "r");
   242         if (file == NULL) {
   243           outbuf = (char*) malloc(81);
   244           snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
   245               filelist->displayname);
   246           add_string(output, outbuf);
   247           perror("  File acces failed");
   248         } else {
   249           do {
   250             a = fgetc(file);
   252             bfile = bfile_check(settings->bfileHeuristics, a);
   254             if (a == 10 || a == EOF) {
   255               line_buffer[line_buffer_offset] = 0;
   256               if (regex_parser_do(settings->regex, line_buffer) == 0) {
   257                 /* Only subtract lines when matching has finished */
   258                 if (!regex_parser_matching(settings->regex)) {
   259                   lines -= settings->regex->matched_lines;
   260                 }
   261               }
   263               line_buffer_offset = 0;
   264               lines++;
   265             } else {
   266               if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
   267                 line_buffer[line_buffer_offset] = a;
   268                 line_buffer_offset++;
   269               } else {
   270                 line_buffer[line_buffer_offset-1] = 0;
   271                 settings->confusing_lnlen = true;
   272               }
   273             }
   274           } while (!bfile && a != EOF);
   275           fclose(file);
   277           /* Print and sum line count */
   278           if (bfile) {
   279             if (!settings->matchesOnly) {
   280               outbuf = (char*) malloc(81);
   281               snprintf(outbuf, 81,
   282                   "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
   283                   filelist->displayname,
   284                   60-filelist->displayname_len-scanner.spaces, "", "binary");
   285               add_string(output, outbuf);
   286             }
   287           } else {
   288             addLinesPerExtension(result->ext, filelist->ext, lines);
   289             result->lines += lines;
   290             outbuf = (char*) malloc(81);
   291             snprintf(outbuf, 81, "%*s%*s%13d lines\n",
   292                 filelist->displayname_len+scanner.spaces, filelist->displayname,
   293                 60-filelist->displayname_len-scanner.spaces, "", lines);
   294             add_string(output, outbuf);
   295           }
   296         }
   297       } else {
   298         if (!settings->matchesOnly) {
   299           /* Print hint */
   300           outbuf = (char*) malloc(81);
   301           snprintf(outbuf, 81, "%*s%*s%19s\n",
   302               filelist->displayname_len+scanner.spaces, filelist->displayname,
   303               60-filelist->displayname_len-scanner.spaces, "", "no match");
   304           add_string(output, outbuf);
   305         }
   306       }
   307     }
   309     free(filelist->filename);
   310     free(filelist->displayname);
   311     filelist_t *freethis = filelist;
   312     filelist = filelist->next;
   313     free(freethis);
   314   }
   315 }

mercurial