HuntnGather – Blame information for rev 19

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