universe@10: /* universe@34: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@34: * Copyright 2011 Mike Becker. All rights reserved. universe@34: * universe@34: * Redistribution and use in source and binary forms, with or without universe@34: * modification, are permitted provided that the following conditions are met: universe@34: * universe@34: * 1. Redistributions of source code must retain the above copyright universe@34: * notice, this list of conditions and the following disclaimer. universe@34: * universe@34: * 2. Redistributions in binary form must reproduce the above copyright universe@34: * notice, this list of conditions and the following disclaimer in the universe@34: * documentation and/or other materials provided with the distribution. universe@34: * universe@34: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@34: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@34: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE universe@34: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE universe@34: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL universe@34: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR universe@34: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER universe@34: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, universe@34: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE universe@34: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. universe@34: * universe@20: * scanner.c universe@10: * universe@10: * Created on: 23.05.2011 universe@20: * Author: Mike universe@10: */ universe@1: universe@1: universe@10: #include "scanner.h" universe@10: #include "suffix_fnc.h" universe@21: #include "bfile_heuristics.h" universe@27: #include "regex_parser.h" universe@23: #include universe@3: universe@23: int scanDirectory(scanner_t scanner, settings_t* settings) { universe@23: universe@23: DIR *dirf; universe@3: struct dirent *entry; universe@18: int lines, a; universe@3: int lineSum = 0; universe@21: bool bfile; universe@23: struct stat statbuf; universe@3: universe@23: if ((dirf = opendir(scanner.dir)) == NULL) { universe@34: printf("%s", scanner.dir); universe@23: perror(" Directory access failed"); universe@23: return 0; universe@23: } universe@23: universe@23: while ((entry = readdir(dirf)) != NULL) { universe@3: if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { universe@23: /* Construct tree view and absolute pathname strings */ universe@23: char entryname[strlen(entry->d_name)+scanner.spaces]; universe@23: for (int t = 0 ; t < scanner.spaces ; t++) { universe@3: entryname[t]=' '; universe@3: } universe@23: entryname[scanner.spaces] = 0; universe@3: strcat(entryname, entry->d_name); universe@3: universe@23: char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; universe@23: strcpy(filename, scanner.dir); universe@14: strncat(filename, &settings->fileSeparator, 1); universe@14: strcat(filename, entry->d_name); universe@14: universe@22: /* Check for subdirectory */ universe@23: if (stat(filename, &statbuf) == 0) { universe@23: if (!(statbuf.st_mode & S_IFREG)) { universe@23: printf("%-60s\n", entryname); universe@23: if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { universe@23: lineSum += scanDirectory( universe@23: (scanner_t) {filename, scanner.spaces+1}, settings); universe@23: } universe@23: continue; universe@3: } universe@23: } else { universe@23: perror(" Error in stat call"); universe@3: continue; universe@3: } universe@3: universe@30: if ((settings->includeSuffixes->count == 0 universe@30: || testSuffix(filename, settings->includeSuffixes)) universe@30: && !testSuffix(filename, settings->excludeSuffixes)) { universe@25: /* Count lines */ universe@25: lines = 0; universe@25: bfile = false; universe@25: bfile_reset(settings->bfileHeuristics); universe@27: char line_buffer[REGEX_MAX_LINELENGTH]; universe@25: int line_buffer_offset = 0; universe@25: universe@3: FILE *file = fopen(filename, "r"); universe@3: if (file == NULL) { universe@34: printf("%s", entryname); universe@3: perror(" File acces failed"); universe@3: continue; universe@3: } universe@3: universe@3: do { universe@3: a = fgetc(file); universe@3: universe@21: bfile = bfile_check(settings->bfileHeuristics, a); universe@21: universe@28: if (a == 10 || a == EOF) { universe@25: line_buffer[line_buffer_offset] = 0; universe@28: if (regex_parser_do(settings->regex, line_buffer) == 0) { universe@28: /* Only subtract lines when matching has finished */ universe@28: if (!regex_parser_matching(settings->regex)) { universe@28: lines -= settings->regex->matched_lines; universe@28: } universe@28: } universe@25: universe@25: line_buffer_offset = 0; universe@3: lines++; universe@25: } else { universe@27: if (line_buffer_offset < REGEX_MAX_LINELENGTH) { universe@25: line_buffer[line_buffer_offset] = a; universe@25: line_buffer_offset++; universe@25: } else { universe@25: line_buffer[line_buffer_offset-1] = 0; universe@25: settings->confusing_lnlen = true; universe@25: } universe@3: } universe@21: } while (!bfile && a != EOF); universe@3: fclose(file); universe@3: universe@22: /* Print and sum line count */ universe@21: if (bfile) { universe@22: if (!settings->matchesOnly) { universe@22: printf("%-60s%19s\n", entryname, "binary"); universe@22: } universe@21: } else { universe@22: lineSum += lines; universe@3: printf("%-60s%13d lines\n", entryname, lines); universe@21: } universe@16: } else { universe@3: if (!settings->matchesOnly) { universe@22: /* Print hint */ universe@21: printf("%-60s%19s\n", entryname, "no match"); universe@3: } universe@3: } universe@3: } universe@3: } universe@23: universe@23: closedir(dirf); universe@23: universe@3: return lineSum; universe@3: }