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

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

mercurial