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

/*
 * 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>

int scanDirectory(scanner_t scanner, settings_t* settings) {

  DIR *dirf;
  struct dirent *entry;
  int lines, a;
  int lineSum = 0;
  bool bfile;
  struct stat statbuf;

  if ((dirf = opendir(scanner.dir)) == NULL) {
    printf(scanner.dir);
    perror("  Directory access failed");
    return 0;
  }

  while ((entry = readdir(dirf)) != NULL) {
    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
      /* Construct tree view and absolute pathname strings */
      char entryname[strlen(entry->d_name)+scanner.spaces];
      for (int t = 0 ; t < scanner.spaces ; t++) {
        entryname[t]=' ';
      }
      entryname[scanner.spaces] = 0;
      strcat(entryname, entry->d_name);
  
      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
      strcpy(filename, scanner.dir);
      strncat(filename, &settings->fileSeparator, 1);
      strcat(filename, entry->d_name);

      /* Check for subdirectory */
      if (stat(filename, &statbuf) == 0) {
        if (!(statbuf.st_mode & S_IFREG)) {
          printf("%-60s\n", entryname);
          if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
            lineSum += scanDirectory(
                (scanner_t) {filename, scanner.spaces+1}, settings);
          }
          continue;
        }
      } else {
        perror("  Error in stat call");
        continue;
      }

      if (testSuffix(filename, settings)) {
        /* Count lines */
        lines = 0;
        bfile = false;
        bfile_reset(settings->bfileHeuristics);
        char line_buffer[REGEX_MAX_LINELENGTH];
        int line_buffer_offset = 0;

        FILE *file = fopen(filename, "r");
        if (file == NULL) {
          printf(entryname);
          perror("  File acces failed");
          continue;
        }

        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) {
            printf("%-60s%19s\n", entryname, "binary");
          }
        } else {
          lineSum += lines;
          printf("%-60s%13d lines\n", entryname, lines);
        }
      } else {
        if (!settings->matchesOnly) {
          /* Print hint */
          printf("%-60s%19s\n", entryname, "no match");
        }
      }
    }
  }

  closedir(dirf);

  return lineSum;
}

mercurial