diff -r 4508da679ffb -r 778388400f7b scanner.c --- a/scanner.c Thu Oct 20 17:29:23 2011 +0200 +++ b/scanner.c Fri Oct 21 15:09:26 2011 +0200 @@ -9,37 +9,50 @@ #include "scanner.h" #include "suffix_fnc.h" #include "bfile_heuristics.h" +#include -int scanDirectory(DIR *dir, const int spaces, - char* currdir, settings_t* settings) { - DIR *subdir; +int scanDirectory(scanner_t scanner, settings_t* settings) { + + DIR *dirf; struct dirent *entry; int lines, a; int lineSum = 0; bool bfile; + struct stat statbuf; - while ((entry = readdir(dir)) != NULL) { + 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) { - /* Print occurence */ - char entryname[strlen(entry->d_name)+spaces]; - for (int t = 0 ; t < spaces ; t++) { + /* 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[spaces] = 0; + entryname[scanner.spaces] = 0; strcat(entryname, entry->d_name); - char filename[(1+strlen(currdir)+strlen(entry->d_name))]; - strcpy(filename, currdir); + 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 ((subdir = opendir(filename)) != NULL) { - printf("%-60s\n", entryname); - if (settings->recursive) { - lineSum += scanDirectory(subdir, spaces+1, filename, settings); + 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; } - closedir(subdir); + } else { + perror(" Error in stat call"); continue; } @@ -50,6 +63,7 @@ if (testSuffix(filename, settings)) { FILE *file = fopen(filename, "r"); if (file == NULL) { + printf(entryname); perror(" File acces failed"); continue; } @@ -82,5 +96,8 @@ } } } + + closedir(dirf); + return lineSum; }