scanner.c

Fri, 21 Oct 2011 15:09:26 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Oct 2011 15:09:26 +0200
changeset 23
778388400f7b
parent 22
4508da679ffb
child 25
802c5382f499
permissions
-rw-r--r--

encapsulated scanner arguments + enabled optimizer + empty file is no bfile

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

mercurial