HuntnGather – Blame information for rev 33

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