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