HuntnGather – Diff between revs 50 and 52

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 50 Rev 52
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.6" 34 #define PROGRAM_VERSION "1.7.7"
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 SearchDatabaseExact(char *dbFile, char *needle) { 56 void SearchDatabaseExact(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 dbLine *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 #if defined ___NOCASE_FS___ 75 #if defined ___NOCASE_FS___
76 StrUpr(needle); 76 StrUpr(needle);
77 #endif 77 #endif
78   78  
79 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { 79 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
80 #if defined ___AmigaOS___ 80 #if defined ___AmigaOS___
81 // Check if CTRL+C was pressed and abort the program. 81 // Check if CTRL+C was pressed and abort the program.
82 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 82 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
83 PROGRAM_RUN = FALSE; 83 PROGRAM_RUN = FALSE;
84 continue; 84 continue;
85 } 85 }
86 #endif 86 #endif
87   87  
88 if((entry = CreateDatabaseEntry(line)) == NULL) { 88 if((entry = CreateDatabaseEntry(line)) == NULL) {
89 fprintf(stderr, "Unable to create database entry.\n"); 89 fprintf(stderr, "Unable to create database entry.\n");
90 free(line->string); 90 free(line->string);
91 free(line); 91 free(line);
92 line = NULL; 92 line = NULL;
93   93  
94 #if defined ___AsyncIO___ 94 #if defined ___AsyncIO___
95 CloseAsync(fp); 95 CloseAsync(fp);
96 #else 96 #else
97 fclose(fp); 97 fclose(fp);
98 #endif 98 #endif
99 return; 99 return;
100 } 100 }
101   101  
102 #if defined ___AmigaOS___ 102 #if defined ___AmigaOS___
103 #if defined ___NOCASE_FS___ 103 #if defined ___NOCASE_FS___
104 if(Strnicmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) { 104 if(Strnicmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) {
105 #else 105 #else
106 if(StrnCmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) { 106 if(StrnCmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) {
107 #endif 107 #endif
108 #else 108 #else
109 if(strstr(entry->name, needle) != NULL) { 109 if(strstr(entry->name, needle) != NULL) {
110 #endif 110 #endif
111 fprintf(stdout, "%s\n", entry->path); 111 fprintf(stdout, "%s\n", entry->path);
112 } 112 }
113   113  
114 free(entry->name); 114 free(entry->name);
115 free(entry->path); 115 free(entry->path);
116 free(entry); 116 free(entry);
117 entry = NULL; 117 entry = NULL;
118   118  
119 free(line->string); 119 free(line->string);
120 free(line); 120 free(line);
121 line = NULL; 121 line = NULL;
122 } 122 }
123   123  
124 if(line != NULL) { 124 if(line != NULL) {
125 free(line->string); 125 free(line->string);
126 free(line); 126 free(line);
127 line = NULL; 127 line = NULL;
128 } 128 }
129   129  
130 #if defined ___AsyncIO___ 130 #if defined ___AsyncIO___
131 CloseAsync(fp); 131 CloseAsync(fp);
132 #else 132 #else
133 fclose(fp); 133 fclose(fp);
134 #endif 134 #endif
135 } 135 }
136   136  
137 /* 137 /*
138 * 138 *
139 * Search the database for a matching string. 139 * Search the database for a matching string.
140 */ 140 */
141 #if defined ___AmigaOS___ 141 #if defined ___AmigaOS___
142 void SearchDatabasePattern(char *dbFile, UBYTE *pattern) { 142 void SearchDatabasePattern(char *dbFile, UBYTE *pattern) {
143 #if defined ___AsyncIO___ 143 #if defined ___AsyncIO___
144 struct AsyncFile *fp; 144 struct AsyncFile *fp;
145 #else 145 #else
146 FILE *fp; 146 FILE *fp;
147 #endif 147 #endif
148 dbEntry *entry; 148 dbEntry *entry;
149 dbLine *line = NULL; 149 dbLine *line = NULL;
150   150  
151 // Open database file for reading. 151 // Open database file for reading.
152 #if defined ___AsyncIO___ 152 #if defined ___AsyncIO___
153 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 153 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
154 #else 154 #else
155 if((fp = fopen(dbFile, "r")) == NULL) { 155 if((fp = fopen(dbFile, "r")) == NULL) {
156 #endif 156 #endif
157 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); 157 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
158 return; 158 return;
159 } 159 }
160   160  
161 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { 161 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
162 #if defined ___AmigaOS___ 162 #if defined ___AmigaOS___
163 // Check if CTRL+C was pressed and abort the program. 163 // Check if CTRL+C was pressed and abort the program.
164 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 164 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
165 PROGRAM_RUN = FALSE; 165 PROGRAM_RUN = FALSE;
166 continue; 166 continue;
167 } 167 }
168 #endif 168 #endif
169   169  
170 if((entry = CreateDatabaseEntry(line)) == NULL) { 170 if((entry = CreateDatabaseEntry(line)) == NULL) {
171 fprintf(stderr, "Unable to create database entry.\n"); 171 fprintf(stderr, "Unable to create database entry.\n");
172 free(line->string); 172 free(line->string);
173 free(line); 173 free(line);
174 line = NULL; 174 line = NULL;
175   175  
176 #if defined ___AsyncIO___ 176 #if defined ___AsyncIO___
177 CloseAsync(fp); 177 CloseAsync(fp);
178 #else 178 #else
179 fclose(fp); 179 fclose(fp);
180 #endif 180 #endif
181 return; 181 return;
182 } 182 }
183   183  
184 if(MatchPatternNoCase(pattern, entry->name)) { 184 if(MatchPatternNoCase(pattern, entry->name)) {
185 fprintf(stdout, "%s\n", entry->path); 185 fprintf(stdout, "%s\n", entry->path);
186 } 186 }
187   187  
188 free(entry->name); 188 free(entry->name);
189 free(entry->path); 189 free(entry->path);
190 free(entry); 190 free(entry);
191 entry = NULL; 191 entry = NULL;
192   192  
193 free(line->string); 193 free(line->string);
194 free(line); 194 free(line);
195 line = NULL; 195 line = NULL;
196 } 196 }
197   197  
198 if(line != NULL) { 198 if(line != NULL) {
199 free(line->string); 199 free(line->string);
200 free(line); 200 free(line);
201 line = NULL; 201 line = NULL;
202 } 202 }
203   203  
204 #if defined ___AsyncIO___ 204 #if defined ___AsyncIO___
205 CloseAsync(fp); 205 CloseAsync(fp);
206 #else 206 #else
207 fclose(fp); 207 fclose(fp);
208 #endif 208 #endif
209 } 209 }
210 #endif 210 #endif
211   211  
212 /* 212 /*
213 * 213 *
214 * Search the database for the matching string. 214 * Search the database for the matching string.
215 */ 215 */
216 void Hunt(char *dbFile, char *needle) { 216 void Hunt(char *dbFile, char *needle) {
217 #if defined ___AmigaOS___ 217 #if defined ___AmigaOS___
218 UBYTE *pattern; 218 UBYTE *pattern;
219 ULONG size; 219 ULONG size;
220   220  
221 #if defined ___NOCASE_FS___ 221 #if defined ___NOCASE_FS___
222 StrUpr(needle); 222 StrUpr(needle);
223 #endif 223 #endif
224   224  
225 size = strlen(needle) * 2 + 2; 225 size = strlen(needle) * 2 + 2;
226   226  
227 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { 227 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) {
228 switch(ParsePatternNoCase(needle, pattern, (LONG)size)) { 228 switch(ParsePatternNoCase(needle, pattern, (LONG)size)) {
229 case 1: // the needle contains wildcards 229 case 1: // the needle contains wildcards
230 SearchDatabasePattern(dbFile, pattern); 230 SearchDatabasePattern(dbFile, pattern);
231 break; 231 break;
232 case 0: // no wildcards contained in needle 232 case 0: // no wildcards contained in needle
233 SearchDatabaseExact(dbFile, needle); 233 SearchDatabaseExact(dbFile, needle);
234 break; 234 break;
235 case -1: // overflow condition 235 case -1: // overflow condition
236 fprintf(stderr, "Pattern '%s' could not be parsed.\n", needle); 236 fprintf(stderr, "Pattern '%s' could not be parsed.\n", needle);
237 break; 237 break;
238 } 238 }
239 FreeVec(pattern); 239 FreeVec(pattern);
240 pattern = NULL; 240 pattern = NULL;
241 return; 241 return;
242 } 242 }
243 #endif 243 #endif
244   244  
245 // Search the database for the matching string. 245 // Search the database for the matching string.
246 SearchDatabaseExact(dbFile, needle); 246 SearchDatabaseExact(dbFile, needle);
247 } 247 }
248   248  
249 void usage(char *name) { 249 void usage(char *name) {
250 fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); 250 fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name);
251 fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); 251 fprintf(stdout, "Version: %s \n", PROGRAM_VERSION);
252 fprintf(stdout, " \n"); 252 fprintf(stdout, " \n");
253 fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); 253 fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name);
254 fprintf(stdout, " \n"); 254 fprintf(stdout, " \n");
255 fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); 255 fprintf(stdout, " -d DATABASE A path to a database generated by the \n");
256 fprintf(stdout, " Gather tool that should be searched. \n"); 256 fprintf(stdout, " Gather tool that should be searched. \n");
257 fprintf(stdout, " \n"); 257 fprintf(stdout, " \n");
258 fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); 258 fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n");
259 fprintf(stdout, " \n"); 259 fprintf(stdout, " \n");
260 fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); 260 fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n");
261 } 261 }
262   262  
263 int main(int argc, char **argv) { 263 int main(int argc, char **argv) {
264 int option; 264 int option;
265 char *dbFile; 265 char *dbFile;
266   266  
267 // Bind handler to SIGINT. 267 // Bind handler to SIGINT.
268 #if !defined ___AmigaOS___ 268 #if !defined ___AmigaOS___
269 signal(SIGINT, SignalHandler); 269 signal(SIGINT, SignalHandler);
270 #endif 270 #endif
271   271  
272 dbFile = DEFAULT_DATABASE_FILE; 272 dbFile = DEFAULT_DATABASE_FILE;
273 while((option = getopt(argc, argv, "hd:")) != -1) { 273 while((option = getopt(argc, argv, "hd:")) != -1) {
274 switch(option) { 274 switch(option) {
275 case 'd': 275 case 'd':
276 dbFile = optarg; 276 dbFile = optarg;
277 break; 277 break;
278 case 'h': 278 case 'h':
279 usage(argv[0]); 279 usage(argv[0]);
280 return 0; 280 return 0;
281 case '?': 281 case '?':
282 fprintf(stderr, "Invalid option %c.\n", optopt);; 282 fprintf(stderr, "Invalid option %c.\n", optopt);;
283 return 5; 283 return 5;
284 } 284 }
285 } 285 }
286   286  
287 if(optind >= argc) { 287 if(optind >= argc) {
288 usage(argv[0]); 288 usage(argv[0]);
289 return 5; 289 return 5;
290 } 290 }
291   291  
292 switch(GetFsType(dbFile)) { 292 switch(GetFsType(dbFile)) {
293 case UNKNOWN: 293 case UNKNOWN:
294 case DIRECTORY: 294 case DIRECTORY:
295 fprintf(stderr, "'%s' is not a file.\n", dbFile); 295 fprintf(stderr, "'%s' is not a file.\n", dbFile);
296 return 10; 296 return 10;
297 case REGULAR: 297 case REGULAR:
298 Hunt(dbFile, argv[optind]); 298 Hunt(dbFile, argv[optind]);
299 break; 299 break;
300 } 300 }
301   301  
302 return 0; 302 return 0;
303 } 303 }
304   304