Mon, 19 Sep 2011 08:11:08 +0200
added license
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 /* char filename[(1+strlen(currdir)+strlen(entry->d_name))];
48 strcpy(filename, currdir);
49 strncat(filename, &settings->fileSeparator, 1);
50 strcat(filename, entry->d_name); */
51 if (testSuffix(filename, settings)) {
52 FILE *file = fopen(filename, "r");
53 if (file == NULL) {
54 perror(" File acces failed");
55 continue;
56 }
58 do {
59 a = fgetc(file);
61 if (a == 10) {
62 lines++;
63 }
64 } while (a != EOF);
65 fclose(file);
67 // Print line count
68 #ifdef _WIN32
69 printf("%-60s%13d lines\n", entryname, lines);
70 #else
71 printf("%-60s%14d lines\n", entryname, lines);
72 #endif /* _WIN32 */
74 lineSum += lines;
75 }
76 else {
77 if (!settings->matchesOnly) {
78 // Print hint
79 #ifdef _WIN32
80 printf("%-60s%19s\n", entryname, "no match");
81 #else
82 printf("%-60s%20s\n", entryname, "no match");
83 #endif /* _WIN32 */
84 }
85 }
86 }
87 }
88 return lineSum;
89 }