Thu, 06 Oct 2011 00:06:30 +0200
fixed suffixList realloc bug + added destroy_suffix_list_t
1 /*
2 * functions.c
3 *
4 * Created on: 23.05.2011
5 * Author: beckermi
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 char* subdirname;
16 struct dirent *entry;
17 int lines, digits, a;
18 int lineSum = 0;
20 while ((entry = readdir(dir)) != NULL) {
21 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
22 // Print occurence
23 char entryname[strlen(entry->d_name)+spaces];
24 for (int t = 0 ; t < spaces ; t++) {
25 entryname[t]=' ';
26 }
27 entryname[spaces] = 0;
28 strcat(entryname, entry->d_name);
30 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
31 strcpy(filename, currdir);
32 strncat(filename, &settings->fileSeparator, 1);
33 strcat(filename, entry->d_name);
35 // Check for subdirectory
36 if ((subdir = opendir(filename)) != NULL) {
37 printf("%-60s\n", entryname);
38 if (settings->recursive) {
39 lineSum += scanDirectory(subdir, spaces+1, filename, settings);
40 }
41 closedir(subdir);
42 continue;
43 }
45 // Count lines
46 lines = 0;
47 if (testSuffix(filename, settings)) {
48 FILE *file = fopen(filename, "r");
49 if (file == NULL) {
50 perror(" File acces failed");
51 continue;
52 }
54 do {
55 a = fgetc(file);
57 if (a == 10) {
58 lines++;
59 }
60 } while (a != EOF);
61 fclose(file);
63 // Print line count
64 #ifdef _WIN32
65 printf("%-60s%13d lines\n", entryname, lines);
66 #else
67 printf("%-60s%14d lines\n", entryname, lines);
68 #endif /* _WIN32 */
70 lineSum += lines;
71 } else {
72 if (!settings->matchesOnly) {
73 // Print hint
74 #ifdef _WIN32
75 printf("%-60s%19s\n", entryname, "no match");
76 #else
77 printf("%-60s%20s\n", entryname, "no match");
78 #endif /* _WIN32 */
79 }
80 }
81 }
82 }
83 return lineSum;
84 }