src/scanner.c

Wed, 22 May 2013 10:57:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 22 May 2013 10:57:17 +0200
changeset 43
104e75d18ede
parent 42
0402b9b41b0a
child 44
9574a181ec26
permissions
-rw-r--r--

fixed bug: line sum of subdirectories were not added to total sum

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

mercurial