Fri, 21 Oct 2011 15:09:26 +0200
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
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@23 | 12 | #include <sys/stat.h> |
universe@3 | 13 | |
universe@23 | 14 | int scanDirectory(scanner_t scanner, settings_t* settings) { |
universe@23 | 15 | |
universe@23 | 16 | DIR *dirf; |
universe@3 | 17 | struct dirent *entry; |
universe@18 | 18 | int lines, a; |
universe@3 | 19 | int lineSum = 0; |
universe@21 | 20 | bool bfile; |
universe@23 | 21 | struct stat statbuf; |
universe@3 | 22 | |
universe@23 | 23 | if ((dirf = opendir(scanner.dir)) == NULL) { |
universe@23 | 24 | printf(scanner.dir); |
universe@23 | 25 | perror(" Directory access failed"); |
universe@23 | 26 | return 0; |
universe@23 | 27 | } |
universe@23 | 28 | |
universe@23 | 29 | while ((entry = readdir(dirf)) != NULL) { |
universe@3 | 30 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@23 | 31 | /* Construct tree view and absolute pathname strings */ |
universe@23 | 32 | char entryname[strlen(entry->d_name)+scanner.spaces]; |
universe@23 | 33 | for (int t = 0 ; t < scanner.spaces ; t++) { |
universe@3 | 34 | entryname[t]=' '; |
universe@3 | 35 | } |
universe@23 | 36 | entryname[scanner.spaces] = 0; |
universe@3 | 37 | strcat(entryname, entry->d_name); |
universe@3 | 38 | |
universe@23 | 39 | char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; |
universe@23 | 40 | strcpy(filename, scanner.dir); |
universe@14 | 41 | strncat(filename, &settings->fileSeparator, 1); |
universe@14 | 42 | strcat(filename, entry->d_name); |
universe@14 | 43 | |
universe@22 | 44 | /* Check for subdirectory */ |
universe@23 | 45 | if (stat(filename, &statbuf) == 0) { |
universe@23 | 46 | if (!(statbuf.st_mode & S_IFREG)) { |
universe@23 | 47 | printf("%-60s\n", entryname); |
universe@23 | 48 | if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { |
universe@23 | 49 | lineSum += scanDirectory( |
universe@23 | 50 | (scanner_t) {filename, scanner.spaces+1}, settings); |
universe@23 | 51 | } |
universe@23 | 52 | continue; |
universe@3 | 53 | } |
universe@23 | 54 | } else { |
universe@23 | 55 | perror(" Error in stat call"); |
universe@3 | 56 | continue; |
universe@3 | 57 | } |
universe@3 | 58 | |
universe@22 | 59 | /* Count lines */ |
universe@3 | 60 | lines = 0; |
universe@21 | 61 | bfile = false; |
universe@22 | 62 | bfile_reset(settings->bfileHeuristics); |
universe@3 | 63 | if (testSuffix(filename, settings)) { |
universe@3 | 64 | FILE *file = fopen(filename, "r"); |
universe@3 | 65 | if (file == NULL) { |
universe@23 | 66 | printf(entryname); |
universe@3 | 67 | perror(" File acces failed"); |
universe@3 | 68 | continue; |
universe@3 | 69 | } |
universe@3 | 70 | |
universe@3 | 71 | do { |
universe@3 | 72 | a = fgetc(file); |
universe@3 | 73 | |
universe@21 | 74 | bfile = bfile_check(settings->bfileHeuristics, a); |
universe@21 | 75 | |
universe@3 | 76 | if (a == 10) { |
universe@3 | 77 | lines++; |
universe@3 | 78 | } |
universe@21 | 79 | } while (!bfile && a != EOF); |
universe@3 | 80 | fclose(file); |
universe@3 | 81 | |
universe@22 | 82 | /* Print and sum line count */ |
universe@21 | 83 | if (bfile) { |
universe@22 | 84 | if (!settings->matchesOnly) { |
universe@22 | 85 | printf("%-60s%19s\n", entryname, "binary"); |
universe@22 | 86 | } |
universe@21 | 87 | } else { |
universe@22 | 88 | lineSum += lines; |
universe@3 | 89 | printf("%-60s%13d lines\n", entryname, lines); |
universe@21 | 90 | } |
universe@16 | 91 | } else { |
universe@3 | 92 | if (!settings->matchesOnly) { |
universe@22 | 93 | /* Print hint */ |
universe@21 | 94 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 95 | } |
universe@3 | 96 | } |
universe@3 | 97 | } |
universe@3 | 98 | } |
universe@23 | 99 | |
universe@23 | 100 | closedir(dirf); |
universe@23 | 101 | |
universe@3 | 102 | return lineSum; |
universe@3 | 103 | } |