Thu, 02 Feb 2012 16:55:51 +0100
fixed author note
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@27 | 12 | #include "regex_parser.h" |
universe@23 | 13 | #include <sys/stat.h> |
universe@3 | 14 | |
universe@23 | 15 | int scanDirectory(scanner_t scanner, settings_t* settings) { |
universe@23 | 16 | |
universe@23 | 17 | DIR *dirf; |
universe@3 | 18 | struct dirent *entry; |
universe@18 | 19 | int lines, a; |
universe@3 | 20 | int lineSum = 0; |
universe@21 | 21 | bool bfile; |
universe@23 | 22 | struct stat statbuf; |
universe@3 | 23 | |
universe@23 | 24 | if ((dirf = opendir(scanner.dir)) == NULL) { |
universe@23 | 25 | printf(scanner.dir); |
universe@23 | 26 | perror(" Directory access failed"); |
universe@23 | 27 | return 0; |
universe@23 | 28 | } |
universe@23 | 29 | |
universe@23 | 30 | while ((entry = readdir(dirf)) != NULL) { |
universe@3 | 31 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@23 | 32 | /* Construct tree view and absolute pathname strings */ |
universe@23 | 33 | char entryname[strlen(entry->d_name)+scanner.spaces]; |
universe@23 | 34 | for (int t = 0 ; t < scanner.spaces ; t++) { |
universe@3 | 35 | entryname[t]=' '; |
universe@3 | 36 | } |
universe@23 | 37 | entryname[scanner.spaces] = 0; |
universe@3 | 38 | strcat(entryname, entry->d_name); |
universe@3 | 39 | |
universe@23 | 40 | char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; |
universe@23 | 41 | strcpy(filename, scanner.dir); |
universe@14 | 42 | strncat(filename, &settings->fileSeparator, 1); |
universe@14 | 43 | strcat(filename, entry->d_name); |
universe@14 | 44 | |
universe@22 | 45 | /* Check for subdirectory */ |
universe@23 | 46 | if (stat(filename, &statbuf) == 0) { |
universe@23 | 47 | if (!(statbuf.st_mode & S_IFREG)) { |
universe@23 | 48 | printf("%-60s\n", entryname); |
universe@23 | 49 | if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { |
universe@23 | 50 | lineSum += scanDirectory( |
universe@23 | 51 | (scanner_t) {filename, scanner.spaces+1}, settings); |
universe@23 | 52 | } |
universe@23 | 53 | continue; |
universe@3 | 54 | } |
universe@23 | 55 | } else { |
universe@23 | 56 | perror(" Error in stat call"); |
universe@3 | 57 | continue; |
universe@3 | 58 | } |
universe@3 | 59 | |
universe@3 | 60 | if (testSuffix(filename, settings)) { |
universe@25 | 61 | /* Count lines */ |
universe@25 | 62 | lines = 0; |
universe@25 | 63 | bfile = false; |
universe@25 | 64 | bfile_reset(settings->bfileHeuristics); |
universe@27 | 65 | char line_buffer[REGEX_MAX_LINELENGTH]; |
universe@25 | 66 | int line_buffer_offset = 0; |
universe@25 | 67 | |
universe@3 | 68 | FILE *file = fopen(filename, "r"); |
universe@3 | 69 | if (file == NULL) { |
universe@23 | 70 | printf(entryname); |
universe@3 | 71 | perror(" File acces failed"); |
universe@3 | 72 | continue; |
universe@3 | 73 | } |
universe@3 | 74 | |
universe@3 | 75 | do { |
universe@3 | 76 | a = fgetc(file); |
universe@3 | 77 | |
universe@21 | 78 | bfile = bfile_check(settings->bfileHeuristics, a); |
universe@21 | 79 | |
universe@28 | 80 | if (a == 10 || a == EOF) { |
universe@25 | 81 | line_buffer[line_buffer_offset] = 0; |
universe@28 | 82 | if (regex_parser_do(settings->regex, line_buffer) == 0) { |
universe@28 | 83 | /* Only subtract lines when matching has finished */ |
universe@28 | 84 | if (!regex_parser_matching(settings->regex)) { |
universe@28 | 85 | lines -= settings->regex->matched_lines; |
universe@28 | 86 | } |
universe@28 | 87 | } |
universe@25 | 88 | |
universe@25 | 89 | line_buffer_offset = 0; |
universe@3 | 90 | lines++; |
universe@25 | 91 | } else { |
universe@27 | 92 | if (line_buffer_offset < REGEX_MAX_LINELENGTH) { |
universe@25 | 93 | line_buffer[line_buffer_offset] = a; |
universe@25 | 94 | line_buffer_offset++; |
universe@25 | 95 | } else { |
universe@25 | 96 | line_buffer[line_buffer_offset-1] = 0; |
universe@25 | 97 | settings->confusing_lnlen = true; |
universe@25 | 98 | } |
universe@3 | 99 | } |
universe@21 | 100 | } while (!bfile && a != EOF); |
universe@3 | 101 | fclose(file); |
universe@3 | 102 | |
universe@22 | 103 | /* Print and sum line count */ |
universe@21 | 104 | if (bfile) { |
universe@22 | 105 | if (!settings->matchesOnly) { |
universe@22 | 106 | printf("%-60s%19s\n", entryname, "binary"); |
universe@22 | 107 | } |
universe@21 | 108 | } else { |
universe@22 | 109 | lineSum += lines; |
universe@3 | 110 | printf("%-60s%13d lines\n", entryname, lines); |
universe@21 | 111 | } |
universe@16 | 112 | } else { |
universe@3 | 113 | if (!settings->matchesOnly) { |
universe@22 | 114 | /* Print hint */ |
universe@21 | 115 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 116 | } |
universe@3 | 117 | } |
universe@3 | 118 | } |
universe@3 | 119 | } |
universe@23 | 120 | |
universe@23 | 121 | closedir(dirf); |
universe@23 | 122 | |
universe@3 | 123 | return lineSum; |
universe@3 | 124 | } |