HuntnGather – Diff between revs 8 and 9

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