scanner.c

Thu, 02 Feb 2012 16:55:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 02 Feb 2012 16:55:51 +0100
changeset 29
fa625066ae52
parent 28
72a98cbcb9f1
child 30
d642fdb6745e
permissions
-rw-r--r--

fixed author note

     1 /*
     2  * scanner.c
     3  *
     4  *  Created on: 23.05.2011
     5  *      Author: Mike
     6  */
     9 #include "scanner.h"
    10 #include "suffix_fnc.h"
    11 #include "bfile_heuristics.h"
    12 #include "regex_parser.h"
    13 #include <sys/stat.h>
    15 int scanDirectory(scanner_t scanner, settings_t* settings) {
    17   DIR *dirf;
    18   struct dirent *entry;
    19   int lines, a;
    20   int lineSum = 0;
    21   bool bfile;
    22   struct stat statbuf;
    24   if ((dirf = opendir(scanner.dir)) == NULL) {
    25     printf(scanner.dir);
    26     perror("  Directory access failed");
    27     return 0;
    28   }
    30   while ((entry = readdir(dirf)) != NULL) {
    31     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    32       /* Construct tree view and absolute pathname strings */
    33       char entryname[strlen(entry->d_name)+scanner.spaces];
    34       for (int t = 0 ; t < scanner.spaces ; t++) {
    35         entryname[t]=' ';
    36       }
    37       entryname[scanner.spaces] = 0;
    38       strcat(entryname, entry->d_name);
    40       char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
    41       strcpy(filename, scanner.dir);
    42       strncat(filename, &settings->fileSeparator, 1);
    43       strcat(filename, entry->d_name);
    45       /* Check for subdirectory */
    46       if (stat(filename, &statbuf) == 0) {
    47         if (!(statbuf.st_mode & S_IFREG)) {
    48           printf("%-60s\n", entryname);
    49           if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    50             lineSum += scanDirectory(
    51                 (scanner_t) {filename, scanner.spaces+1}, settings);
    52           }
    53           continue;
    54         }
    55       } else {
    56         perror("  Error in stat call");
    57         continue;
    58       }
    60       if (testSuffix(filename, settings)) {
    61         /* Count lines */
    62         lines = 0;
    63         bfile = false;
    64         bfile_reset(settings->bfileHeuristics);
    65         char line_buffer[REGEX_MAX_LINELENGTH];
    66         int line_buffer_offset = 0;
    68         FILE *file = fopen(filename, "r");
    69         if (file == NULL) {
    70           printf(entryname);
    71           perror("  File acces failed");
    72           continue;
    73         }
    75         do {
    76           a = fgetc(file);
    78           bfile = bfile_check(settings->bfileHeuristics, a);
    80           if (a == 10 || a == EOF) {
    81             line_buffer[line_buffer_offset] = 0;
    82             if (regex_parser_do(settings->regex, line_buffer) == 0) {
    83               /* Only subtract lines when matching has finished */
    84               if (!regex_parser_matching(settings->regex)) {
    85                 lines -= settings->regex->matched_lines;
    86               }
    87             }
    89             line_buffer_offset = 0;
    90             lines++;
    91           } else {
    92             if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
    93               line_buffer[line_buffer_offset] = a;
    94               line_buffer_offset++;
    95             } else {
    96               line_buffer[line_buffer_offset-1] = 0;
    97               settings->confusing_lnlen = true;
    98             }
    99           }
   100         } while (!bfile && a != EOF);
   101         fclose(file);
   103         /* Print and sum line count */
   104         if (bfile) {
   105           if (!settings->matchesOnly) {
   106             printf("%-60s%19s\n", entryname, "binary");
   107           }
   108         } else {
   109           lineSum += lines;
   110           printf("%-60s%13d lines\n", entryname, lines);
   111         }
   112       } else {
   113         if (!settings->matchesOnly) {
   114           /* Print hint */
   115           printf("%-60s%19s\n", entryname, "no match");
   116         }
   117       }
   118     }
   119   }
   121   closedir(dirf);
   123   return lineSum;
   124 }

mercurial