Thu, 01 Dec 2011 17:04:30 +0100
Added line buffer (and warning message - there is no regexp parser, though)
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 <sys/stat.h>
14 int scanDirectory(scanner_t scanner, settings_t* settings) {
16 DIR *dirf;
17 struct dirent *entry;
18 int lines, a;
19 int lineSum = 0;
20 bool bfile;
21 struct stat statbuf;
23 if ((dirf = opendir(scanner.dir)) == NULL) {
24 printf(scanner.dir);
25 perror(" Directory access failed");
26 return 0;
27 }
29 while ((entry = readdir(dirf)) != NULL) {
30 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
31 /* Construct tree view and absolute pathname strings */
32 char entryname[strlen(entry->d_name)+scanner.spaces];
33 for (int t = 0 ; t < scanner.spaces ; t++) {
34 entryname[t]=' ';
35 }
36 entryname[scanner.spaces] = 0;
37 strcat(entryname, entry->d_name);
39 char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
40 strcpy(filename, scanner.dir);
41 strncat(filename, &settings->fileSeparator, 1);
42 strcat(filename, entry->d_name);
44 /* Check for subdirectory */
45 if (stat(filename, &statbuf) == 0) {
46 if (!(statbuf.st_mode & S_IFREG)) {
47 printf("%-60s\n", entryname);
48 if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
49 lineSum += scanDirectory(
50 (scanner_t) {filename, scanner.spaces+1}, settings);
51 }
52 continue;
53 }
54 } else {
55 perror(" Error in stat call");
56 continue;
57 }
59 if (testSuffix(filename, settings)) {
60 /* Count lines */
61 lines = 0;
62 bfile = false;
63 bfile_reset(settings->bfileHeuristics);
64 char line_buffer[2048];
65 int line_buffer_offset = 0;
67 FILE *file = fopen(filename, "r");
68 if (file == NULL) {
69 printf(entryname);
70 perror(" File acces failed");
71 continue;
72 }
74 do {
75 a = fgetc(file);
77 bfile = bfile_check(settings->bfileHeuristics, a);
79 if (a == 10) {
80 line_buffer[line_buffer_offset] = 0;
81 /* TODO: do regex parsing */
83 line_buffer_offset = 0;
84 lines++;
85 } else {
86 if (line_buffer_offset < 2048) {
87 line_buffer[line_buffer_offset] = a;
88 line_buffer_offset++;
89 } else {
90 line_buffer[line_buffer_offset-1] = 0;
91 settings->confusing_lnlen = true;
92 }
93 }
94 } while (!bfile && a != EOF);
95 fclose(file);
97 /* Print and sum line count */
98 if (bfile) {
99 if (!settings->matchesOnly) {
100 printf("%-60s%19s\n", entryname, "binary");
101 }
102 } else {
103 lineSum += lines;
104 printf("%-60s%13d lines\n", entryname, lines);
105 }
106 } else {
107 if (!settings->matchesOnly) {
108 /* Print hint */
109 printf("%-60s%19s\n", entryname, "no match");
110 }
111 }
112 }
113 }
115 closedir(dirf);
117 return lineSum;
118 }