scanner.c

changeset 34
fa9bda32de17
parent 33
1a2d7298bc82
child 35
35120de6ee53
equal deleted inserted replaced
33:1a2d7298bc82 34:fa9bda32de17
1 /*
2 * scanner.c
3 *
4 * Created on: 23.05.2011
5 * Author: Mike
6 */
7
8
9 #include "scanner.h"
10 #include "suffix_fnc.h"
11 #include "bfile_heuristics.h"
12 #include "regex_parser.h"
13 #include <sys/stat.h>
14
15 int scanDirectory(scanner_t scanner, settings_t* settings) {
16
17 DIR *dirf;
18 struct dirent *entry;
19 int lines, a;
20 int lineSum = 0;
21 bool bfile;
22 struct stat statbuf;
23
24 if ((dirf = opendir(scanner.dir)) == NULL) {
25 printf(scanner.dir);
26 perror(" Directory access failed");
27 return 0;
28 }
29
30 while ((entry = readdir(dirf)) != NULL) {
31 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
32 /* Construct tree view and absolute pathname strings */
33 char entryname[strlen(entry->d_name)+scanner.spaces];
34 for (int t = 0 ; t < scanner.spaces ; t++) {
35 entryname[t]=' ';
36 }
37 entryname[scanner.spaces] = 0;
38 strcat(entryname, entry->d_name);
39
40 char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
41 strcpy(filename, scanner.dir);
42 strncat(filename, &settings->fileSeparator, 1);
43 strcat(filename, entry->d_name);
44
45 /* Check for subdirectory */
46 if (stat(filename, &statbuf) == 0) {
47 if (!(statbuf.st_mode & S_IFREG)) {
48 printf("%-60s\n", entryname);
49 if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
50 lineSum += scanDirectory(
51 (scanner_t) {filename, scanner.spaces+1}, settings);
52 }
53 continue;
54 }
55 } else {
56 perror(" Error in stat call");
57 continue;
58 }
59
60 if ((settings->includeSuffixes->count == 0
61 || testSuffix(filename, settings->includeSuffixes))
62 && !testSuffix(filename, settings->excludeSuffixes)) {
63 /* Count lines */
64 lines = 0;
65 bfile = false;
66 bfile_reset(settings->bfileHeuristics);
67 char line_buffer[REGEX_MAX_LINELENGTH];
68 int line_buffer_offset = 0;
69
70 FILE *file = fopen(filename, "r");
71 if (file == NULL) {
72 printf(entryname);
73 perror(" File acces failed");
74 continue;
75 }
76
77 do {
78 a = fgetc(file);
79
80 bfile = bfile_check(settings->bfileHeuristics, a);
81
82 if (a == 10 || a == EOF) {
83 line_buffer[line_buffer_offset] = 0;
84 if (regex_parser_do(settings->regex, line_buffer) == 0) {
85 /* Only subtract lines when matching has finished */
86 if (!regex_parser_matching(settings->regex)) {
87 lines -= settings->regex->matched_lines;
88 }
89 }
90
91 line_buffer_offset = 0;
92 lines++;
93 } else {
94 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
95 line_buffer[line_buffer_offset] = a;
96 line_buffer_offset++;
97 } else {
98 line_buffer[line_buffer_offset-1] = 0;
99 settings->confusing_lnlen = true;
100 }
101 }
102 } while (!bfile && a != EOF);
103 fclose(file);
104
105 /* Print and sum line count */
106 if (bfile) {
107 if (!settings->matchesOnly) {
108 printf("%-60s%19s\n", entryname, "binary");
109 }
110 } else {
111 lineSum += lines;
112 printf("%-60s%13d lines\n", entryname, lines);
113 }
114 } else {
115 if (!settings->matchesOnly) {
116 /* Print hint */
117 printf("%-60s%19s\n", entryname, "no match");
118 }
119 }
120 }
121 }
122
123 closedir(dirf);
124
125 return lineSum;
126 }

mercurial