HuntnGather – Blame information for rev 15

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