HuntnGather – Diff between revs 24 and 26
?pathlinks?
Rev 24 | Rev 26 | |||
---|---|---|---|---|
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 <dirent.h> |
8 | #include <dirent.h> |
|
9 | #include <signal.h> |
9 | #include <signal.h> |
|
10 | #include <ctype.h> |
10 | #include <ctype.h> |
|
11 | |
11 | |
|
12 | #include <sys/types.h> |
12 | #include <sys/types.h> |
|
13 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
|
- | 14 | |
||
14 | |
15 | #if defined ___AmigaOS___ |
|
15 | #include <proto/dos.h> |
16 | #include <proto/dos.h> |
|
- | 17 | #include <proto/exec.h> |
||
- | 18 | #include <proto/utility.h> |
||
16 | #include <proto/exec.h> |
19 | #endif |
|
17 | |
20 | |
|
18 | #if defined ___AsyncIO___ |
21 | #if defined ___AsyncIO___ |
|
19 | #include <asyncio.h> |
22 | #include <asyncio.h> |
|
20 | #endif |
23 | #endif |
|
21 | |
24 | |
|
22 | #if !defined ___HAVE_GETOPT___ |
25 | #if !defined ___HAVE_GETOPT___ |
|
23 | #include "getopt.h" |
26 | #include "getopt.h" |
|
24 | #endif |
27 | #endif |
|
25 | |
28 | |
|
26 | #define PROGRAM_VERSION "1.7.3" |
29 | #define PROGRAM_VERSION "1.7.4" |
|
27 | |
30 | |
|
28 | #if defined ___AmigaOS___ |
31 | #if defined ___AmigaOS___ |
|
29 | /*************************************************************************/ |
32 | /*************************************************************************/ |
|
30 | /* Version string used for querrying the program version. */ |
33 | /* Version string used for querrying the program version. */ |
|
31 | /*************************************************************************/ |
34 | /*************************************************************************/ |
|
32 | TEXT version_string[] = |
35 | TEXT version_string[] = |
|
33 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
36 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
|
34 | #endif |
37 | #endif |
|
35 | |
38 | |
|
36 | #if !defined TRUE |
39 | #if !defined TRUE |
|
37 | #define TRUE 1; |
40 | #define TRUE 1; |
|
38 | #endif |
41 | #endif |
|
39 | |
42 | |
|
40 | #if !defined FALSE |
43 | #if !defined FALSE |
|
41 | #define FALSE 0; |
44 | #define FALSE 0; |
|
42 | #endif |
45 | #endif |
|
43 | |
46 | |
|
44 | #define ASYNC_BUF 8192 |
47 | #define ASYNC_BUF 8192 |
|
45 | #define NAME_BUF 32 |
48 | #define NAME_BUF 32 |
|
46 | #define PATH_BUF 128 |
49 | #define PATH_BUF 128 |
|
47 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
50 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
|
48 | |
51 | |
|
49 | int run = TRUE; |
52 | int run = TRUE; |
|
50 | |
53 | |
|
51 | void SignalHandler(int sig) { |
54 | void SignalHandler(int sig) { |
|
52 | // Toggle the run flag to stop execution. |
55 | // Toggle the run flag to stop execution. |
|
53 | run = FALSE; |
56 | run = FALSE; |
|
54 | } |
57 | } |
|
55 | |
58 | |
|
56 | /* |
59 | /* |
|
57 | * Compare two strings. |
60 | * Compare two strings. |
|
58 | */ |
61 | */ |
|
59 | int compare(char *a, char *b) { |
62 | int compare(char *a, char *b) { |
|
60 | #if defined ___AmigaOS___ |
63 | #if defined ___AmigaOS___ |
|
61 | ULONG size; |
64 | ULONG size; |
|
62 | int success; |
65 | int success; |
|
63 | UBYTE *pattern; |
66 | UBYTE *pattern; |
|
64 | char *e = a; |
67 | char *e = a; |
|
65 | char *n = b; |
68 | char *n = b; |
|
66 | |
- | ||
67 | #if defined ___NOCASE_FS___ |
- | ||
68 | e = strupr(e); |
- | ||
69 | n = strupr(n); |
- | ||
70 | #endif |
- | ||
71 | |
69 | |
|
72 | // "must be at least 2 times as large plus 2 bytes" |
70 | // "must be at least 2 times as large plus 2 bytes" |
|
73 | size = strlen(n) * 2 + 2; |
71 | size = strlen(n) * 2 + 2; |
|
74 | |
72 | |
|
75 | success = FALSE; |
73 | success = FALSE; |
|
76 | |
74 | |
|
77 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
75 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
|
78 | switch(ParsePatternNoCase(n, pattern, (LONG)size)) { |
76 | switch(ParsePatternNoCase(n, pattern, (LONG)size)) { |
|
79 | case 1: // the pattern contains wildcards |
77 | case 1: // the pattern contains wildcards |
|
80 | success = MatchPatternNoCase(pattern, e); |
78 | success = MatchPatternNoCase(pattern, e); |
|
81 | |
79 | |
|
82 | break; |
80 | break; |
|
83 | case 0: // no wildcards so fall back to exact name match |
81 | case 0: // no wildcards so fall back to exact name match |
|
- | 82 | #if defined ___NOCASE_FS___ |
||
- | 83 | success = Strnicmp(e, n, (LONG)strlen(n)) == 0; |
||
- | 84 | #else |
||
84 | success = (strstr(e, n) != NULL); |
85 | success = StrnCmp(e, n, (LONG)strlen(n)) == 0; |
|
- | 86 | #endif |
||
85 | |
87 | |
|
86 | break; |
88 | break; |
|
87 | } |
89 | } |
|
88 | |
90 | |
|
89 | FreeVec(pattern); |
91 | FreeVec(pattern); |
|
90 | } |
92 | } |
|
91 | |
93 | |
|
92 | return success; |
94 | return success; |
|
93 | #else |
95 | #else |
|
94 | int success; |
96 | int success; |
|
95 | char *e = a; |
97 | char *e = a; |
|
96 | char *n = b; |
98 | char *n = b; |
|
97 | |
99 | |
|
98 | success = FALSE; |
100 | success = FALSE; |
|
99 | |
101 | |
|
100 | #if defined ___NOCASE_FS___ |
102 | #if defined ___NOCASE_FS___ |
|
101 | e = strupr(e); |
103 | e = strupr(e); |
|
102 | n = strupr(n); |
104 | n = strupr(n); |
|
103 | #endif |
105 | #endif |
|
- | 106 | |
||
104 | |
107 | // search for substring |
|
105 | success = (strstr(e, n) != NULL); |
108 | success = strstr(e, n) != NULL; |
|
106 | |
109 | |
|
107 | return success; |
110 | return success; |
|
108 | #endif |
111 | #endif |
|
109 | } |
112 | } |
|
110 | |
113 | |
|
111 | |
114 | |
|
112 | /* |
115 | /* |
|
113 | * |
116 | * |
|
114 | * Search the database for a matching string. |
117 | * Search the database for a matching string. |
|
115 | */ |
118 | */ |
|
116 | void SearchDatabase(char *dbFile, char* needle) { |
119 | void SearchDatabase(char *dbFile, char* needle) { |
|
117 | #if defined ___AsyncIO___ |
120 | #if defined ___AsyncIO___ |
|
118 | struct AsyncFile *fp; |
121 | struct AsyncFile *fp; |
|
119 | LONG c; |
122 | LONG c; |
|
120 | #else |
123 | #else |
|
121 | FILE *fp; |
124 | FILE *fp; |
|
122 | char c; |
125 | char c; |
|
123 | #endif |
126 | #endif |
|
124 | char *name; |
127 | char *name; |
|
125 | int name_size; |
128 | int name_size; |
|
126 | char *path; |
129 | char *path; |
|
127 | int path_size; |
130 | int path_size; |
|
128 | int i; |
131 | int i; |
|
129 | int side; |
132 | int side; |
|
130 | int match; |
133 | int match; |
|
131 | int total; |
134 | int total; |
|
132 | |
135 | |
|
133 | #if defined ___AsyncIO___ |
136 | #if defined ___AsyncIO___ |
|
134 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
137 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
|
135 | #else |
138 | #else |
|
136 | if((fp = fopen(dbFile, "r")) == NULL) { |
139 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
137 | #endif |
140 | #endif |
|
138 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
141 | fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile); |
|
139 | return; |
142 | return; |
|
140 | } |
143 | } |
|
141 | |
144 | |
|
142 | name_size = NAME_BUF; |
145 | name_size = NAME_BUF; |
|
143 | name = malloc(name_size * sizeof(*name)); |
146 | name = malloc(name_size * sizeof(*name)); |
|
144 | path_size = PATH_BUF; |
147 | path_size = PATH_BUF; |
|
145 | path = malloc(path_size * sizeof(*path)); |
148 | path = malloc(path_size * sizeof(*path)); |
|
146 | |
149 | |
|
147 | i = 0; |
150 | i = 0; |
|
148 | side = 0; |
151 | side = 0; |
|
149 | match = FALSE; |
152 | match = FALSE; |
|
150 | total = 0; |
153 | total = 0; |
|
151 | |
154 | |
|
152 | #if defined ___AsyncIO___ |
155 | #if defined ___AsyncIO___ |
|
153 | while(run && (c = ReadCharAsync(fp)) != -1) { |
156 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
154 | #else |
157 | #else |
|
155 | while(run && fscanf(fp, "%c", &c) == 1) { |
158 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
156 | #endif |
159 | #endif |
|
157 | #if defined ___AmigaOS___ |
160 | #if defined ___AmigaOS___ |
|
158 | // Check if CTRL+C was pressed and abort the program. |
161 | // Check if CTRL+C was pressed and abort the program. |
|
159 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
162 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
160 | run = FALSE; |
163 | run = FALSE; |
|
161 | break; |
164 | continue; |
|
162 | } |
165 | } |
|
163 | #endif |
166 | #endif |
|
164 | |
167 | |
|
165 | switch(c) { |
168 | switch(c) { |
|
166 | case '\n': |
169 | case '\n': |
|
167 | ++total; |
170 | ++total; |
|
168 | if(match) { |
171 | if(match) { |
|
169 | fprintf(stdout, "%s\n", path); |
172 | fprintf(stdout, "%s\n", path); |
|
170 | match = FALSE; |
173 | match = FALSE; |
|
171 | } |
174 | } |
|
172 | if(name != NULL) { |
175 | if(name != NULL) { |
|
173 | free(name); |
176 | free(name); |
|
174 | name_size = NAME_BUF; |
177 | name_size = NAME_BUF; |
|
175 | name = malloc(name_size * sizeof(*name)); |
178 | name = malloc(name_size * sizeof(*name)); |
|
176 | } |
179 | } |
|
177 | --side; |
180 | --side; |
|
178 | i = 0; |
181 | i = 0; |
|
179 | break; |
182 | break; |
|
180 | case '\t': |
183 | case '\t': |
|
181 | // Case insensitive match. |
184 | // Case insensitive match. |
|
182 | if(compare(name, needle)) { |
185 | if(compare(name, needle)) { |
|
183 | match = TRUE; |
186 | match = TRUE; |
|
184 | } |
187 | } |
|
185 | if(path != NULL) { |
188 | if(path != NULL) { |
|
186 | free(path); |
189 | free(path); |
|
187 | path_size = PATH_BUF; |
190 | path_size = PATH_BUF; |
|
188 | path = malloc(path_size * sizeof(*name)); |
191 | path = malloc(path_size * sizeof(*name)); |
|
189 | } |
192 | } |
|
190 | ++side; |
193 | ++side; |
|
191 | i = 0; |
194 | i = 0; |
|
192 | break; |
195 | break; |
|
193 | default: |
196 | default: |
|
194 | switch(side) { |
197 | switch(side) { |
|
195 | case 0: |
198 | case 0: |
|
196 | if(strlen(name) == name_size) { |
199 | if(strlen(name) == name_size) { |
|
197 | name_size = 1.5 * name_size; |
200 | name_size = 1.5 * name_size; |
|
198 | name = realloc(name, name_size * sizeof(*name)); |
201 | name = realloc(name, name_size * sizeof(*name)); |
|
199 | } |
202 | } |
|
200 | //name = realloc(name, (i + 1 + 1) * sizeof(*name)); |
- | ||
201 | name[i] = c; |
203 | name[i] = c; |
|
202 | name[i + 1] = '\0'; |
204 | name[i + 1] = '\0'; |
|
203 | break; |
205 | break; |
|
204 | case 1: |
206 | case 1: |
|
205 | if(strlen(path) == path_size) { |
207 | if(strlen(path) == path_size) { |
|
206 | path_size = 1.5 * path_size; |
208 | path_size = 1.5 * path_size; |
|
207 | path = realloc(path, path_size * sizeof(*path)); |
209 | path = realloc(path, path_size * sizeof(*path)); |
|
208 | } |
210 | } |
|
209 | //path = realloc(path, (i + 1 + 1) * sizeof(*path)); |
- | ||
210 | path[i] = c; |
211 | path[i] = c; |
|
211 | path[i + 1] = '\0'; |
212 | path[i + 1] = '\0'; |
|
212 | break; |
213 | break; |
|
213 | default: |
214 | default: |
|
214 | fprintf(stderr, "Database corrupted.\n"); |
215 | fprintf(stderr, "Database corrupted.\n"); |
|
215 | break; |
216 | break; |
|
216 | } |
217 | } |
|
217 | ++i; |
218 | ++i; |
|
218 | break; |
219 | break; |
|
219 | } |
220 | } |
|
220 | } |
221 | } |
|
221 | |
222 | |
|
222 | free(name); |
223 | free(name); |
|
223 | free(path); |
224 | free(path); |
|
224 | |
225 | |
|
225 | #if defined ___AsyncIO___ |
226 | #if defined ___AsyncIO___ |
|
226 | CloseAsync(fp); |
227 | CloseAsync(fp); |
|
227 | #else |
228 | #else |
|
228 | fclose(fp); |
229 | fclose(fp); |
|
229 | #endif |
230 | #endif |
|
230 | } |
231 | } |
|
231 | |
232 | |
|
232 | /* |
233 | /* |
|
233 | * |
234 | * |
|
234 | * Search the database for the matching string. |
235 | * Search the database for the matching string. |
|
235 | */ |
236 | */ |
|
236 | void Hunt(char *dbFile, char *needle) { |
237 | void Hunt(char *dbFile, char *needle) { |
|
237 | // Search the database for the matching string. |
238 | // Search the database for the matching string. |
|
238 | SearchDatabase(dbFile, needle); |
239 | SearchDatabase(dbFile, needle); |
|
239 | } |
240 | } |
|
240 | |
241 | |
|
241 | void usage(char *name) { |
242 | void usage(char *name) { |
|
242 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
243 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
|
243 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
244 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
|
244 | fprintf(stdout, " \n"); |
245 | fprintf(stdout, " \n"); |
|
245 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
246 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
|
246 | fprintf(stdout, " \n"); |
247 | fprintf(stdout, " \n"); |
|
247 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
248 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
|
248 | fprintf(stdout, " Gather tool that should be searched. \n"); |
249 | fprintf(stdout, " Gather tool that should be searched. \n"); |
|
249 | fprintf(stdout, " \n"); |
250 | fprintf(stdout, " \n"); |
|
250 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
251 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
|
251 | fprintf(stdout, " \n"); |
252 | fprintf(stdout, " \n"); |
|
252 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
253 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
|
253 | } |
254 | } |
|
254 | |
255 | |
|
255 | int main(int argc, char **argv) { |
256 | int main(int argc, char **argv) { |
|
256 | int option; |
257 | int option; |
|
257 | char *dbFile; |
258 | char *dbFile; |
|
258 | struct stat path; |
259 | struct stat path; |
|
259 | |
260 | |
|
260 | // Bind handler to SIGINT. |
261 | // Bind handler to SIGINT. |
|
- | 262 | #if !defined ___AmigaOS___ |
||
261 | signal(SIGINT, SignalHandler); |
263 | signal(SIGINT, SignalHandler); |
|
- | 264 | #endif |
||
262 | |
265 | |
|
263 | dbFile = DEFAULT_DATABASE_FILE; |
266 | dbFile = DEFAULT_DATABASE_FILE; |
|
264 | while((option = getopt(argc, argv, "hd:")) != -1) { |
267 | while((option = getopt(argc, argv, "hd:")) != -1) { |
|
265 | switch(option) { |
268 | switch(option) { |
|
266 | case 'd': |
269 | case 'd': |
|
267 | dbFile = optarg; |
270 | dbFile = optarg; |
|
268 | break; |
271 | break; |
|
269 | case 'h': |
272 | case 'h': |
|
270 | usage(argv[0]); |
273 | usage(argv[0]); |
|
271 | return 0; |
274 | return 0; |
|
272 | case '?': |
275 | case '?': |
|
273 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
276 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
|
274 | return 1; |
277 | return 1; |
|
275 | } |
278 | } |
|
276 | } |
279 | } |
|
277 | |
280 | |
|
278 | if(optind >= argc) { |
281 | if(optind >= argc) { |
|
279 | usage(argv[0]); |
282 | usage(argv[0]); |
|
280 | return 1; |
283 | return 1; |
|
281 | } |
284 | } |
|
282 | |
285 | |
|
283 | stat(dbFile, &path); |
286 | stat(dbFile, &path); |
|
284 | if(!S_ISREG(path.st_mode)) { |
287 | if(!S_ISREG(path.st_mode)) { |
|
285 | fprintf(stderr, "Database file '%s' is not a file.\n", dbFile); |
288 | fprintf(stderr, "'%s' is not a file.\n", dbFile); |
|
286 | return 1; |
289 | return 1; |
|
287 | } |
290 | } |
|
288 | |
291 | |
|
289 | Hunt(dbFile, argv[optind]); |
292 | Hunt(dbFile, argv[optind]); |
|
290 | |
293 | |
|
291 | return 0; |
294 | return 0; |
|
292 | } |
295 | } |
|
293 | |
296 | |