Tue, 21 May 2013 13:19:37 +0200
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
10 | 1 | /* |
34
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
36
a7ff583e153f
updated copyright year + added make install + removed project files
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
3 | * Copyright 2013 Mike Becker. All rights reserved. |
34
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
4 | * |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
5 | * Redistribution and use in source and binary forms, with or without |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
6 | * modification, are permitted provided that the following conditions are met: |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
7 | * |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
8 | * 1. Redistributions of source code must retain the above copyright |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
9 | * notice, this list of conditions and the following disclaimer. |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
10 | * |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
12 | * notice, this list of conditions and the following disclaimer in the |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
13 | * documentation and/or other materials provided with the distribution. |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
14 | * |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
25 | * |
20
43725438ac50
Changed author comments + added signatures for upcomming bfile heuristics
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
26 | * scanner.c |
10 | 27 | * |
28 | * Created on: 23.05.2011 | |
20
43725438ac50
Changed author comments + added signatures for upcomming bfile heuristics
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
29 | * Author: Mike |
10 | 30 | */ |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
31 | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
32 | |
10 | 33 | #include "scanner.h" |
34 | #include "suffix_fnc.h" | |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
35 | #include "bfile_heuristics.h" |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
36 | #include "regex_parser.h" |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
37 | #include <sys/stat.h> |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
38 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
39 | typedef struct filelist filelist_t; |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
40 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
41 | struct filelist { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
42 | char *displayname; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
43 | int displayname_len; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
44 | char *filename; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
45 | int st_mode; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
46 | filelist_t *next; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
47 | }; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
48 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
49 | filelist_t *buildFileList(scanner_t scanner, settings_t* settings, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
50 | filelist_t* list) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
51 | |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
52 | DIR *dirf; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
53 | struct dirent *entry; |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
54 | struct stat statbuf; |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
55 | filelist_t *listentry = list; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
56 | |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
57 | if ((dirf = opendir(scanner.dir)) == NULL) { |
34
fa9bda32de17
moved src files to src subdirectory and added licence text
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
58 | printf("%s", scanner.dir); |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
59 | perror(" Directory access failed"); |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
60 | return 0; |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
61 | } |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
62 | |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
63 | while ((entry = readdir(dirf)) != NULL) { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
64 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
65 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
66 | /* Create new filelist entry */ |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
67 | filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
68 | // TODO: don't just append - create a sorted list! |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
69 | if (listentry) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
70 | listentry->next = newentry; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
71 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
72 | listentry = newentry; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
73 | if (!list) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
74 | list = listentry; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
75 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
76 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
77 | listentry->next = NULL; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
78 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
79 | listentry->displayname_len = strlen(entry->d_name); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
80 | listentry->displayname = (char*) malloc(listentry->displayname_len+1); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
81 | memcpy(listentry->displayname, entry->d_name, listentry->displayname_len); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
82 | listentry->displayname[listentry->displayname_len] = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
83 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
84 | listentry->st_mode = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
85 | |
40
5938a9b74e8e
improved printing the results and fixed bug where the filename was displayed as an empty string
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
86 | /* Construct absolute pathname string */ |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
87 | size_t dirnamelen = strlen(scanner.dir); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
88 | char *filename = (char*) malloc(2+dirnamelen+listentry->displayname_len); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
89 | memcpy(filename, scanner.dir, dirnamelen); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
90 | filename[dirnamelen] = settings->fileSeparator; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
91 | memcpy(filename+dirnamelen+1, entry->d_name, listentry->displayname_len); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
92 | filename[1+dirnamelen+listentry->displayname_len] = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
93 | listentry->filename = filename; |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
94 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
95 | /* Check for subdirectory */ |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
96 | if (stat(filename, &statbuf) == 0) { |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
97 | listentry->st_mode = statbuf.st_mode; |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
98 | } else { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
99 | perror(" Error in stat call"); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
100 | continue; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
101 | } |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
102 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
103 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
104 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
105 | closedir(dirf); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
106 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
107 | return list; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
108 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
109 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
110 | int scanDirectory(scanner_t scanner, settings_t* settings) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
111 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
112 | int lines, a; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
113 | int lineSum = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
114 | bool bfile; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
115 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
116 | filelist_t *filelist = buildFileList(scanner, settings, NULL); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
117 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
118 | while (filelist != NULL) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
119 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
120 | /* Scan subdirectories */ |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
121 | if (!(filelist->st_mode & S_IFREG)) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
122 | printf("%*s\n", filelist->displayname_len+scanner.spaces, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
123 | filelist->displayname); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
124 | if (settings->recursive && (filelist->st_mode & S_IFDIR)) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
125 | scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1}, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
126 | settings); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
127 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
128 | } else { |
30
d642fdb6745e
allow parallel use of -s and -S
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
129 | if ((settings->includeSuffixes->count == 0 |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
130 | || testSuffix(filelist->displayname, settings->includeSuffixes)) |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
131 | && !testSuffix(filelist->displayname, settings->excludeSuffixes)) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
132 | |
25
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
133 | /* Count lines */ |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
134 | lines = 0; |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
135 | bfile = false; |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
136 | bfile_reset(settings->bfileHeuristics); |
27
95a958e3de88
added regexp_parser struct and compile function
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
137 | char line_buffer[REGEX_MAX_LINELENGTH]; |
25
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
138 | int line_buffer_offset = 0; |
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
139 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
140 | FILE *file = fopen(filelist->filename, "r"); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
141 | if (file == NULL) { |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
142 | printf("%*s", filelist->displayname_len+scanner.spaces, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
143 | filelist->displayname); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
144 | perror(" File acces failed"); |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
145 | } else { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
146 | do { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
147 | a = fgetc(file); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
148 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
149 | bfile = bfile_check(settings->bfileHeuristics, a); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
150 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
151 | if (a == 10 || a == EOF) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
152 | line_buffer[line_buffer_offset] = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
153 | if (regex_parser_do(settings->regex, line_buffer) == 0) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
154 | /* Only subtract lines when matching has finished */ |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
155 | if (!regex_parser_matching(settings->regex)) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
156 | lines -= settings->regex->matched_lines; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
157 | } |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
158 | } |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
159 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
160 | line_buffer_offset = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
161 | lines++; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
162 | } else { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
163 | if (line_buffer_offset < REGEX_MAX_LINELENGTH) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
164 | line_buffer[line_buffer_offset] = a; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
165 | line_buffer_offset++; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
166 | } else { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
167 | line_buffer[line_buffer_offset-1] = 0; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
168 | settings->confusing_lnlen = true; |
28 | 169 | } |
170 | } | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
171 | } while (!bfile && a != EOF); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
172 | fclose(file); |
25
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
173 | |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
174 | /* Print and sum line count */ |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
175 | if (bfile) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
176 | if (!settings->matchesOnly) { |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
177 | printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
178 | filelist->displayname, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
179 | 60-filelist->displayname_len-scanner.spaces, "", "binary"); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
180 | } |
25
802c5382f499
Added line buffer (and warning message - there is no regexp parser, though)
Mike Becker <universe@uap-core.de>
parents:
23
diff
changeset
|
181 | } else { |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
182 | lineSum += lines; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
183 | printf("%*s%*s%13d lines\n", |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
184 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
185 | 60-filelist->displayname_len-scanner.spaces, "", lines); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
186 | } |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
187 | } |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
188 | } else { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
189 | if (!settings->matchesOnly) { |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
190 | /* Print hint */ |
40
5938a9b74e8e
improved printing the results and fixed bug where the filename was displayed as an empty string
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
191 | printf("%*s%*s%19s\n", |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
192 | filelist->displayname_len+scanner.spaces, filelist->displayname, |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
193 | 60-filelist->displayname_len-scanner.spaces, "", "no match"); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
194 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
195 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
196 | } |
41
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
197 | |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
198 | free(filelist->filename); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
199 | free(filelist->displayname); |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
200 | filelist_t *freethis = filelist; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
201 | filelist = filelist->next; |
c2e73e175341
replaced direct scanning of directories with a two-step approach (first: create filename list, second: scan)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
202 | free(freethis); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
203 | } |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
204 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
205 | return lineSum; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
206 | } |