src/scanner.c

Tue, 21 May 2013 13:19:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 May 2013 13:19:37 +0200
changeset 41
c2e73e175341
parent 40
5938a9b74e8e
child 42
0402b9b41b0a
permissions
-rw-r--r--

replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)

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@41 39 typedef struct filelist filelist_t;
universe@23 40
universe@41 41 struct filelist {
universe@41 42 char *displayname;
universe@41 43 int displayname_len;
universe@41 44 char *filename;
universe@41 45 int st_mode;
universe@41 46 filelist_t *next;
universe@41 47 };
universe@41 48
universe@41 49 filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
universe@41 50 filelist_t* list) {
universe@41 51
universe@23 52 DIR *dirf;
universe@3 53 struct dirent *entry;
universe@23 54 struct stat statbuf;
universe@41 55 filelist_t *listentry = list;
universe@41 56
universe@23 57 if ((dirf = opendir(scanner.dir)) == NULL) {
universe@34 58 printf("%s", scanner.dir);
universe@23 59 perror(" Directory access failed");
universe@23 60 return 0;
universe@23 61 }
universe@23 62
universe@23 63 while ((entry = readdir(dirf)) != NULL) {
universe@3 64 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
universe@41 65
universe@41 66 /* Create new filelist entry */
universe@41 67 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
universe@41 68 // TODO: don't just append - create a sorted list!
universe@41 69 if (listentry) {
universe@41 70 listentry->next = newentry;
universe@41 71 }
universe@41 72 listentry = newentry;
universe@41 73 if (!list) {
universe@41 74 list = listentry;
universe@41 75 }
universe@41 76
universe@41 77 listentry->next = NULL;
universe@41 78
universe@41 79 listentry->displayname_len = strlen(entry->d_name);
universe@41 80 listentry->displayname = (char*) malloc(listentry->displayname_len+1);
universe@41 81 memcpy(listentry->displayname, entry->d_name, listentry->displayname_len);
universe@41 82 listentry->displayname[listentry->displayname_len] = 0;
universe@41 83
universe@41 84 listentry->st_mode = 0;
universe@41 85
universe@40 86 /* Construct absolute pathname string */
universe@41 87 size_t dirnamelen = strlen(scanner.dir);
universe@41 88 char *filename = (char*) malloc(2+dirnamelen+listentry->displayname_len);
universe@41 89 memcpy(filename, scanner.dir, dirnamelen);
universe@41 90 filename[dirnamelen] = settings->fileSeparator;
universe@41 91 memcpy(filename+dirnamelen+1, entry->d_name, listentry->displayname_len);
universe@41 92 filename[1+dirnamelen+listentry->displayname_len] = 0;
universe@41 93 listentry->filename = filename;
universe@14 94
universe@22 95 /* Check for subdirectory */
universe@23 96 if (stat(filename, &statbuf) == 0) {
universe@41 97 listentry->st_mode = statbuf.st_mode;
universe@23 98 } else {
universe@23 99 perror(" Error in stat call");
universe@3 100 continue;
universe@3 101 }
universe@41 102 }
universe@41 103 }
universe@41 104
universe@41 105 closedir(dirf);
universe@41 106
universe@41 107 return list;
universe@41 108 }
universe@3 109
universe@41 110 int scanDirectory(scanner_t scanner, settings_t* settings) {
universe@41 111
universe@41 112 int lines, a;
universe@41 113 int lineSum = 0;
universe@41 114 bool bfile;
universe@41 115
universe@41 116 filelist_t *filelist = buildFileList(scanner, settings, NULL);
universe@41 117
universe@41 118 while (filelist != NULL) {
universe@41 119
universe@41 120 /* Scan subdirectories */
universe@41 121 if (!(filelist->st_mode & S_IFREG)) {
universe@41 122 printf("%*s\n", filelist->displayname_len+scanner.spaces,
universe@41 123 filelist->displayname);
universe@41 124 if (settings->recursive && (filelist->st_mode & S_IFDIR)) {
universe@41 125 scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1},
universe@41 126 settings);
universe@41 127 }
universe@41 128 } else {
universe@30 129 if ((settings->includeSuffixes->count == 0
universe@41 130 || testSuffix(filelist->displayname, settings->includeSuffixes))
universe@41 131 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
universe@41 132
universe@25 133 /* Count lines */
universe@25 134 lines = 0;
universe@25 135 bfile = false;
universe@25 136 bfile_reset(settings->bfileHeuristics);
universe@27 137 char line_buffer[REGEX_MAX_LINELENGTH];
universe@25 138 int line_buffer_offset = 0;
universe@25 139
universe@41 140 FILE *file = fopen(filelist->filename, "r");
universe@3 141 if (file == NULL) {
universe@41 142 printf("%*s", filelist->displayname_len+scanner.spaces,
universe@41 143 filelist->displayname);
universe@3 144 perror(" File acces failed");
universe@41 145 } else {
universe@41 146 do {
universe@41 147 a = fgetc(file);
universe@3 148
universe@41 149 bfile = bfile_check(settings->bfileHeuristics, a);
universe@3 150
universe@41 151 if (a == 10 || a == EOF) {
universe@41 152 line_buffer[line_buffer_offset] = 0;
universe@41 153 if (regex_parser_do(settings->regex, line_buffer) == 0) {
universe@41 154 /* Only subtract lines when matching has finished */
universe@41 155 if (!regex_parser_matching(settings->regex)) {
universe@41 156 lines -= settings->regex->matched_lines;
universe@41 157 }
universe@41 158 }
universe@21 159
universe@41 160 line_buffer_offset = 0;
universe@41 161 lines++;
universe@41 162 } else {
universe@41 163 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
universe@41 164 line_buffer[line_buffer_offset] = a;
universe@41 165 line_buffer_offset++;
universe@41 166 } else {
universe@41 167 line_buffer[line_buffer_offset-1] = 0;
universe@41 168 settings->confusing_lnlen = true;
universe@28 169 }
universe@28 170 }
universe@41 171 } while (!bfile && a != EOF);
universe@41 172 fclose(file);
universe@25 173
universe@41 174 /* Print and sum line count */
universe@41 175 if (bfile) {
universe@41 176 if (!settings->matchesOnly) {
universe@41 177 printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
universe@41 178 filelist->displayname,
universe@41 179 60-filelist->displayname_len-scanner.spaces, "", "binary");
universe@41 180 }
universe@25 181 } else {
universe@41 182 lineSum += lines;
universe@41 183 printf("%*s%*s%13d lines\n",
universe@41 184 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 185 60-filelist->displayname_len-scanner.spaces, "", lines);
universe@3 186 }
universe@21 187 }
universe@16 188 } else {
universe@3 189 if (!settings->matchesOnly) {
universe@22 190 /* Print hint */
universe@40 191 printf("%*s%*s%19s\n",
universe@41 192 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 193 60-filelist->displayname_len-scanner.spaces, "", "no match");
universe@3 194 }
universe@3 195 }
universe@3 196 }
universe@41 197
universe@41 198 free(filelist->filename);
universe@41 199 free(filelist->displayname);
universe@41 200 filelist_t *freethis = filelist;
universe@41 201 filelist = filelist->next;
universe@41 202 free(freethis);
universe@3 203 }
universe@23 204
universe@3 205 return lineSum;
universe@3 206 }

mercurial