HuntnGather – Blame information for rev 5

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