Wed, 22 May 2013 10:26:22 +0200
file list is now sorted
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2013 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) {
122 int lines, a;
123 int lineSum = 0;
124 bool bfile;
126 filelist_t *filelist = buildFileList(scanner, settings, NULL);
128 while (filelist != NULL) {
130 /* Scan subdirectories */
131 if (!S_ISREG(filelist->st_mode)) {
132 printf("%*s\n", filelist->displayname_len+scanner.spaces,
133 filelist->displayname);
134 if (settings->recursive && S_ISDIR(filelist->st_mode)) {
135 scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1},
136 settings);
137 }
138 } else {
139 if ((settings->includeSuffixes->count == 0
140 || testSuffix(filelist->displayname, settings->includeSuffixes))
141 && !testSuffix(filelist->displayname, settings->excludeSuffixes)) {
143 /* Count lines */
144 lines = 0;
145 bfile = false;
146 bfile_reset(settings->bfileHeuristics);
147 char line_buffer[REGEX_MAX_LINELENGTH];
148 int line_buffer_offset = 0;
150 FILE *file = fopen(filelist->filename, "r");
151 if (file == NULL) {
152 printf("%*s", filelist->displayname_len+scanner.spaces,
153 filelist->displayname);
154 perror(" File acces failed");
155 } else {
156 do {
157 a = fgetc(file);
159 bfile = bfile_check(settings->bfileHeuristics, a);
161 if (a == 10 || a == EOF) {
162 line_buffer[line_buffer_offset] = 0;
163 if (regex_parser_do(settings->regex, line_buffer) == 0) {
164 /* Only subtract lines when matching has finished */
165 if (!regex_parser_matching(settings->regex)) {
166 lines -= settings->regex->matched_lines;
167 }
168 }
170 line_buffer_offset = 0;
171 lines++;
172 } else {
173 if (line_buffer_offset < REGEX_MAX_LINELENGTH) {
174 line_buffer[line_buffer_offset] = a;
175 line_buffer_offset++;
176 } else {
177 line_buffer[line_buffer_offset-1] = 0;
178 settings->confusing_lnlen = true;
179 }
180 }
181 } while (!bfile && a != EOF);
182 fclose(file);
184 /* Print and sum line count */
185 if (bfile) {
186 if (!settings->matchesOnly) {
187 printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
188 filelist->displayname,
189 60-filelist->displayname_len-scanner.spaces, "", "binary");
190 }
191 } else {
192 lineSum += lines;
193 printf("%*s%*s%13d lines\n",
194 filelist->displayname_len+scanner.spaces, filelist->displayname,
195 60-filelist->displayname_len-scanner.spaces, "", lines);
196 }
197 }
198 } else {
199 if (!settings->matchesOnly) {
200 /* Print hint */
201 printf("%*s%*s%19s\n",
202 filelist->displayname_len+scanner.spaces, filelist->displayname,
203 60-filelist->displayname_len-scanner.spaces, "", "no match");
204 }
205 }
206 }
208 free(filelist->filename);
209 free(filelist->displayname);
210 filelist_t *freethis = filelist;
211 filelist = filelist->next;
212 free(freethis);
213 }
215 return lineSum;
216 }