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