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