HuntnGather – Diff between revs 19 and 22

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