Sat, 25 Jul 2020 17:42:51 +0200
fixes error message when directory access fails
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 int scanDirectory(scanner_t scanner, settings_t* settings,
116 string_list_t* output) {
118 int lines, a;
119 int lineSum = 0;
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 lines = scanDirectory(
132 (scanner_t) {filelist->filename, scanner.spaces+1},
133 settings, recoutput);
134 lineSum += lines;
135 if (!settings->matchesOnly || recoutput->count > 0) {
136 outbuf = (char*) malloc(81);
137 snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
138 filelist->displayname_len+scanner.spaces, filelist->displayname,
139 60-filelist->displayname_len-scanner.spaces-1, "", lines);
140 add_string(output, outbuf);
141 for (int i = 0 ; i < recoutput->count ; i++) {
142 add_string(output, recoutput->items[i]);
143 }
144 }
145 destroy_string_list_t(recoutput);
146 } else {
147 outbuf = (char*) malloc(81);
148 snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
149 filelist->displayname);
150 add_string(output, outbuf);
151 }
152 } else {
153 if ((settings->includeSuffixes->count == 0
154 || testSuffix(filelist->displayname, settings->includeSuffixes))
155 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
157 /* Count lines */
158 lines = 0;
159 bfile = false;
160 bfile_reset(settings->bfileHeuristics);
161 regex_parser_reset(settings->regex);
162 char line_buffer[REGEX_MAX_LINELENGTH];
163 int line_buffer_offset = 0;
165 FILE *file = fopen(filelist->filename, "r");
166 if (file == NULL) {
167 outbuf = (char*) malloc(81);
168 snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
169 filelist->displayname);
170 add_string(output, outbuf);
171 perror(" File acces failed");
172 } else {
173 do {
174 a = fgetc(file);
176 bfile = bfile_check(settings->bfileHeuristics, a);
178 if (a == 10 || a == EOF) {
179 line_buffer[line_buffer_offset] = 0;
180 if (regex_parser_do(settings->regex, line_buffer) == 0) {
181 /* Only subtract lines when matching has finished */
182 if (!regex_parser_matching(settings->regex)) {
183 lines -= settings->regex->matched_lines;
184 }
185 }
187 line_buffer_offset = 0;
188 lines++;
189 } else {
190 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
191 line_buffer[line_buffer_offset] = a;
192 line_buffer_offset++;
193 } else {
194 line_buffer[line_buffer_offset-1] = 0;
195 settings->confusing_lnlen = true;
196 }
197 }
198 } while (!bfile && a != EOF);
199 fclose(file);
201 /* Print and sum line count */
202 if (bfile) {
203 if (!settings->matchesOnly) {
204 outbuf = (char*) malloc(81);
205 snprintf(outbuf, 81,
206 "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
207 filelist->displayname,
208 60-filelist->displayname_len-scanner.spaces, "", "binary");
209 add_string(output, outbuf);
210 }
211 } else {
212 lineSum += lines;
213 outbuf = (char*) malloc(81);
214 snprintf(outbuf, 81, "%*s%*s%13d lines\n",
215 filelist->displayname_len+scanner.spaces, filelist->displayname,
216 60-filelist->displayname_len-scanner.spaces, "", lines);
217 add_string(output, outbuf);
218 }
219 }
220 } else {
221 if (!settings->matchesOnly) {
222 /* Print hint */
223 outbuf = (char*) malloc(81);
224 snprintf(outbuf, 81, "%*s%*s%19s\n",
225 filelist->displayname_len+scanner.spaces, filelist->displayname,
226 60-filelist->displayname_len-scanner.spaces, "", "no match");
227 add_string(output, outbuf);
228 }
229 }
230 }
232 free(filelist->filename);
233 free(filelist->displayname);
234 filelist_t *freethis = filelist;
235 filelist = filelist->next;
236 free(freethis);
237 }
239 return lineSum;
240 }