Thu, 01 Dec 2011 17:06:27 +0100
Merge with 3963e8800a1268be93d938c709e2d4be4d0b2d39
/* * scanner.c * * Created on: 23.05.2011 * Author: Mike */ #include "scanner.h" #include "suffix_fnc.h" #include "bfile_heuristics.h" #include <sys/stat.h> int scanDirectory(scanner_t scanner, settings_t* settings) { DIR *dirf; struct dirent *entry; int lines, a; int lineSum = 0; bool bfile; struct stat statbuf; if ((dirf = opendir(scanner.dir)) == NULL) { printf(scanner.dir); perror(" Directory access failed"); return 0; } while ((entry = readdir(dirf)) != NULL) { if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { /* Construct tree view and absolute pathname strings */ char entryname[strlen(entry->d_name)+scanner.spaces]; for (int t = 0 ; t < scanner.spaces ; t++) { entryname[t]=' '; } entryname[scanner.spaces] = 0; strcat(entryname, entry->d_name); char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; strcpy(filename, scanner.dir); strncat(filename, &settings->fileSeparator, 1); strcat(filename, entry->d_name); /* Check for subdirectory */ if (stat(filename, &statbuf) == 0) { if (!(statbuf.st_mode & S_IFREG)) { printf("%-60s\n", entryname); if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { lineSum += scanDirectory( (scanner_t) {filename, scanner.spaces+1}, settings); } continue; } } else { perror(" Error in stat call"); continue; } if (testSuffix(filename, settings)) { /* Count lines */ lines = 0; bfile = false; bfile_reset(settings->bfileHeuristics); char line_buffer[2048]; int line_buffer_offset = 0; FILE *file = fopen(filename, "r"); if (file == NULL) { printf(entryname); perror(" File acces failed"); continue; } do { a = fgetc(file); bfile = bfile_check(settings->bfileHeuristics, a); if (a == 10) { line_buffer[line_buffer_offset] = 0; /* TODO: do regex parsing */ line_buffer_offset = 0; lines++; } else { if (line_buffer_offset < 2048) { line_buffer[line_buffer_offset] = a; line_buffer_offset++; } else { line_buffer[line_buffer_offset-1] = 0; settings->confusing_lnlen = true; } } } while (!bfile && a != EOF); fclose(file); /* Print and sum line count */ if (bfile) { if (!settings->matchesOnly) { printf("%-60s%19s\n", entryname, "binary"); } } else { lineSum += lines; printf("%-60s%13d lines\n", entryname, lines); } } else { if (!settings->matchesOnly) { /* Print hint */ printf("%-60s%19s\n", entryname, "no match"); } } } } closedir(dirf); return lineSum; }