1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/scanner.c Thu Sep 15 13:29:06 2011 +0200 1.3 @@ -0,0 +1,88 @@ 1.4 +/* 1.5 + * functions.c 1.6 + * 1.7 + * Created on: 23.05.2011 1.8 + * Author: beckermi 1.9 + */ 1.10 + 1.11 + 1.12 +#include "scanner.h" 1.13 +#include "suffix_fnc.h" 1.14 + 1.15 +int scanDirectory(DIR *dir, const int spaces, 1.16 + char* currdir, settings_t* settings) { 1.17 + DIR *subdir; 1.18 + char* subdirname; 1.19 + struct dirent *entry; 1.20 + int lines, digits, a; 1.21 + int lineSum = 0; 1.22 + 1.23 + while ((entry = readdir(dir)) != NULL) { 1.24 + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 1.25 + // Print occurence 1.26 + char entryname[strlen(entry->d_name)+spaces]; 1.27 + for (int t = 0 ; t < spaces ; t++) { 1.28 + entryname[t]=' '; 1.29 + } 1.30 + entryname[spaces] = 0; 1.31 + strcat(entryname, entry->d_name); 1.32 + 1.33 + // Check for subdirectory 1.34 + char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; 1.35 + strcpy(subdirname, currdir); 1.36 + strncat(subdirname, &settings->fileSeparator, 1); 1.37 + strcat(subdirname, entry->d_name); 1.38 + if ((subdir = opendir(subdirname)) != NULL) { 1.39 + printf("%-60s\n", entryname); 1.40 + if (settings->recursive) { 1.41 + lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); 1.42 + } 1.43 + closedir(subdir); 1.44 + continue; 1.45 + } 1.46 + 1.47 + // Count lines 1.48 + lines = 0; 1.49 + char filename[(1+strlen(currdir)+strlen(entry->d_name))]; 1.50 + strcpy(filename, currdir); 1.51 + strncat(filename, &settings->fileSeparator, 1); 1.52 + strcat(filename, entry->d_name); 1.53 + if (testSuffix(filename, settings)) { 1.54 + FILE *file = fopen(filename, "r"); 1.55 + if (file == NULL) { 1.56 + perror(" File acces failed"); 1.57 + continue; 1.58 + } 1.59 + 1.60 + do { 1.61 + a = fgetc(file); 1.62 + 1.63 + if (a == 10) { 1.64 + lines++; 1.65 + } 1.66 + } while (a != EOF); 1.67 + fclose(file); 1.68 + 1.69 + // Print line count 1.70 + #ifdef _WIN32 1.71 + printf("%-60s%13d lines\n", entryname, lines); 1.72 + #else 1.73 + printf("%-60s%14d lines\n", entryname, lines); 1.74 + #endif /* _WIN32 */ 1.75 + 1.76 + lineSum += lines; 1.77 + } 1.78 + else { 1.79 + if (!settings->matchesOnly) { 1.80 + // Print hint 1.81 + #ifdef _WIN32 1.82 + printf("%-60s%19s\n", entryname, "no match"); 1.83 + #else 1.84 + printf("%-60s%20s\n", entryname, "no match"); 1.85 + #endif /* _WIN32 */ 1.86 + } 1.87 + } 1.88 + } 1.89 + } 1.90 + return lineSum; 1.91 +}