HuntnGather – Diff between revs 37 and 38
?pathlinks?
Rev 37 | Rev 38 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
|
3 | /////////////////////////////////////////////////////////////////////////// |
3 | /////////////////////////////////////////////////////////////////////////// |
|
4 | |
4 | |
|
5 | #include <stdio.h> |
5 | #include <stdio.h> |
|
6 | #include <stdlib.h> |
6 | #include <stdlib.h> |
|
7 | #include <string.h> |
7 | #include <string.h> |
|
8 | #include <ctype.h> |
8 | #include <ctype.h> |
|
9 | #if !defined ___AmigaOS___ |
9 | #if !defined ___AmigaOS___ |
|
10 | #include <signal.h> |
10 | #include <signal.h> |
|
11 | #include <dirent.h> |
11 | #include <dirent.h> |
|
12 | #include <sys/stat.h> |
12 | #include <sys/stat.h> |
|
13 | #endif |
13 | #endif |
|
14 | |
14 | |
|
15 | #include <sys/types.h> |
15 | #include <sys/types.h> |
|
16 | |
16 | |
|
17 | #if defined ___AmigaOS___ |
17 | #if defined ___AmigaOS___ |
|
18 | |
18 | |
|
19 | #include <proto/dos.h> |
19 | #include <proto/dos.h> |
|
20 | #include <proto/exec.h> |
20 | #include <proto/exec.h> |
|
21 | #include <proto/utility.h> |
21 | #include <proto/utility.h> |
|
22 | #endif |
22 | #endif |
|
23 | |
23 | |
|
24 | #if defined ___AsyncIO___ |
24 | #if defined ___AsyncIO___ |
|
25 | #include <asyncio.h> |
25 | #include <asyncio.h> |
|
26 | #endif |
26 | #endif |
|
27 | |
27 | |
|
28 | #if !defined ___HAVE_GETOPT___ |
28 | #if !defined ___HAVE_GETOPT___ |
|
29 | #include "/shared/getopt.h" |
29 | #include "/shared/getopt.h" |
|
30 | #endif |
30 | #endif |
|
31 | |
31 | |
|
32 | #include "/shared/utilities.h" |
32 | #include "/shared/utilities.h" |
|
33 | |
33 | |
|
34 | #define PROGRAM_VERSION "1.7.4" |
34 | #define PROGRAM_VERSION "1.7.4" |
|
35 | |
35 | |
|
36 | #if defined ___AmigaOS___ |
36 | #if defined ___AmigaOS___ |
|
37 | /*************************************************************************/ |
37 | /*************************************************************************/ |
|
38 | /* Version string used for querrying the program version. */ |
38 | /* Version string used for querrying the program version. */ |
|
39 | /*************************************************************************/ |
39 | /*************************************************************************/ |
|
40 | TEXT version_string[] = |
40 | TEXT version_string[] = |
|
41 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
41 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
|
42 | #endif |
42 | #endif |
|
43 | |
43 | |
|
44 | int PROGRAM_RUN = TRUE; |
44 | int PROGRAM_RUN = TRUE; |
|
45 | int PROGRAM_VERBOSE = FALSE; |
45 | int PROGRAM_VERBOSE = FALSE; |
|
46 | |
46 | |
|
47 | void SignalHandler(int sig) { |
47 | void SignalHandler(int sig) { |
|
48 | // Toggle the run flag to stop execution. |
48 | // Toggle the run flag to stop execution. |
|
49 | PROGRAM_RUN = FALSE; |
49 | PROGRAM_RUN = FALSE; |
|
50 | } |
50 | } |
|
51 | |
51 | |
|
52 | /* |
52 | /* |
|
53 | * |
53 | * |
|
54 | * Search the database for a matching string. |
54 | * Search the database for a matching string. |
|
55 | */ |
55 | */ |
|
56 | void SearchDatabase(char *dbFile, char* needle) { |
56 | void SearchDatabase(char *dbFile, char* needle) { |
|
57 | #if defined ___AsyncIO___ |
57 | #if defined ___AsyncIO___ |
|
58 | struct AsyncFile *fp; |
58 | struct AsyncFile *fp; |
|
59 | #else |
59 | #else |
|
60 | FILE *fp; |
60 | FILE *fp; |
|
61 | #endif |
61 | #endif |
|
62 | dbEntry *entry; |
62 | dbEntry *entry; |
|
63 | char *line = NULL; |
63 | dbLine *line = NULL; |
|
64 | |
64 | |
|
65 | // Open database file for reading. |
65 | // Open database file for reading. |
|
66 | #if defined ___AsyncIO___ |
66 | #if defined ___AsyncIO___ |
|
67 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
67 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
68 | #else |
68 | #else |
|
69 | if((fp = fopen(dbFile, "r")) == NULL) { |
69 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
70 | #endif |
70 | #endif |
|
71 | fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); |
71 | fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); |
|
72 | return; |
72 | return; |
|
73 | } |
73 | } |
|
74 | |
74 | |
|
75 | while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { |
75 | while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { |
|
76 | #if defined ___AmigaOS___ |
76 | #if defined ___AmigaOS___ |
|
77 | // Check if CTRL+C was pressed and abort the program. |
77 | // Check if CTRL+C was pressed and abort the program. |
|
78 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
78 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
79 | PROGRAM_RUN = FALSE; |
79 | PROGRAM_RUN = FALSE; |
|
80 | continue; |
80 | continue; |
|
81 | } |
81 | } |
|
82 | #endif |
82 | #endif |
|
83 | |
83 | |
|
84 | if((entry = CreateDatabaseEntry(line)) == NULL) { |
84 | if((entry = CreateDatabaseEntry(line)) == NULL) { |
|
85 | fprintf(stderr, "Unable to create database entry.\n"); |
85 | fprintf(stderr, "Unable to create database entry.\n"); |
|
- | 86 | free(line->content); |
||
86 | free(line); |
87 | free(line); |
|
87 | #if defined ___AsyncIO___ |
88 | #if defined ___AsyncIO___ |
|
88 | CloseAsync(fp); |
89 | CloseAsync(fp); |
|
89 | #else |
90 | #else |
|
90 | fclose(fp); |
91 | fclose(fp); |
|
91 | #endif |
92 | #endif |
|
92 | return; |
93 | return; |
|
93 | } |
94 | } |
|
94 | |
95 | |
|
95 | if(StringMatch(entry->name, needle)) { |
96 | if(StringMatch(entry->name, needle)) { |
|
96 | fprintf(stdout, "%s\n", entry->path); |
97 | fprintf(stdout, "%s\n", entry->path); |
|
97 | } |
98 | } |
|
98 | |
99 | |
|
99 | free(entry->name); |
100 | free(entry->name); |
|
100 | free(entry->path); |
101 | free(entry->path); |
|
- | 102 | free(entry); |
||
- | 103 | entry = NULL; |
||
- | 104 | |
||
- | 105 | free(line->content); |
||
101 | free(line); |
106 | free(line); |
|
- | 107 | line = NULL; |
||
102 | } |
108 | } |
|
103 | |
109 | |
|
104 | if(line != NULL) { |
110 | if(line != NULL) { |
|
- | 111 | free(line->content); |
||
105 | free(line); |
112 | free(line); |
|
106 | } |
113 | } |
|
107 | |
114 | |
|
108 | #if defined ___AsyncIO___ |
115 | #if defined ___AsyncIO___ |
|
109 | CloseAsync(fp); |
116 | CloseAsync(fp); |
|
110 | #else |
117 | #else |
|
111 | fclose(fp); |
118 | fclose(fp); |
|
112 | #endif |
119 | #endif |
|
113 | } |
120 | } |
|
114 | |
121 | |
|
115 | /* |
122 | /* |
|
116 | * |
123 | * |
|
117 | * Search the database for the matching string. |
124 | * Search the database for the matching string. |
|
118 | */ |
125 | */ |
|
119 | void Hunt(char *dbFile, char *needle) { |
126 | void Hunt(char *dbFile, char *needle) { |
|
120 | // Search the database for the matching string. |
127 | // Search the database for the matching string. |
|
121 | SearchDatabase(dbFile, needle); |
128 | SearchDatabase(dbFile, needle); |
|
122 | } |
129 | } |
|
123 | |
130 | |
|
124 | void usage(char *name) { |
131 | void usage(char *name) { |
|
125 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
132 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
|
126 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
133 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
|
127 | fprintf(stdout, " \n"); |
134 | fprintf(stdout, " \n"); |
|
128 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
135 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
|
129 | fprintf(stdout, " \n"); |
136 | fprintf(stdout, " \n"); |
|
130 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
137 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
|
131 | fprintf(stdout, " Gather tool that should be searched. \n"); |
138 | fprintf(stdout, " Gather tool that should be searched. \n"); |
|
132 | fprintf(stdout, " \n"); |
139 | fprintf(stdout, " \n"); |
|
133 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
140 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
|
134 | fprintf(stdout, " \n"); |
141 | fprintf(stdout, " \n"); |
|
135 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
142 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
|
136 | } |
143 | } |
|
137 | |
144 | |
|
138 | int main(int argc, char **argv) { |
145 | int main(int argc, char **argv) { |
|
139 | int option; |
146 | int option; |
|
140 | char *dbFile; |
147 | char *dbFile; |
|
141 | |
148 | |
|
142 | // Bind handler to SIGINT. |
149 | // Bind handler to SIGINT. |
|
143 | #if !defined ___AmigaOS___ |
150 | #if !defined ___AmigaOS___ |
|
144 | signal(SIGINT, SignalHandler); |
151 | signal(SIGINT, SignalHandler); |
|
145 | #endif |
152 | #endif |
|
146 | |
153 | |
|
147 | dbFile = DEFAULT_DATABASE_FILE; |
154 | dbFile = DEFAULT_DATABASE_FILE; |
|
148 | while((option = getopt(argc, argv, "hd:")) != -1) { |
155 | while((option = getopt(argc, argv, "hd:")) != -1) { |
|
149 | switch(option) { |
156 | switch(option) { |
|
150 | case 'd': |
157 | case 'd': |
|
151 | dbFile = optarg; |
158 | dbFile = optarg; |
|
152 | break; |
159 | break; |
|
153 | case 'h': |
160 | case 'h': |
|
154 | usage(argv[0]); |
161 | usage(argv[0]); |
|
155 | return 0; |
162 | return 0; |
|
156 | case '?': |
163 | case '?': |
|
157 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
164 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
|
158 | return 5; |
165 | return 5; |
|
159 | } |
166 | } |
|
160 | } |
167 | } |
|
161 | |
168 | |
|
162 | if(optind >= argc) { |
169 | if(optind >= argc) { |
|
163 | usage(argv[0]); |
170 | usage(argv[0]); |
|
164 | return 5; |
171 | return 5; |
|
165 | } |
172 | } |
|
166 | |
173 | |
|
167 | switch(GetFsType(dbFile)) { |
174 | switch(GetFsType(dbFile)) { |
|
168 | case UNKNOWN: |
175 | case UNKNOWN: |
|
169 | case DIRECTORY: |
176 | case DIRECTORY: |
|
170 | fprintf(stderr, "'%s' is not a file.\n", dbFile); |
177 | fprintf(stderr, "'%s' is not a file.\n", dbFile); |
|
171 | return 10; |
178 | return 10; |
|
172 | case REGULAR: |
179 | case REGULAR: |
|
173 | Hunt(dbFile, argv[optind]); |
180 | Hunt(dbFile, argv[optind]); |
|
174 | break; |
181 | break; |
|
175 | } |
182 | } |
|
176 | |
183 | |
|
177 | return 0; |
184 | return 0; |
|
178 | } |
185 | } |
|
179 | |
186 | |