# HG changeset patch # User Mike Becker # Date 1319202566 -7200 # Node ID 778388400f7b0e87b9465261b15bd8305a965ede # Parent 4508da679ffbe900f140c0f77f520511b6def0a6 encapsulated scanner arguments + enabled optimizer + empty file is no bfile diff -r 4508da679ffb -r 778388400f7b Makefile --- a/Makefile Thu Oct 20 17:29:23 2011 +0200 +++ b/Makefile Fri Oct 21 15:09:26 2011 +0200 @@ -1,5 +1,5 @@ CC = gcc -CARG = -Wall -std=gnu99 +CARG = -Wall -std=gnu99 -O BUILDDIR = build/ OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') BIN = ${BUILDDIR}cline diff -r 4508da679ffb -r 778388400f7b bfile_heuristics.c --- a/bfile_heuristics.c Thu Oct 20 17:29:23 2011 +0200 +++ b/bfile_heuristics.c Fri Oct 21 15:09:26 2011 +0200 @@ -32,20 +32,23 @@ def->bcount++; } - switch (def->level) { - case BFILE_LOW_ACCURACY: - if (def->tcount > 15 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.32; - } - break; - case BFILE_HIGH_ACCURACY: - if (def->tcount > 500 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.1; - } - break; - default: /* BFILE_MEDIUM_ACCURACY */ - if (def->tcount > 100 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.1; + if (def->tcount > 1) { /* empty files are text files */ + switch (def->level) { + case BFILE_LOW_ACCURACY: + if (def->tcount > 15 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.32; + } + break; + case BFILE_HIGH_ACCURACY: + if (def->tcount > 500 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.1; + } + break; + default: /* BFILE_MEDIUM_ACCURACY */ + if (def->tcount > 100 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.1; + } + break; } } } diff -r 4508da679ffb -r 778388400f7b cline.c --- a/cline.c Thu Oct 20 17:29:23 2011 +0200 +++ b/cline.c Fri Oct 21 15:09:26 2011 +0200 @@ -153,17 +153,8 @@ finder = strtok(NULL, ","); } - /* Open directory */ - DIR *dir = opendir(directory); - if (dir == NULL) { - perror("Operation failed"); - destroy_settings_t(settings); - return 1; - } - /* Scan directory */ - int lines = scanDirectory(dir, 0, directory, settings); - closedir(dir); + int lines = scanDirectory((scanner_t){directory, 0}, settings); destroy_settings_t(settings); /* Print double line and line count */ 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; } diff -r 4508da679ffb -r 778388400f7b scanner.h --- a/scanner.h Thu Oct 20 17:29:23 2011 +0200 +++ b/scanner.h Fri Oct 21 15:09:26 2011 +0200 @@ -11,12 +11,16 @@ #include "stdinc.h" #include "settings.h" +typedef struct { + char *dir; + int spaces; +} scanner_t; + #ifdef _cplusplus extern "C" { #endif -int scanDirectory(DIR *dir, const int spaces, - char* currdir, settings_t* settings); +int scanDirectory(scanner_t scanner, settings_t* settings); #ifdef _cplusplus }