HuntnGather – Blame information for rev 11

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