src/scanner.c

Fri, 17 May 2013 14:41:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 17 May 2013 14:41:44 +0200
changeset 40
5938a9b74e8e
parent 36
a7ff583e153f
child 41
c2e73e175341
permissions
-rw-r--r--

improved printing the results and fixed bug where the filename was displayed as an empty string

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     3  * Copyright 2013 Mike Becker. All rights reserved.
     4  * 
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  * 
     8  * 1. Redistributions of source code must retain the above copyright
     9  * notice, this list of conditions and the following disclaimer.
    10  * 
    11  * 2. Redistributions in binary form must reproduce the above copyright
    12  * notice, this list of conditions and the following disclaimer in the
    13  * documentation and/or other materials provided with the distribution.
    14  * 
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    25  *
    26  * scanner.c
    27  *
    28  *  Created on: 23.05.2011
    29  *      Author: Mike
    30  */
    33 #include "scanner.h"
    34 #include "suffix_fnc.h"
    35 #include "bfile_heuristics.h"
    36 #include "regex_parser.h"
    37 #include <sys/stat.h>
    39 int scanDirectory(scanner_t scanner, settings_t* settings) {
    41   DIR *dirf;
    42   struct dirent *entry;
    43   int entrynamelen;
    44   int lines, a;
    45   int lineSum = 0;
    46   bool bfile;
    47   struct stat statbuf;
    49   if ((dirf = opendir(scanner.dir)) == NULL) {
    50     printf("%s", scanner.dir);
    51     perror("  Directory access failed");
    52     return 0;
    53   }
    55   while ((entry = readdir(dirf)) != NULL) {
    56     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    57       /* Construct absolute pathname string */
    58       entrynamelen = strlen(entry->d_name);
    59       char filename[(1+strlen(scanner.dir)+entrynamelen)];
    60       strcpy(filename, scanner.dir);
    61       strncat(filename, &settings->fileSeparator, 1);
    62       strcat(filename, entry->d_name);
    64       /* Check for subdirectory */
    65       if (stat(filename, &statbuf) == 0) {
    66         if (!(statbuf.st_mode & S_IFREG)) {
    67           printf("%*s\n", entrynamelen+scanner.spaces, entry->d_name);
    68           if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    69             lineSum += scanDirectory(
    70                 (scanner_t) {filename, scanner.spaces+1}, settings);
    71           }
    72           continue;
    73         }
    74       } else {
    75         perror("  Error in stat call");
    76         continue;
    77       }
    79       if ((settings->includeSuffixes->count == 0
    80           || testSuffix(filename, settings->includeSuffixes))
    81           && !testSuffix(filename, settings->excludeSuffixes)) {
    82         /* Count lines */
    83         lines = 0;
    84         bfile = false;
    85         bfile_reset(settings->bfileHeuristics);
    86         char line_buffer[REGEX_MAX_LINELENGTH];
    87         int line_buffer_offset = 0;
    89         FILE *file = fopen(filename, "r");
    90         if (file == NULL) {
    91           printf("%*s", entrynamelen+scanner.spaces, entry->d_name);
    92           perror("  File acces failed");
    93           continue;
    94         }
    96         do {
    97           a = fgetc(file);
    99           bfile = bfile_check(settings->bfileHeuristics, a);
   101           if (a == 10 || a == EOF) {
   102             line_buffer[line_buffer_offset] = 0;
   103             if (regex_parser_do(settings->regex, line_buffer) == 0) {
   104               /* Only subtract lines when matching has finished */
   105               if (!regex_parser_matching(settings->regex)) {
   106                 lines -= settings->regex->matched_lines;
   107               }
   108             }
   110             line_buffer_offset = 0;
   111             lines++;
   112           } else {
   113             if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
   114               line_buffer[line_buffer_offset] = a;
   115               line_buffer_offset++;
   116             } else {
   117               line_buffer[line_buffer_offset-1] = 0;
   118               settings->confusing_lnlen = true;
   119             }
   120           }
   121         } while (!bfile && a != EOF);
   122         fclose(file);
   124         /* Print and sum line count */
   125         if (bfile) {
   126           if (!settings->matchesOnly) {
   127             printf("%*s%*s%19s\n",
   128                 entrynamelen+scanner.spaces, entry->d_name,
   129                 60-entrynamelen-scanner.spaces, "", "binary");
   130           }
   131         } else {
   132           lineSum += lines;
   133           printf("%*s%*s%13d lines\n",
   134               entrynamelen+scanner.spaces, entry->d_name,
   135               60-entrynamelen-scanner.spaces, "", lines);
   136         }
   137       } else {
   138         if (!settings->matchesOnly) {
   139           /* Print hint */
   140           printf("%*s%*s%19s\n",
   141               entrynamelen+scanner.spaces, entry->d_name,
   142               60-entrynamelen-scanner.spaces, "", "no match");
   143         }
   144       }
   145     }
   146   }
   148   closedir(dirf);
   150   return lineSum;
   151 }

mercurial