scanner.c

changeset 23
778388400f7b
parent 22
4508da679ffb
child 25
802c5382f499
     1.1 --- a/scanner.c	Thu Oct 20 17:29:23 2011 +0200
     1.2 +++ b/scanner.c	Fri Oct 21 15:09:26 2011 +0200
     1.3 @@ -9,37 +9,50 @@
     1.4  #include "scanner.h"
     1.5  #include "suffix_fnc.h"
     1.6  #include "bfile_heuristics.h"
     1.7 +#include <sys/stat.h>
     1.8  
     1.9 -int scanDirectory(DIR *dir, const int spaces,
    1.10 -                  char* currdir, settings_t* settings) {
    1.11 -  DIR *subdir;
    1.12 +int scanDirectory(scanner_t scanner, settings_t* settings) {
    1.13 +
    1.14 +  DIR *dirf;
    1.15    struct dirent *entry;
    1.16    int lines, a;
    1.17    int lineSum = 0;
    1.18    bool bfile;
    1.19 +  struct stat statbuf;
    1.20  
    1.21 -  while ((entry = readdir(dir)) != NULL) {
    1.22 +  if ((dirf = opendir(scanner.dir)) == NULL) {
    1.23 +    printf(scanner.dir);
    1.24 +    perror("  Directory access failed");
    1.25 +    return 0;
    1.26 +  }
    1.27 +
    1.28 +  while ((entry = readdir(dirf)) != NULL) {
    1.29      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    1.30 -      /* Print occurence */
    1.31 -      char entryname[strlen(entry->d_name)+spaces];
    1.32 -      for (int t = 0 ; t < spaces ; t++) {
    1.33 +      /* Construct tree view and absolute pathname strings */
    1.34 +      char entryname[strlen(entry->d_name)+scanner.spaces];
    1.35 +      for (int t = 0 ; t < scanner.spaces ; t++) {
    1.36          entryname[t]=' ';
    1.37        }
    1.38 -      entryname[spaces] = 0;
    1.39 +      entryname[scanner.spaces] = 0;
    1.40        strcat(entryname, entry->d_name);
    1.41    
    1.42 -      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    1.43 -      strcpy(filename, currdir);
    1.44 +      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
    1.45 +      strcpy(filename, scanner.dir);
    1.46        strncat(filename, &settings->fileSeparator, 1);
    1.47        strcat(filename, entry->d_name);
    1.48  
    1.49        /* Check for subdirectory */
    1.50 -      if ((subdir = opendir(filename)) != NULL) {
    1.51 -        printf("%-60s\n", entryname);
    1.52 -        if (settings->recursive) {
    1.53 -          lineSum += scanDirectory(subdir, spaces+1, filename, settings);
    1.54 +      if (stat(filename, &statbuf) == 0) {
    1.55 +        if (!(statbuf.st_mode & S_IFREG)) {
    1.56 +          printf("%-60s\n", entryname);
    1.57 +          if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    1.58 +            lineSum += scanDirectory(
    1.59 +                (scanner_t) {filename, scanner.spaces+1}, settings);
    1.60 +          }
    1.61 +          continue;
    1.62          }
    1.63 -        closedir(subdir);
    1.64 +      } else {
    1.65 +        perror("  Error in stat call");
    1.66          continue;
    1.67        }
    1.68  
    1.69 @@ -50,6 +63,7 @@
    1.70        if (testSuffix(filename, settings)) {
    1.71          FILE *file = fopen(filename, "r");
    1.72          if (file == NULL) {
    1.73 +          printf(entryname);
    1.74            perror("  File acces failed");
    1.75            continue;
    1.76          }
    1.77 @@ -82,5 +96,8 @@
    1.78        }
    1.79      }
    1.80    }
    1.81 +
    1.82 +  closedir(dirf);
    1.83 +
    1.84    return lineSum;
    1.85  }

mercurial