src/scanner.c

Sat, 25 Jul 2020 18:28:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 18:28:01 +0200
changeset 60
69be673a4fd0
parent 58
bb512754aaf3
child 61
9c8d768f0244
permissions
-rw-r--r--

preparing changes for individual sum feature

universe@10 1 /*
universe@34 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@57 3 * Copyright 2018 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@57 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@10 25 */
universe@1 26
universe@1 27
universe@10 28 #include "scanner.h"
universe@10 29 #include "suffix_fnc.h"
universe@21 30 #include "bfile_heuristics.h"
universe@27 31 #include "regex_parser.h"
universe@23 32 #include <sys/stat.h>
universe@3 33
universe@41 34 typedef struct filelist filelist_t;
universe@23 35
universe@41 36 struct filelist {
universe@41 37 char *displayname;
universe@41 38 int displayname_len;
universe@41 39 char *filename;
universe@41 40 int st_mode;
universe@41 41 filelist_t *next;
universe@41 42 };
universe@41 43
universe@41 44 filelist_t *buildFileList(scanner_t scanner, settings_t* settings,
universe@41 45 filelist_t* list) {
universe@41 46
universe@23 47 DIR *dirf;
universe@3 48 struct dirent *entry;
universe@23 49 struct stat statbuf;
universe@41 50
universe@23 51 if ((dirf = opendir(scanner.dir)) == NULL) {
universe@58 52 fprintf(stderr, "%s - ", scanner.dir);
universe@58 53 perror("Directory access failed");
universe@23 54 return 0;
universe@23 55 }
universe@23 56
universe@23 57 while ((entry = readdir(dirf)) != NULL) {
universe@3 58 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
universe@41 59
universe@41 60 /* Create new filelist entry */
universe@41 61 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t));
universe@42 62 newentry->next = NULL;
universe@41 63
universe@42 64 newentry->displayname_len = strlen(entry->d_name);
universe@42 65 newentry->displayname = (char*) malloc(newentry->displayname_len+1);
universe@42 66 memcpy(newentry->displayname, entry->d_name, newentry->displayname_len);
universe@42 67 newentry->displayname[newentry->displayname_len] = 0;
universe@41 68
universe@42 69 newentry->st_mode = 0;
universe@41 70
universe@40 71 /* Construct absolute pathname string */
universe@41 72 size_t dirnamelen = strlen(scanner.dir);
universe@42 73 char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len);
universe@41 74 memcpy(filename, scanner.dir, dirnamelen);
universe@41 75 filename[dirnamelen] = settings->fileSeparator;
universe@42 76 memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len);
universe@42 77 filename[1+dirnamelen+newentry->displayname_len] = 0;
universe@42 78 newentry->filename = filename;
universe@14 79
universe@22 80 /* Check for subdirectory */
universe@23 81 if (stat(filename, &statbuf) == 0) {
universe@42 82 newentry->st_mode = statbuf.st_mode;
universe@23 83 } else {
universe@23 84 perror(" Error in stat call");
universe@3 85 continue;
universe@3 86 }
universe@42 87
universe@42 88 if (list) {
universe@42 89 // create fake root to have a pointer on the true root
universe@42 90 filelist_t root;
universe@42 91 root.next = list;
universe@42 92 filelist_t *parent = &root;
universe@42 93 while (parent->next &&
universe@42 94 (strcasecmp(parent->next->displayname, newentry->displayname) < 0 ||
universe@42 95 (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode))
universe@42 96 ) &&
universe@42 97 (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode))
universe@42 98 ) {
universe@42 99 parent = parent->next;
universe@42 100 }
universe@42 101 newentry->next = parent->next;
universe@42 102 parent->next = newentry;
universe@42 103 list = root.next;
universe@42 104 } else {
universe@42 105 list = newentry;
universe@42 106 }
universe@41 107 }
universe@41 108 }
universe@41 109
universe@41 110 closedir(dirf);
universe@41 111
universe@41 112 return list;
universe@41 113 }
universe@3 114
universe@60 115 void scanDirectory(scanner_t scanner, settings_t* settings,
universe@60 116 string_list_t* output, scanresult_t* result) {
universe@41 117
universe@60 118 result->directory = 0;
universe@60 119 int a;
universe@41 120 bool bfile;
universe@44 121 char *outbuf;
universe@41 122
universe@41 123 filelist_t *filelist = buildFileList(scanner, settings, NULL);
universe@41 124
universe@41 125 while (filelist != NULL) {
universe@41 126
universe@41 127 /* Scan subdirectories */
universe@42 128 if (!S_ISREG(filelist->st_mode)) {
universe@44 129 if (settings->recursive && S_ISDIR(filelist->st_mode)) {
universe@44 130 string_list_t *recoutput = new_string_list_t();
universe@60 131 scanresult_t recresult;
universe@60 132 scanDirectory(
universe@44 133 (scanner_t) {filelist->filename, scanner.spaces+1},
universe@60 134 settings, recoutput, &recresult);
universe@60 135 result->directory += recresult.directory;
universe@44 136 if (!settings->matchesOnly || recoutput->count > 0) {
universe@44 137 outbuf = (char*) malloc(81);
universe@44 138 snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
universe@44 139 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@60 140 60-filelist->displayname_len-scanner.spaces-1, "",
universe@60 141 recresult.directory);
universe@44 142 add_string(output, outbuf);
universe@44 143 for (int i = 0 ; i < recoutput->count ; i++) {
universe@44 144 add_string(output, recoutput->items[i]);
universe@44 145 }
universe@44 146 }
universe@44 147 destroy_string_list_t(recoutput);
universe@44 148 } else {
universe@44 149 outbuf = (char*) malloc(81);
universe@44 150 snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
universe@41 151 filelist->displayname);
universe@44 152 add_string(output, outbuf);
universe@41 153 }
universe@41 154 } else {
universe@30 155 if ((settings->includeSuffixes->count == 0
universe@41 156 || testSuffix(filelist->displayname, settings->includeSuffixes))
universe@41 157 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
universe@41 158
universe@25 159 /* Count lines */
universe@60 160 int lines = 0;
universe@25 161 bfile = false;
universe@25 162 bfile_reset(settings->bfileHeuristics);
universe@54 163 regex_parser_reset(settings->regex);
universe@27 164 char line_buffer[REGEX_MAX_LINELENGTH];
universe@25 165 int line_buffer_offset = 0;
universe@25 166
universe@41 167 FILE *file = fopen(filelist->filename, "r");
universe@3 168 if (file == NULL) {
universe@44 169 outbuf = (char*) malloc(81);
universe@44 170 snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
universe@41 171 filelist->displayname);
universe@44 172 add_string(output, outbuf);
universe@3 173 perror(" File acces failed");
universe@41 174 } else {
universe@41 175 do {
universe@41 176 a = fgetc(file);
universe@3 177
universe@41 178 bfile = bfile_check(settings->bfileHeuristics, a);
universe@3 179
universe@41 180 if (a == 10 || a == EOF) {
universe@41 181 line_buffer[line_buffer_offset] = 0;
universe@41 182 if (regex_parser_do(settings->regex, line_buffer) == 0) {
universe@41 183 /* Only subtract lines when matching has finished */
universe@41 184 if (!regex_parser_matching(settings->regex)) {
universe@41 185 lines -= settings->regex->matched_lines;
universe@41 186 }
universe@41 187 }
universe@21 188
universe@41 189 line_buffer_offset = 0;
universe@41 190 lines++;
universe@41 191 } else {
universe@41 192 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
universe@41 193 line_buffer[line_buffer_offset] = a;
universe@41 194 line_buffer_offset++;
universe@41 195 } else {
universe@41 196 line_buffer[line_buffer_offset-1] = 0;
universe@41 197 settings->confusing_lnlen = true;
universe@28 198 }
universe@28 199 }
universe@41 200 } while (!bfile && a != EOF);
universe@41 201 fclose(file);
universe@25 202
universe@41 203 /* Print and sum line count */
universe@41 204 if (bfile) {
universe@41 205 if (!settings->matchesOnly) {
universe@44 206 outbuf = (char*) malloc(81);
universe@44 207 snprintf(outbuf, 81,
universe@44 208 "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
universe@41 209 filelist->displayname,
universe@41 210 60-filelist->displayname_len-scanner.spaces, "", "binary");
universe@44 211 add_string(output, outbuf);
universe@41 212 }
universe@25 213 } else {
universe@60 214 result->directory += lines;
universe@44 215 outbuf = (char*) malloc(81);
universe@44 216 snprintf(outbuf, 81, "%*s%*s%13d lines\n",
universe@41 217 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 218 60-filelist->displayname_len-scanner.spaces, "", lines);
universe@44 219 add_string(output, outbuf);
universe@3 220 }
universe@21 221 }
universe@16 222 } else {
universe@3 223 if (!settings->matchesOnly) {
universe@22 224 /* Print hint */
universe@44 225 outbuf = (char*) malloc(81);
universe@44 226 snprintf(outbuf, 81, "%*s%*s%19s\n",
universe@41 227 filelist->displayname_len+scanner.spaces, filelist->displayname,
universe@41 228 60-filelist->displayname_len-scanner.spaces, "", "no match");
universe@44 229 add_string(output, outbuf);
universe@3 230 }
universe@3 231 }
universe@3 232 }
universe@41 233
universe@41 234 free(filelist->filename);
universe@41 235 free(filelist->displayname);
universe@41 236 filelist_t *freethis = filelist;
universe@41 237 filelist = filelist->next;
universe@41 238 free(freethis);
universe@3 239 }
universe@3 240 }

mercurial