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