scanner.c

changeset 23
778388400f7b
parent 22
4508da679ffb
child 25
802c5382f499
equal deleted inserted replaced
22:4508da679ffb 23:778388400f7b
7 7
8 8
9 #include "scanner.h" 9 #include "scanner.h"
10 #include "suffix_fnc.h" 10 #include "suffix_fnc.h"
11 #include "bfile_heuristics.h" 11 #include "bfile_heuristics.h"
12 #include <sys/stat.h>
12 13
13 int scanDirectory(DIR *dir, const int spaces, 14 int scanDirectory(scanner_t scanner, settings_t* settings) {
14 char* currdir, settings_t* settings) { 15
15 DIR *subdir; 16 DIR *dirf;
16 struct dirent *entry; 17 struct dirent *entry;
17 int lines, a; 18 int lines, a;
18 int lineSum = 0; 19 int lineSum = 0;
19 bool bfile; 20 bool bfile;
21 struct stat statbuf;
20 22
21 while ((entry = readdir(dir)) != NULL) { 23 if ((dirf = opendir(scanner.dir)) == NULL) {
24 printf(scanner.dir);
25 perror(" Directory access failed");
26 return 0;
27 }
28
29 while ((entry = readdir(dirf)) != NULL) {
22 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 30 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
23 /* Print occurence */ 31 /* Construct tree view and absolute pathname strings */
24 char entryname[strlen(entry->d_name)+spaces]; 32 char entryname[strlen(entry->d_name)+scanner.spaces];
25 for (int t = 0 ; t < spaces ; t++) { 33 for (int t = 0 ; t < scanner.spaces ; t++) {
26 entryname[t]=' '; 34 entryname[t]=' ';
27 } 35 }
28 entryname[spaces] = 0; 36 entryname[scanner.spaces] = 0;
29 strcat(entryname, entry->d_name); 37 strcat(entryname, entry->d_name);
30 38
31 char filename[(1+strlen(currdir)+strlen(entry->d_name))]; 39 char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
32 strcpy(filename, currdir); 40 strcpy(filename, scanner.dir);
33 strncat(filename, &settings->fileSeparator, 1); 41 strncat(filename, &settings->fileSeparator, 1);
34 strcat(filename, entry->d_name); 42 strcat(filename, entry->d_name);
35 43
36 /* Check for subdirectory */ 44 /* Check for subdirectory */
37 if ((subdir = opendir(filename)) != NULL) { 45 if (stat(filename, &statbuf) == 0) {
38 printf("%-60s\n", entryname); 46 if (!(statbuf.st_mode & S_IFREG)) {
39 if (settings->recursive) { 47 printf("%-60s\n", entryname);
40 lineSum += scanDirectory(subdir, spaces+1, filename, settings); 48 if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
49 lineSum += scanDirectory(
50 (scanner_t) {filename, scanner.spaces+1}, settings);
51 }
52 continue;
41 } 53 }
42 closedir(subdir); 54 } else {
55 perror(" Error in stat call");
43 continue; 56 continue;
44 } 57 }
45 58
46 /* Count lines */ 59 /* Count lines */
47 lines = 0; 60 lines = 0;
48 bfile = false; 61 bfile = false;
49 bfile_reset(settings->bfileHeuristics); 62 bfile_reset(settings->bfileHeuristics);
50 if (testSuffix(filename, settings)) { 63 if (testSuffix(filename, settings)) {
51 FILE *file = fopen(filename, "r"); 64 FILE *file = fopen(filename, "r");
52 if (file == NULL) { 65 if (file == NULL) {
66 printf(entryname);
53 perror(" File acces failed"); 67 perror(" File acces failed");
54 continue; 68 continue;
55 } 69 }
56 70
57 do { 71 do {
80 printf("%-60s%19s\n", entryname, "no match"); 94 printf("%-60s%19s\n", entryname, "no match");
81 } 95 }
82 } 96 }
83 } 97 }
84 } 98 }
99
100 closedir(dirf);
101
85 return lineSum; 102 return lineSum;
86 } 103 }

mercurial