HuntnGather – Diff between revs 15 and 19

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