HuntnGather – Diff between revs 5 and 8
?pathlinks?
Rev 5 | Rev 8 | |||
---|---|---|---|---|
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 | #include <proto/dos.h> |
15 | #include <proto/dos.h> |
|
16 | #include <proto/exec.h> |
16 | #include <proto/exec.h> |
|
17 | |
17 | |
|
18 | #if !defined ___HAVE_GETOPT___ |
18 | #if !defined ___HAVE_GETOPT___ |
|
19 | #include "getopt.h" |
19 | #include "getopt.h" |
|
20 | #endif |
20 | #endif |
|
21 | |
21 | |
|
22 | #if defined ___AmigaOS___ |
22 | #if defined ___AmigaOS___ |
|
23 | #include <dos/dos.h> |
- | ||
24 | #include <dos/rdargs.h> |
- | ||
25 | #define TEMPLATE "D=Database/K,P=Pattern/A/F" |
- | ||
26 | #define OPT_DATABASE 0 |
- | ||
27 | #define OPT_PATTERN 1 |
- | ||
28 | #define OPT_COUNT 2 |
- | ||
29 | LONG result[OPT_COUNT]; |
- | ||
30 | /*************************************************************************/ |
23 | /*************************************************************************/ |
|
31 | /* Version string used for querrying the program version. */ |
24 | /* Version string used for querrying the program version. */ |
|
32 | /*************************************************************************/ |
25 | /*************************************************************************/ |
|
33 | TEXT version_string[] = |
26 | TEXT version_string[] = |
|
34 | "\0$VER: Hunt 1.4 "__DATE__" by Wizardry and Steamworks"; |
27 | "\0$VER: Hunt 1.5 "__DATE__" by Wizardry and Steamworks"; |
|
35 | #endif |
28 | #endif |
|
36 | |
29 | |
|
37 | #if !defined TRUE |
30 | #if !defined TRUE |
|
38 | #define TRUE 1; |
31 | #define TRUE 1; |
|
39 | #endif |
32 | #endif |
|
40 | |
33 | |
|
41 | #if !defined FALSE |
34 | #if !defined FALSE |
|
42 | #define FALSE 0; |
35 | #define FALSE 0; |
|
43 | #endif |
36 | #endif |
|
44 | |
37 | |
|
45 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
38 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
|
46 | |
39 | |
|
47 | int run = TRUE; |
40 | int run = TRUE; |
|
48 | |
41 | |
|
49 | void SignalHandler(int sig) { |
42 | void SignalHandler(int sig) { |
|
50 | // Toggle the run flag to stop execution. |
43 | // Toggle the run flag to stop execution. |
|
51 | run = FALSE; |
44 | run = FALSE; |
|
52 | } |
45 | } |
|
53 | |
46 | |
|
54 | /* |
47 | /* |
|
55 | * Convert string to uppercase. |
48 | * Convert string to uppercase. |
|
56 | */ |
49 | */ |
|
57 | char *strupr(char *str) { |
50 | char *strupr(char *str) { |
|
58 | char *up; |
51 | char *up; |
|
59 | int i; |
52 | int i; |
|
60 | |
53 | |
|
61 | up = (char *) malloc((strlen(str) + 1) * sizeof(char)); |
54 | up = (char *) malloc((strlen(str) + 1) * sizeof(char)); |
|
62 | sprintf(up, "%s", str); |
55 | sprintf(up, "%s", str); |
|
63 | |
56 | |
|
64 | i = strlen(up); |
57 | i = strlen(up); |
|
65 | while(--i > -1) { |
58 | while(--i > -1) { |
|
66 | up[i] = toupper(up[i]); |
59 | up[i] = toupper(up[i]); |
|
67 | } |
60 | } |
|
68 | |
61 | |
|
69 | return up; |
62 | return up; |
|
70 | } |
63 | } |
|
71 | |
64 | |
|
72 | /* |
65 | /* |
|
73 | * Compare "name" and "needle" for equality. |
66 | * Compare "name" and "needle" for equality. |
|
74 | */ |
67 | */ |
|
75 | int compare(char *name, char *need) { |
68 | int compare(char *name, char *need) { |
|
76 | #if defined ___AmigaOS___ |
69 | #if defined ___AmigaOS___ |
|
77 | ULONG size; |
70 | ULONG size; |
|
78 | char *upn, *upe; |
71 | char *upn, *upe; |
|
79 | int success; |
72 | int success; |
|
80 | UBYTE *pattern; |
73 | UBYTE *pattern; |
|
81 | |
74 | |
|
82 | upe = strupr(need); |
75 | upe = strupr(need); |
|
83 | upn = strupr(name); |
76 | upn = strupr(name); |
|
84 | |
77 | |
|
85 | size = strlen(upe) * 3; |
78 | size = strlen(upe) * 3; |
|
86 | |
79 | |
|
87 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
80 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
|
88 | |
81 | |
|
89 | if(ParsePatternNoCase(upe, pattern, (LONG)size) > 0) { |
82 | if(ParsePatternNoCase(upe, pattern, (LONG)size) > 0) { |
|
90 | success = MatchPatternNoCase(pattern, upn); |
83 | success = MatchPatternNoCase(pattern, upn); |
|
91 | |
84 | |
|
92 | FreeMem(pattern, size); |
85 | FreeMem(pattern, size); |
|
93 | |
86 | |
|
94 | return success; |
87 | return success; |
|
95 | } |
88 | } |
|
96 | |
89 | |
|
97 | FreeMem(pattern, size); |
90 | FreeMem(pattern, size); |
|
98 | } |
91 | } |
|
99 | |
92 | |
|
100 | success = strstr(upn, upe) != NULL; |
93 | success = strstr(upn, upe) != NULL; |
|
101 | free(upn); |
94 | free(upn); |
|
102 | free(upe); |
95 | free(upe); |
|
103 | return success; |
96 | return success; |
|
104 | |
97 | |
|
105 | #else |
98 | #else |
|
106 | return strstr(strupr(name), strupr(need)) != NULL; |
99 | return strstr(strupr(name), strupr(need)) != NULL; |
|
107 | #endif |
100 | #endif |
|
108 | } |
101 | } |
|
109 | |
102 | |
|
110 | |
103 | |
|
111 | /* |
104 | /* |
|
112 | * |
105 | * |
|
113 | * Search the database for a matching string. |
106 | * Search the database for a matching string. |
|
114 | */ |
107 | */ |
|
115 | void SearchDatabase(char *dbFile, char* need) { |
108 | void SearchDatabase(char *dbFile, char* need) { |
|
116 | FILE *fp; |
109 | FILE *fp; |
|
117 | char *name; |
110 | char *name; |
|
118 | char *path; |
111 | char *path; |
|
119 | char c; |
112 | char c; |
|
120 | int i; |
113 | int i; |
|
121 | int side; |
114 | int side; |
|
122 | int match; |
115 | int match; |
|
123 | int total; |
116 | int total; |
|
124 | |
117 | |
|
125 | if((fp = fopen(dbFile, "r")) == NULL) { |
118 | if((fp = fopen(dbFile, "r")) == NULL) { |
|
126 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
119 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
127 | return; |
120 | return; |
|
128 | } |
121 | } |
|
129 | |
122 | |
|
130 | name = (char *) malloc(sizeof(char)); |
123 | name = (char *) malloc(sizeof(char)); |
|
131 | path = (char *) malloc(sizeof(char)); |
124 | path = (char *) malloc(sizeof(char)); |
|
132 | i = 0; |
125 | i = 0; |
|
133 | side = 0; |
126 | side = 0; |
|
134 | match = FALSE; |
127 | match = FALSE; |
|
135 | total = 0; |
128 | total = 0; |
|
136 | while(run && fscanf(fp, "%c", &c) == 1) { |
129 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
137 | #if defined ___AmigaOS___ |
130 | #if defined ___AmigaOS___ |
|
138 | // Check if CTRL+C was pressed and abort the program. |
131 | // Check if CTRL+C was pressed and abort the program. |
|
139 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
132 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
|
140 | run = FALSE; |
133 | run = FALSE; |
|
141 | break; |
134 | break; |
|
142 | } |
135 | } |
|
143 | #endif |
136 | #endif |
|
144 | |
137 | |
|
145 | switch(c) { |
138 | switch(c) { |
|
146 | case '\n': |
139 | case '\n': |
|
147 | ++total; |
140 | ++total; |
|
148 | if(match) { |
141 | if(match) { |
|
149 | fprintf(stdout, "%s\n", path); |
142 | fprintf(stdout, "%s\n", path); |
|
150 | match = FALSE; |
143 | match = FALSE; |
|
151 | } |
144 | } |
|
152 | if(name != NULL) { |
145 | if(name != NULL) { |
|
153 | free(name); |
146 | free(name); |
|
154 | name = (char *) malloc(sizeof(char)); |
147 | name = (char *) malloc(sizeof(char)); |
|
155 | } |
148 | } |
|
156 | --side; |
149 | --side; |
|
157 | i = 0; |
150 | i = 0; |
|
158 | break; |
151 | break; |
|
159 | case '\t': |
152 | case '\t': |
|
160 | // Case insensitive match. |
153 | // Case insensitive match. |
|
161 | if(compare(name, need)) { |
154 | if(compare(name, need)) { |
|
162 | match = TRUE; |
155 | match = TRUE; |
|
163 | } |
156 | } |
|
164 | if(path != NULL) { |
157 | if(path != NULL) { |
|
165 | free(path); |
158 | free(path); |
|
166 | path = (char *) malloc(sizeof(char)); |
159 | path = (char *) malloc(sizeof(char)); |
|
167 | } |
160 | } |
|
168 | ++side; |
161 | ++side; |
|
169 | i = 0; |
162 | i = 0; |
|
170 | break; |
163 | break; |
|
171 | default: |
164 | default: |
|
172 | switch(side) { |
165 | switch(side) { |
|
173 | case 0: |
166 | case 0: |
|
174 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
167 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
|
175 | name[i] = c; |
168 | name[i] = c; |
|
176 | name[i + 1] = '\0'; |
169 | name[i + 1] = '\0'; |
|
177 | break; |
170 | break; |
|
178 | case 1: |
171 | case 1: |
|
179 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
172 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
|
180 | path[i] = c; |
173 | path[i] = c; |
|
181 | path[i + 1] = '\0'; |
174 | path[i + 1] = '\0'; |
|
182 | break; |
175 | break; |
|
183 | default: |
176 | default: |
|
184 | fprintf(stderr, "Database corrupted.\n"); |
177 | fprintf(stderr, "Database corrupted.\n"); |
|
185 | break; |
178 | break; |
|
186 | } |
179 | } |
|
187 | ++i; |
180 | ++i; |
|
188 | break; |
181 | break; |
|
189 | } |
182 | } |
|
190 | } |
183 | } |
|
191 | |
184 | |
|
192 | fclose(fp); |
185 | fclose(fp); |
|
193 | } |
186 | } |
|
194 | |
187 | |
|
195 | /* |
188 | /* |
|
196 | * |
189 | * |
|
197 | * Search the database for the matching string. |
190 | * Search the database for the matching string. |
|
198 | */ |
191 | */ |
|
199 | void Hunt(char *dbFile, char *need) { |
192 | void Hunt(char *dbFile, char *need) { |
|
200 | // Search the database for the matching string. |
193 | // Search the database for the matching string. |
|
201 | SearchDatabase(dbFile, need); |
194 | SearchDatabase(dbFile, need); |
|
202 | } |
195 | } |
|
203 | |
196 | |
|
204 | int main(int argc, char **argv) { |
197 | int main(int argc, char **argv) { |
|
205 | #if defined ___AmigaOS___ |
- | ||
206 | struct RDArgs *rdargs; |
- | ||
207 | struct stat statPath; |
- | ||
208 | UBYTE *dbFile; |
- | ||
209 | UBYTE *argPattern; |
- | ||
210 | |
- | ||
211 | dbFile = DEFAULT_DATABASE_FILE; |
- | ||
212 | argPattern = NULL; |
- | ||
213 | if(rdargs = (struct RDArgs *) AllocDosObject(DOS_RDARGS, NULL)) { |
- | ||
214 | if(ReadArgs(TEMPLATE, result, rdargs) == NULL) { |
- | ||
215 | FreeArgs(rdargs); |
- | ||
216 | FreeDosObject(DOS_RDARGS, rdargs); |
- | ||
217 | return 1; |
- | ||
218 | } |
- | ||
219 | |
- | ||
220 | if(result[OPT_DATABASE] != NULL) { |
- | ||
221 | dbFile = (UBYTE *) result[OPT_DATABASE]; |
- | ||
222 | } |
- | ||
223 | |
- | ||
224 | argPattern = (UBYTE *) result[OPT_PATTERN]; |
- | ||
225 | |
- | ||
226 | FreeArgs(rdargs); |
- | ||
227 | FreeDosObject(DOS_RDARGS, rdargs); |
- | ||
228 | } |
- | ||
229 | |
- | ||
230 | stat(dbFile, &statPath); |
- | ||
231 | if(!S_ISREG(statPath.st_mode)) { |
- | ||
232 | fprintf(stderr, "Database file '%s' is not a file.\n", dbFile); |
- | ||
233 | return 1; |
- | ||
234 | } |
- | ||
235 | |
- | ||
236 | Hunt(dbFile, argPattern); |
- | ||
237 | #else |
- | ||
238 | int option; |
198 | int option; |
|
239 | char *dbFile; |
199 | char *dbFile; |
|
240 | struct stat path; |
200 | struct stat path; |
|
241 | |
201 | |
|
242 | // Bind handler to SIGINT. |
202 | // Bind handler to SIGINT. |
|
243 | signal(SIGINT, SignalHandler); |
203 | signal(SIGINT, SignalHandler); |
|
244 | |
204 | |
|
245 | dbFile = DEFAULT_DATABASE_FILE; |
205 | dbFile = DEFAULT_DATABASE_FILE; |
|
246 | while((option = getopt(argc, argv, "hd:")) != -1) { |
206 | while((option = getopt(argc, argv, "hd:")) != -1) { |
|
247 | switch(option) { |
207 | switch(option) { |
|
248 | case 'd': |
208 | case 'd': |
|
249 | dbFile = optarg; |
209 | dbFile = optarg; |
|
250 | break; |
210 | break; |
|
251 | case 'h': |
211 | case 'h': |
|
252 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
212 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
|
253 | break; |
213 | return 0; |
|
254 | case '?': |
214 | case '?': |
|
255 | fprintf(stderr, "Invalid option %c.\n", optopt); |
215 | fprintf(stderr, "Invalid option %c.\n", optopt); |
|
256 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
216 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
|
257 | return 1; |
217 | return 1; |
|
258 | } |
218 | } |
|
259 | } |
219 | } |
|
260 | |
220 | |
|
261 | if(optind > argc) { |
221 | if(optind > argc) { |
|
262 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
222 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
|
263 | return 1; |
223 | return 1; |
|
264 | } |
224 | } |
|
265 | |
225 | |
|
266 | stat(dbFile, &path); |
226 | stat(dbFile, &path); |
|
267 | if(!S_ISREG(path.st_mode)) { |
227 | if(!S_ISREG(path.st_mode)) { |
|
268 | fprintf(stderr, "Database file '%s' is not a file.\n", dbFile); |
228 | fprintf(stderr, "Database file '%s' is not a file.\n", dbFile); |
|
269 | return 1; |
229 | return 1; |
|
270 | } |
230 | } |
|
271 | |
231 | |
|
272 | Hunt(dbFile, argv[optind]); |
232 | Hunt(dbFile, argv[optind]); |
|
273 | #endif |
- | ||
274 | |
233 | |
|
275 | return 0; |
234 | return 0; |
|
276 | } |
235 | } |
|
277 | |
236 | |