src/scanner.c

Wed, 22 May 2013 10:57:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 22 May 2013 10:57:17 +0200
changeset 43
104e75d18ede
parent 42
0402b9b41b0a
child 44
9574a181ec26
permissions
-rw-r--r--

fixed bug: line sum of subdirectories were not added to total sum

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

typedef struct filelist filelist_t;

struct filelist {
  char *displayname;
  int displayname_len;
  char *filename;
  int st_mode;
  filelist_t *next;
};

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) {
    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) {
      
      /* 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;

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

int scanDirectory(scanner_t scanner, settings_t* settings) {

  int lines, a;
  int lineSum = 0;
  bool bfile;

  filelist_t *filelist = buildFileList(scanner, settings, NULL);

  while (filelist != NULL) {

    /* Scan subdirectories */
    if (!S_ISREG(filelist->st_mode)) {
      printf("%*s\n", filelist->displayname_len+scanner.spaces,
          filelist->displayname);
      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
        lineSum += scanDirectory(
            (scanner_t) {filelist->filename, scanner.spaces+1}, settings);
      }
    } else {
      if ((settings->includeSuffixes->count == 0
        || testSuffix(filelist->displayname, settings->includeSuffixes))
        && !testSuffix(filelist->displayname, 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(filelist->filename, "r");
        if (file == NULL) {
          printf("%*s", filelist->displayname_len+scanner.spaces,
              filelist->displayname);
          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) {
              printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
                  filelist->displayname,
                  60-filelist->displayname_len-scanner.spaces, "", "binary");
            }
          } else {
            lineSum += lines;
            printf("%*s%*s%13d lines\n",
                filelist->displayname_len+scanner.spaces, filelist->displayname,
                60-filelist->displayname_len-scanner.spaces, "", lines);
          }
        }
      } else {
        if (!settings->matchesOnly) {
          /* Print hint */
          printf("%*s%*s%19s\n",
              filelist->displayname_len+scanner.spaces, filelist->displayname,
              60-filelist->displayname_len-scanner.spaces, "", "no match");
        }
      }
    }
    
    free(filelist->filename);
    free(filelist->displayname);
    filelist_t *freethis = filelist;
    filelist = filelist->next;
    free(freethis);
  }

  return lineSum;
}

mercurial