HuntnGather – Diff between revs 9 and 10

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