Thu, 20 Oct 2011 14:13:56 +0200
Changed author comments + added signatures for upcomming bfile heuristics
1 /*
2 * scanner.c
3 *
4 * Created on: 23.05.2011
5 * Author: Mike
6 */
9 #include "scanner.h"
10 #include "suffix_fnc.h"
12 int scanDirectory(DIR *dir, const int spaces,
13 char* currdir, settings_t* settings) {
14 DIR *subdir;
15 struct dirent *entry;
16 int lines, a;
17 int lineSum = 0;
19 while ((entry = readdir(dir)) != NULL) {
20 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
21 // Print occurence
22 char entryname[strlen(entry->d_name)+spaces];
23 for (int t = 0 ; t < spaces ; t++) {
24 entryname[t]=' ';
25 }
26 entryname[spaces] = 0;
27 strcat(entryname, entry->d_name);
29 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
30 strcpy(filename, currdir);
31 strncat(filename, &settings->fileSeparator, 1);
32 strcat(filename, entry->d_name);
34 // Check for subdirectory
35 if ((subdir = opendir(filename)) != NULL) {
36 printf("%-60s\n", entryname);
37 if (settings->recursive) {
38 lineSum += scanDirectory(subdir, spaces+1, filename, settings);
39 }
40 closedir(subdir);
41 continue;
42 }
44 // Count lines
45 lines = 0;
46 if (testSuffix(filename, settings)) {
47 FILE *file = fopen(filename, "r");
48 if (file == NULL) {
49 perror(" File acces failed");
50 continue;
51 }
53 do {
54 a = fgetc(file);
56 if (a == 10) {
57 lines++;
58 }
59 } while (a != EOF);
60 fclose(file);
62 // Print line count
63 #ifdef _WIN32
64 printf("%-60s%13d lines\n", entryname, lines);
65 #else
66 printf("%-60s%14d lines\n", entryname, lines);
67 #endif /* _WIN32 */
69 lineSum += lines;
70 } else {
71 if (!settings->matchesOnly) {
72 // Print hint
73 #ifdef _WIN32
74 printf("%-60s%19s\n", entryname, "no match");
75 #else
76 printf("%-60s%20s\n", entryname, "no match");
77 #endif /* _WIN32 */
78 }
79 }
80 }
81 }
82 return lineSum;
83 }