Thu, 26 Jan 2012 15:55:52 +0100
added regexp_parser struct and compile function
1 /*
2 * scanner.c
3 *
4 * Created on: 23.05.2011
5 * Author: Mike
6 */
9 #include "scanner.h"
10 #include "suffix_fnc.h"
11 #include "bfile_heuristics.h"
12 #include "regex_parser.h"
13 #include <sys/stat.h>
15 int scanDirectory(scanner_t scanner, settings_t* settings) {
17 DIR *dirf;
18 struct dirent *entry;
19 int lines, a;
20 int lineSum = 0;
21 bool bfile;
22 struct stat statbuf;
24 if ((dirf = opendir(scanner.dir)) == NULL) {
25 printf(scanner.dir);
26 perror(" Directory access failed");
27 return 0;
28 }
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);
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);
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 }
60 if (testSuffix(filename, settings)) {
61 /* Count lines */
62 lines = 0;
63 bfile = false;
64 bfile_reset(settings->bfileHeuristics);
65 char line_buffer[REGEX_MAX_LINELENGTH];
66 int line_buffer_offset = 0;
68 FILE *file = fopen(filename, "r");
69 if (file == NULL) {
70 printf(entryname);
71 perror(" File acces failed");
72 continue;
73 }
75 do {
76 a = fgetc(file);
78 bfile = bfile_check(settings->bfileHeuristics, a);
80 if (a == 10) {
81 line_buffer[line_buffer_offset] = 0;
82 /* TODO: do regex parsing */
84 line_buffer_offset = 0;
85 lines++;
86 } else {
87 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
88 line_buffer[line_buffer_offset] = a;
89 line_buffer_offset++;
90 } else {
91 line_buffer[line_buffer_offset-1] = 0;
92 settings->confusing_lnlen = true;
93 }
94 }
95 } while (!bfile && a != EOF);
96 fclose(file);
98 /* Print and sum line count */
99 if (bfile) {
100 if (!settings->matchesOnly) {
101 printf("%-60s%19s\n", entryname, "binary");
102 }
103 } else {
104 lineSum += lines;
105 printf("%-60s%13d lines\n", entryname, lines);
106 }
107 } else {
108 if (!settings->matchesOnly) {
109 /* Print hint */
110 printf("%-60s%19s\n", entryname, "no match");
111 }
112 }
113 }
114 }
116 closedir(dirf);
118 return lineSum;
119 }