Thu, 20 Oct 2011 14:13:56 +0200
Changed author comments + added signatures for upcomming bfile heuristics
universe@10 | 1 | /* |
universe@20 | 2 | * scanner.c |
universe@10 | 3 | * |
universe@10 | 4 | * Created on: 23.05.2011 |
universe@20 | 5 | * Author: Mike |
universe@10 | 6 | */ |
universe@1 | 7 | |
universe@1 | 8 | |
universe@10 | 9 | #include "scanner.h" |
universe@10 | 10 | #include "suffix_fnc.h" |
universe@3 | 11 | |
universe@3 | 12 | int scanDirectory(DIR *dir, const int spaces, |
universe@3 | 13 | char* currdir, settings_t* settings) { |
universe@3 | 14 | DIR *subdir; |
universe@3 | 15 | struct dirent *entry; |
universe@18 | 16 | int lines, a; |
universe@3 | 17 | int lineSum = 0; |
universe@3 | 18 | |
universe@3 | 19 | while ((entry = readdir(dir)) != NULL) { |
universe@3 | 20 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@3 | 21 | // Print occurence |
universe@3 | 22 | char entryname[strlen(entry->d_name)+spaces]; |
universe@3 | 23 | for (int t = 0 ; t < spaces ; t++) { |
universe@3 | 24 | entryname[t]=' '; |
universe@3 | 25 | } |
universe@3 | 26 | entryname[spaces] = 0; |
universe@3 | 27 | strcat(entryname, entry->d_name); |
universe@3 | 28 | |
universe@14 | 29 | char filename[(1+strlen(currdir)+strlen(entry->d_name))]; |
universe@14 | 30 | strcpy(filename, currdir); |
universe@14 | 31 | strncat(filename, &settings->fileSeparator, 1); |
universe@14 | 32 | strcat(filename, entry->d_name); |
universe@14 | 33 | |
universe@3 | 34 | // Check for subdirectory |
universe@14 | 35 | if ((subdir = opendir(filename)) != NULL) { |
universe@3 | 36 | printf("%-60s\n", entryname); |
universe@3 | 37 | if (settings->recursive) { |
universe@14 | 38 | lineSum += scanDirectory(subdir, spaces+1, filename, settings); |
universe@3 | 39 | } |
universe@3 | 40 | closedir(subdir); |
universe@3 | 41 | continue; |
universe@3 | 42 | } |
universe@3 | 43 | |
universe@3 | 44 | // Count lines |
universe@3 | 45 | lines = 0; |
universe@3 | 46 | if (testSuffix(filename, settings)) { |
universe@3 | 47 | FILE *file = fopen(filename, "r"); |
universe@3 | 48 | if (file == NULL) { |
universe@3 | 49 | perror(" File acces failed"); |
universe@3 | 50 | continue; |
universe@3 | 51 | } |
universe@3 | 52 | |
universe@3 | 53 | do { |
universe@3 | 54 | a = fgetc(file); |
universe@3 | 55 | |
universe@3 | 56 | if (a == 10) { |
universe@3 | 57 | lines++; |
universe@3 | 58 | } |
universe@3 | 59 | } while (a != EOF); |
universe@3 | 60 | fclose(file); |
universe@3 | 61 | |
universe@3 | 62 | // Print line count |
universe@3 | 63 | #ifdef _WIN32 |
universe@3 | 64 | printf("%-60s%13d lines\n", entryname, lines); |
universe@3 | 65 | #else |
universe@3 | 66 | printf("%-60s%14d lines\n", entryname, lines); |
universe@3 | 67 | #endif /* _WIN32 */ |
universe@3 | 68 | |
universe@3 | 69 | lineSum += lines; |
universe@16 | 70 | } else { |
universe@3 | 71 | if (!settings->matchesOnly) { |
universe@3 | 72 | // Print hint |
universe@3 | 73 | #ifdef _WIN32 |
universe@3 | 74 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 75 | #else |
universe@3 | 76 | printf("%-60s%20s\n", entryname, "no match"); |
universe@3 | 77 | #endif /* _WIN32 */ |
universe@3 | 78 | } |
universe@3 | 79 | } |
universe@3 | 80 | } |
universe@3 | 81 | } |
universe@3 | 82 | return lineSum; |
universe@3 | 83 | } |