Sun, 06 Nov 2011 20:12:55 +0100
replaced stricmp with strcasecmp to stay standard compliant
10 | 1 | /* |
20
43725438ac50
Changed author comments + added signatures for upcomming bfile heuristics
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
2 | * scanner.c |
10 | 3 | * |
4 | * 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
|
5 | * Author: Mike |
10 | 6 | */ |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
7 | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
8 | |
10 | 9 | #include "scanner.h" |
10 | #include "suffix_fnc.h" | |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
11 | #include "bfile_heuristics.h" |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
12 | #include <sys/stat.h> |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
13 | |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
14 | int scanDirectory(scanner_t scanner, settings_t* settings) { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
15 | |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
16 | DIR *dirf; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
17 | struct dirent *entry; |
18
cae1294702aa
Increased warning level, fixed resulting warnings, changed std form c99 to gnu99
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
18 | int lines, a; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
19 | int lineSum = 0; |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
20 | bool bfile; |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
21 | struct stat statbuf; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
22 | |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
23 | if ((dirf = opendir(scanner.dir)) == NULL) { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
24 | printf(scanner.dir); |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
25 | 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
|
26 | return 0; |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
27 | } |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
28 | |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
29 | while ((entry = readdir(dirf)) != NULL) { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
30 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
31 | /* Construct tree view and absolute pathname strings */ |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
32 | char entryname[strlen(entry->d_name)+scanner.spaces]; |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
33 | for (int t = 0 ; t < scanner.spaces ; t++) { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
34 | entryname[t]=' '; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
35 | } |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
36 | entryname[scanner.spaces] = 0; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
37 | strcat(entryname, entry->d_name); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
38 | |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
39 | char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
40 | strcpy(filename, scanner.dir); |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
41 | strncat(filename, &settings->fileSeparator, 1); |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
42 | strcat(filename, entry->d_name); |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
43 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
44 | /* 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
|
45 | if (stat(filename, &statbuf) == 0) { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
46 | if (!(statbuf.st_mode & S_IFREG)) { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
47 | printf("%-60s\n", entryname); |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
48 | if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
49 | lineSum += scanDirectory( |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
50 | (scanner_t) {filename, scanner.spaces+1}, settings); |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
51 | } |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
52 | continue; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
53 | } |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
54 | } else { |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
55 | perror(" Error in stat call"); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
56 | continue; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
57 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
58 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
59 | /* Count lines */ |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
60 | lines = 0; |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
61 | bfile = false; |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
62 | bfile_reset(settings->bfileHeuristics); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
63 | if (testSuffix(filename, settings)) { |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
64 | FILE *file = fopen(filename, "r"); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
65 | if (file == NULL) { |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
66 | printf(entryname); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
67 | perror(" File acces failed"); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
68 | continue; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
69 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
70 | |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
71 | do { |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
72 | a = fgetc(file); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
73 | |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
74 | bfile = bfile_check(settings->bfileHeuristics, a); |
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
75 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
76 | if (a == 10) { |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
77 | lines++; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
78 | } |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
79 | } while (!bfile && a != EOF); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
80 | fclose(file); |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
81 | |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
82 | /* Print and sum line count */ |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
83 | if (bfile) { |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
84 | if (!settings->matchesOnly) { |
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
85 | printf("%-60s%19s\n", entryname, "binary"); |
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
86 | } |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
87 | } else { |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
88 | lineSum += lines; |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
89 | printf("%-60s%13d lines\n", entryname, lines); |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
90 | } |
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
|
91 | } else { |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
92 | if (!settings->matchesOnly) { |
22
4508da679ffb
completed binary file heuristics
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
93 | /* Print hint */ |
21
91e0890464b0
implemented bfile heuristics option + TODO: implement algorithm
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
94 | printf("%-60s%19s\n", entryname, "no match"); |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
95 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
96 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
97 | } |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
98 | } |
23
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
99 | |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
100 | closedir(dirf); |
778388400f7b
encapsulated scanner arguments + enabled optimizer + empty file is no bfile
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
101 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
102 | return lineSum; |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
103 | } |