HuntnGather – Diff between revs 11 and 13

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