Fri, 28 Dec 2012 15:44:28 +0100
moved src files to src subdirectory and added licence text
universe@10 | 1 | /* |
universe@34 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@34 | 3 | * Copyright 2011 Mike Becker. All rights reserved. |
universe@34 | 4 | * |
universe@34 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@34 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@34 | 7 | * |
universe@34 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@34 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@34 | 10 | * |
universe@34 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@34 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@34 | 13 | * documentation and/or other materials provided with the distribution. |
universe@34 | 14 | * |
universe@34 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@34 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@34 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@34 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@34 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@34 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@34 | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@34 | 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@34 | 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@34 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@34 | 25 | * |
universe@20 | 26 | * scanner.c |
universe@10 | 27 | * |
universe@10 | 28 | * Created on: 23.05.2011 |
universe@20 | 29 | * Author: Mike |
universe@10 | 30 | */ |
universe@1 | 31 | |
universe@1 | 32 | |
universe@10 | 33 | #include "scanner.h" |
universe@10 | 34 | #include "suffix_fnc.h" |
universe@21 | 35 | #include "bfile_heuristics.h" |
universe@27 | 36 | #include "regex_parser.h" |
universe@23 | 37 | #include <sys/stat.h> |
universe@3 | 38 | |
universe@23 | 39 | int scanDirectory(scanner_t scanner, settings_t* settings) { |
universe@23 | 40 | |
universe@23 | 41 | DIR *dirf; |
universe@3 | 42 | struct dirent *entry; |
universe@18 | 43 | int lines, a; |
universe@3 | 44 | int lineSum = 0; |
universe@21 | 45 | bool bfile; |
universe@23 | 46 | struct stat statbuf; |
universe@3 | 47 | |
universe@23 | 48 | if ((dirf = opendir(scanner.dir)) == NULL) { |
universe@34 | 49 | printf("%s", scanner.dir); |
universe@23 | 50 | perror(" Directory access failed"); |
universe@23 | 51 | return 0; |
universe@23 | 52 | } |
universe@23 | 53 | |
universe@23 | 54 | while ((entry = readdir(dirf)) != NULL) { |
universe@3 | 55 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@23 | 56 | /* Construct tree view and absolute pathname strings */ |
universe@23 | 57 | char entryname[strlen(entry->d_name)+scanner.spaces]; |
universe@23 | 58 | for (int t = 0 ; t < scanner.spaces ; t++) { |
universe@3 | 59 | entryname[t]=' '; |
universe@3 | 60 | } |
universe@23 | 61 | entryname[scanner.spaces] = 0; |
universe@3 | 62 | strcat(entryname, entry->d_name); |
universe@3 | 63 | |
universe@23 | 64 | char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; |
universe@23 | 65 | strcpy(filename, scanner.dir); |
universe@14 | 66 | strncat(filename, &settings->fileSeparator, 1); |
universe@14 | 67 | strcat(filename, entry->d_name); |
universe@14 | 68 | |
universe@22 | 69 | /* Check for subdirectory */ |
universe@23 | 70 | if (stat(filename, &statbuf) == 0) { |
universe@23 | 71 | if (!(statbuf.st_mode & S_IFREG)) { |
universe@23 | 72 | printf("%-60s\n", entryname); |
universe@23 | 73 | if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { |
universe@23 | 74 | lineSum += scanDirectory( |
universe@23 | 75 | (scanner_t) {filename, scanner.spaces+1}, settings); |
universe@23 | 76 | } |
universe@23 | 77 | continue; |
universe@3 | 78 | } |
universe@23 | 79 | } else { |
universe@23 | 80 | perror(" Error in stat call"); |
universe@3 | 81 | continue; |
universe@3 | 82 | } |
universe@3 | 83 | |
universe@30 | 84 | if ((settings->includeSuffixes->count == 0 |
universe@30 | 85 | || testSuffix(filename, settings->includeSuffixes)) |
universe@30 | 86 | && !testSuffix(filename, settings->excludeSuffixes)) { |
universe@25 | 87 | /* Count lines */ |
universe@25 | 88 | lines = 0; |
universe@25 | 89 | bfile = false; |
universe@25 | 90 | bfile_reset(settings->bfileHeuristics); |
universe@27 | 91 | char line_buffer[REGEX_MAX_LINELENGTH]; |
universe@25 | 92 | int line_buffer_offset = 0; |
universe@25 | 93 | |
universe@3 | 94 | FILE *file = fopen(filename, "r"); |
universe@3 | 95 | if (file == NULL) { |
universe@34 | 96 | printf("%s", entryname); |
universe@3 | 97 | perror(" File acces failed"); |
universe@3 | 98 | continue; |
universe@3 | 99 | } |
universe@3 | 100 | |
universe@3 | 101 | do { |
universe@3 | 102 | a = fgetc(file); |
universe@3 | 103 | |
universe@21 | 104 | bfile = bfile_check(settings->bfileHeuristics, a); |
universe@21 | 105 | |
universe@28 | 106 | if (a == 10 || a == EOF) { |
universe@25 | 107 | line_buffer[line_buffer_offset] = 0; |
universe@28 | 108 | if (regex_parser_do(settings->regex, line_buffer) == 0) { |
universe@28 | 109 | /* Only subtract lines when matching has finished */ |
universe@28 | 110 | if (!regex_parser_matching(settings->regex)) { |
universe@28 | 111 | lines -= settings->regex->matched_lines; |
universe@28 | 112 | } |
universe@28 | 113 | } |
universe@25 | 114 | |
universe@25 | 115 | line_buffer_offset = 0; |
universe@3 | 116 | lines++; |
universe@25 | 117 | } else { |
universe@27 | 118 | if (line_buffer_offset < REGEX_MAX_LINELENGTH) { |
universe@25 | 119 | line_buffer[line_buffer_offset] = a; |
universe@25 | 120 | line_buffer_offset++; |
universe@25 | 121 | } else { |
universe@25 | 122 | line_buffer[line_buffer_offset-1] = 0; |
universe@25 | 123 | settings->confusing_lnlen = true; |
universe@25 | 124 | } |
universe@3 | 125 | } |
universe@21 | 126 | } while (!bfile && a != EOF); |
universe@3 | 127 | fclose(file); |
universe@3 | 128 | |
universe@22 | 129 | /* Print and sum line count */ |
universe@21 | 130 | if (bfile) { |
universe@22 | 131 | if (!settings->matchesOnly) { |
universe@22 | 132 | printf("%-60s%19s\n", entryname, "binary"); |
universe@22 | 133 | } |
universe@21 | 134 | } else { |
universe@22 | 135 | lineSum += lines; |
universe@3 | 136 | printf("%-60s%13d lines\n", entryname, lines); |
universe@21 | 137 | } |
universe@16 | 138 | } else { |
universe@3 | 139 | if (!settings->matchesOnly) { |
universe@22 | 140 | /* Print hint */ |
universe@21 | 141 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 142 | } |
universe@3 | 143 | } |
universe@3 | 144 | } |
universe@3 | 145 | } |
universe@23 | 146 | |
universe@23 | 147 | closedir(dirf); |
universe@23 | 148 | |
universe@3 | 149 | return lineSum; |
universe@3 | 150 | } |