src/scanner.c

changeset 34
fa9bda32de17
parent 30
d642fdb6745e
child 36
a7ff583e153f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scanner.c	Fri Dec 28 15:44:28 2012 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     1.6 + * Copyright 2011 Mike Becker. All rights reserved.
     1.7 + * 
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions are met:
    1.10 + * 
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + * notice, this list of conditions and the following disclaimer.
    1.13 + * 
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + * notice, this list of conditions and the following disclaimer in the
    1.16 + * documentation and/or other materials provided with the distribution.
    1.17 + * 
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.21 + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    1.22 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.23 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    1.25 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.26 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.27 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    1.28 + *
    1.29 + * scanner.c
    1.30 + *
    1.31 + *  Created on: 23.05.2011
    1.32 + *      Author: Mike
    1.33 + */
    1.34 +
    1.35 +
    1.36 +#include "scanner.h"
    1.37 +#include "suffix_fnc.h"
    1.38 +#include "bfile_heuristics.h"
    1.39 +#include "regex_parser.h"
    1.40 +#include <sys/stat.h>
    1.41 +
    1.42 +int scanDirectory(scanner_t scanner, settings_t* settings) {
    1.43 +
    1.44 +  DIR *dirf;
    1.45 +  struct dirent *entry;
    1.46 +  int lines, a;
    1.47 +  int lineSum = 0;
    1.48 +  bool bfile;
    1.49 +  struct stat statbuf;
    1.50 +
    1.51 +  if ((dirf = opendir(scanner.dir)) == NULL) {
    1.52 +    printf("%s", scanner.dir);
    1.53 +    perror("  Directory access failed");
    1.54 +    return 0;
    1.55 +  }
    1.56 +
    1.57 +  while ((entry = readdir(dirf)) != NULL) {
    1.58 +    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    1.59 +      /* Construct tree view and absolute pathname strings */
    1.60 +      char entryname[strlen(entry->d_name)+scanner.spaces];
    1.61 +      for (int t = 0 ; t < scanner.spaces ; t++) {
    1.62 +        entryname[t]=' ';
    1.63 +      }
    1.64 +      entryname[scanner.spaces] = 0;
    1.65 +      strcat(entryname, entry->d_name);
    1.66 +  
    1.67 +      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
    1.68 +      strcpy(filename, scanner.dir);
    1.69 +      strncat(filename, &settings->fileSeparator, 1);
    1.70 +      strcat(filename, entry->d_name);
    1.71 +
    1.72 +      /* Check for subdirectory */
    1.73 +      if (stat(filename, &statbuf) == 0) {
    1.74 +        if (!(statbuf.st_mode & S_IFREG)) {
    1.75 +          printf("%-60s\n", entryname);
    1.76 +          if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    1.77 +            lineSum += scanDirectory(
    1.78 +                (scanner_t) {filename, scanner.spaces+1}, settings);
    1.79 +          }
    1.80 +          continue;
    1.81 +        }
    1.82 +      } else {
    1.83 +        perror("  Error in stat call");
    1.84 +        continue;
    1.85 +      }
    1.86 +
    1.87 +      if ((settings->includeSuffixes->count == 0
    1.88 +          || testSuffix(filename, settings->includeSuffixes))
    1.89 +          && !testSuffix(filename, settings->excludeSuffixes)) {
    1.90 +        /* Count lines */
    1.91 +        lines = 0;
    1.92 +        bfile = false;
    1.93 +        bfile_reset(settings->bfileHeuristics);
    1.94 +        char line_buffer[REGEX_MAX_LINELENGTH];
    1.95 +        int line_buffer_offset = 0;
    1.96 +
    1.97 +        FILE *file = fopen(filename, "r");
    1.98 +        if (file == NULL) {
    1.99 +          printf("%s", entryname);
   1.100 +          perror("  File acces failed");
   1.101 +          continue;
   1.102 +        }
   1.103 +
   1.104 +        do {
   1.105 +          a = fgetc(file);
   1.106 +
   1.107 +          bfile = bfile_check(settings->bfileHeuristics, a);
   1.108 +
   1.109 +          if (a == 10 || a == EOF) {
   1.110 +            line_buffer[line_buffer_offset] = 0;
   1.111 +            if (regex_parser_do(settings->regex, line_buffer) == 0) {
   1.112 +              /* Only subtract lines when matching has finished */
   1.113 +              if (!regex_parser_matching(settings->regex)) {
   1.114 +                lines -= settings->regex->matched_lines;
   1.115 +              }
   1.116 +            }
   1.117 +
   1.118 +            line_buffer_offset = 0;
   1.119 +            lines++;
   1.120 +          } else {
   1.121 +            if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
   1.122 +              line_buffer[line_buffer_offset] = a;
   1.123 +              line_buffer_offset++;
   1.124 +            } else {
   1.125 +              line_buffer[line_buffer_offset-1] = 0;
   1.126 +              settings->confusing_lnlen = true;
   1.127 +            }
   1.128 +          }
   1.129 +        } while (!bfile && a != EOF);
   1.130 +        fclose(file);
   1.131 +
   1.132 +        /* Print and sum line count */
   1.133 +        if (bfile) {
   1.134 +          if (!settings->matchesOnly) {
   1.135 +            printf("%-60s%19s\n", entryname, "binary");
   1.136 +          }
   1.137 +        } else {
   1.138 +          lineSum += lines;
   1.139 +          printf("%-60s%13d lines\n", entryname, lines);
   1.140 +        }
   1.141 +      } else {
   1.142 +        if (!settings->matchesOnly) {
   1.143 +          /* Print hint */
   1.144 +          printf("%-60s%19s\n", entryname, "no match");
   1.145 +        }
   1.146 +      }
   1.147 +    }
   1.148 +  }
   1.149 +
   1.150 +  closedir(dirf);
   1.151 +
   1.152 +  return lineSum;
   1.153 +}

mercurial