HuntnGather – Blame information for rev 14

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