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