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