src/scanner.c

Fri, 17 May 2013 14:41:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 17 May 2013 14:41:44 +0200
changeset 40
5938a9b74e8e
parent 36
a7ff583e153f
child 41
c2e73e175341
permissions
-rw-r--r--

improved printing the results and fixed bug where the filename was displayed as an empty string

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
 * Copyright 2013 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>

int scanDirectory(scanner_t scanner, settings_t* settings) {

  DIR *dirf;
  struct dirent *entry;
  int entrynamelen;
  int lines, a;
  int lineSum = 0;
  bool bfile;
  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) {
      /* Construct absolute pathname string */
      entrynamelen = strlen(entry->d_name);
      char filename[(1+strlen(scanner.dir)+entrynamelen)];
      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("%*s\n", entrynamelen+scanner.spaces, entry->d_name);
          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 ((settings->includeSuffixes->count == 0
          || testSuffix(filename, settings->includeSuffixes))
          && !testSuffix(filename, settings->excludeSuffixes)) {
        /* 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("%*s", entrynamelen+scanner.spaces, entry->d_name);
          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("%*s%*s%19s\n",
                entrynamelen+scanner.spaces, entry->d_name,
                60-entrynamelen-scanner.spaces, "", "binary");
          }
        } else {
          lineSum += lines;
          printf("%*s%*s%13d lines\n",
              entrynamelen+scanner.spaces, entry->d_name,
              60-entrynamelen-scanner.spaces, "", lines);
        }
      } else {
        if (!settings->matchesOnly) {
          /* Print hint */
          printf("%*s%*s%19s\n",
              entrynamelen+scanner.spaces, entry->d_name,
              60-entrynamelen-scanner.spaces, "", "no match");
        }
      }
    }
  }

  closedir(dirf);

  return lineSum;
}

mercurial