Thu, 20 Oct 2011 17:29:23 +0200
completed binary file 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"
11 #include "bfile_heuristics.h"
13 int scanDirectory(DIR *dir, const int spaces,
14 char* currdir, settings_t* settings) {
15 DIR *subdir;
16 struct dirent *entry;
17 int lines, a;
18 int lineSum = 0;
19 bool bfile;
21 while ((entry = readdir(dir)) != NULL) {
22 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
23 /* Print occurence */
24 char entryname[strlen(entry->d_name)+spaces];
25 for (int t = 0 ; t < spaces ; t++) {
26 entryname[t]=' ';
27 }
28 entryname[spaces] = 0;
29 strcat(entryname, entry->d_name);
31 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
32 strcpy(filename, currdir);
33 strncat(filename, &settings->fileSeparator, 1);
34 strcat(filename, entry->d_name);
36 /* Check for subdirectory */
37 if ((subdir = opendir(filename)) != NULL) {
38 printf("%-60s\n", entryname);
39 if (settings->recursive) {
40 lineSum += scanDirectory(subdir, spaces+1, filename, settings);
41 }
42 closedir(subdir);
43 continue;
44 }
46 /* Count lines */
47 lines = 0;
48 bfile = false;
49 bfile_reset(settings->bfileHeuristics);
50 if (testSuffix(filename, settings)) {
51 FILE *file = fopen(filename, "r");
52 if (file == NULL) {
53 perror(" File acces failed");
54 continue;
55 }
57 do {
58 a = fgetc(file);
60 bfile = bfile_check(settings->bfileHeuristics, a);
62 if (a == 10) {
63 lines++;
64 }
65 } while (!bfile && a != EOF);
66 fclose(file);
68 /* Print and sum line count */
69 if (bfile) {
70 if (!settings->matchesOnly) {
71 printf("%-60s%19s\n", entryname, "binary");
72 }
73 } else {
74 lineSum += lines;
75 printf("%-60s%13d lines\n", entryname, lines);
76 }
77 } else {
78 if (!settings->matchesOnly) {
79 /* Print hint */
80 printf("%-60s%19s\n", entryname, "no match");
81 }
82 }
83 }
84 }
85 return lineSum;
86 }