scanner.c

changeset 10
ecf787666f44
parent 8
28319b20968c
child 14
ee9333c91dda
equal deleted inserted replaced
8:28319b20968c 10:ecf787666f44
1 /*
2 * functions.c
3 *
4 * Created on: 23.05.2011
5 * Author: beckermi
6 */
7
8
9 #include "scanner.h"
10 #include "suffix_fnc.h"
11
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;
19
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);
29
30 // Check for subdirectory
31 char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
32 strcpy(subdirname, currdir);
33 strncat(subdirname, &settings->fileSeparator, 1);
34 strcat(subdirname, entry->d_name);
35 if ((subdir = opendir(subdirname)) != NULL) {
36 printf("%-60s\n", entryname);
37 if (settings->recursive) {
38 lineSum += scanDirectory(subdir, spaces+1, subdirname, settings);
39 }
40 closedir(subdir);
41 continue;
42 }
43
44 // Count lines
45 lines = 0;
46 char filename[(1+strlen(currdir)+strlen(entry->d_name))];
47 strcpy(filename, currdir);
48 strncat(filename, &settings->fileSeparator, 1);
49 strcat(filename, entry->d_name);
50 if (testSuffix(filename, settings)) {
51 FILE *file = fopen(filename, "r");
52 if (file == NULL) {
53 perror(" File acces failed");
54 continue;
55 }
56
57 do {
58 a = fgetc(file);
59
60 if (a == 10) {
61 lines++;
62 }
63 } while (a != EOF);
64 fclose(file);
65
66 // Print line count
67 #ifdef _WIN32
68 printf("%-60s%13d lines\n", entryname, lines);
69 #else
70 printf("%-60s%14d lines\n", entryname, lines);
71 #endif /* _WIN32 */
72
73 lineSum += lines;
74 }
75 else {
76 if (!settings->matchesOnly) {
77 // Print hint
78 #ifdef _WIN32
79 printf("%-60s%19s\n", entryname, "no match");
80 #else
81 printf("%-60s%20s\n", entryname, "no match");
82 #endif /* _WIN32 */
83 }
84 }
85 }
86 }
87 return lineSum;
88 }

mercurial