HuntnGather – Diff between revs 49 and 50

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