HuntnGather – Blame information for rev 50
?pathlinks?
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> |
||
27 | office | 8 | #include <ctype.h> |
9 | #if !defined ___AmigaOS___ |
||
1 | office | 10 | #include <signal.h> |
29 | office | 11 | #include <dirent.h> |
12 | #include <sys/stat.h> |
||
27 | office | 13 | #endif |
1 | office | 14 | |
2 | office | 15 | #include <sys/types.h> |
16 | |||
26 | office | 17 | #if defined ___AmigaOS___ |
33 | office | 18 | |
1 | office | 19 | #include <proto/dos.h> |
20 | #include <proto/exec.h> |
||
26 | office | 21 | #include <proto/utility.h> |
22 | #endif |
||
1 | office | 23 | |
22 | office | 24 | #if defined ___AsyncIO___ |
25 | #include <asyncio.h> |
||
26 | #endif |
||
27 | |||
5 | office | 28 | #if !defined ___HAVE_GETOPT___ |
33 | office | 29 | #include "/shared/getopt.h" |
1 | office | 30 | #endif |
31 | |||
33 | office | 32 | #include "/shared/utilities.h" |
33 | |||
47 | office | 34 | #define PROGRAM_VERSION "1.7.6" |
19 | office | 35 | |
5 | office | 36 | #if defined ___AmigaOS___ |
37 | /*************************************************************************/ |
||
38 | /* Version string used for querrying the program version. */ |
||
39 | /*************************************************************************/ |
||
40 | TEXT version_string[] = |
||
19 | office | 41 | "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; |
5 | office | 42 | #endif |
43 | |||
33 | office | 44 | int PROGRAM_RUN = TRUE; |
45 | int PROGRAM_VERBOSE = FALSE; |
||
1 | office | 46 | |
47 | void SignalHandler(int sig) { |
||
48 | // Toggle the run flag to stop execution. |
||
33 | office | 49 | PROGRAM_RUN = FALSE; |
1 | office | 50 | } |
51 | |||
52 | /* |
||
53 | * |
||
54 | * Search the database for a matching string. |
||
55 | */ |
||
49 | office | 56 | void SearchDatabaseExact(char *dbFile, char *needle) { |
22 | office | 57 | #if defined ___AsyncIO___ |
58 | struct AsyncFile *fp; |
||
59 | #else |
||
1 | office | 60 | FILE *fp; |
22 | office | 61 | #endif |
37 | office | 62 | dbEntry *entry; |
38 | office | 63 | dbLine *line = NULL; |
1 | office | 64 | |
37 | office | 65 | // Open database file for reading. |
22 | office | 66 | #if defined ___AsyncIO___ |
67 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
68 | #else |
||
1 | office | 69 | if((fp = fopen(dbFile, "r")) == NULL) { |
22 | office | 70 | #endif |
37 | office | 71 | fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); |
1 | office | 72 | return; |
73 | } |
||
74 | |||
50 | office | 75 | #if defined ___NOCASE_FS___ |
76 | StrUpr(needle); |
||
77 | #endif |
||
78 | |||
37 | office | 79 | while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { |
1 | office | 80 | #if defined ___AmigaOS___ |
81 | // Check if CTRL+C was pressed and abort the program. |
||
37 | office | 82 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
33 | office | 83 | PROGRAM_RUN = FALSE; |
26 | office | 84 | continue; |
1 | office | 85 | } |
86 | #endif |
||
87 | |||
37 | office | 88 | if((entry = CreateDatabaseEntry(line)) == NULL) { |
89 | fprintf(stderr, "Unable to create database entry.\n"); |
||
40 | office | 90 | free(line->string); |
37 | office | 91 | free(line); |
40 | office | 92 | line = NULL; |
43 | office | 93 | |
37 | office | 94 | #if defined ___AsyncIO___ |
95 | CloseAsync(fp); |
||
96 | #else |
||
97 | fclose(fp); |
||
98 | #endif |
||
99 | return; |
||
1 | office | 100 | } |
37 | office | 101 | |
49 | office | 102 | #if defined ___AmigaOS___ |
103 | #if defined ___NOCASE_FS___ |
||
104 | if(Strnicmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) { |
||
105 | #else |
||
106 | if(StrnCmp(entry->name, needle, StringLenMax(entry->name, needle)) == 0) { |
||
107 | #endif |
||
108 | #else |
||
109 | if(strstr(entry->name, needle) != NULL) { |
||
110 | #endif |
||
37 | office | 111 | fprintf(stdout, "%s\n", entry->path); |
112 | } |
||
113 | |||
114 | free(entry->name); |
||
115 | free(entry->path); |
||
38 | office | 116 | free(entry); |
117 | entry = NULL; |
||
118 | |||
40 | office | 119 | free(line->string); |
37 | office | 120 | free(line); |
38 | office | 121 | line = NULL; |
1 | office | 122 | } |
123 | |||
37 | office | 124 | if(line != NULL) { |
40 | office | 125 | free(line->string); |
37 | office | 126 | free(line); |
40 | office | 127 | line = NULL; |
37 | office | 128 | } |
9 | office | 129 | |
22 | office | 130 | #if defined ___AsyncIO___ |
131 | CloseAsync(fp); |
||
132 | #else |
||
1 | office | 133 | fclose(fp); |
22 | office | 134 | #endif |
1 | office | 135 | } |
136 | |||
2 | office | 137 | /* |
138 | * |
||
49 | office | 139 | * Search the database for a matching string. |
140 | */ |
||
50 | office | 141 | #if defined ___AmigaOS___ |
49 | office | 142 | void SearchDatabasePattern(char *dbFile, UBYTE *pattern) { |
143 | #if defined ___AsyncIO___ |
||
144 | struct AsyncFile *fp; |
||
145 | #else |
||
146 | FILE *fp; |
||
147 | #endif |
||
148 | dbEntry *entry; |
||
149 | dbLine *line = NULL; |
||
150 | |||
151 | // Open database file for reading. |
||
152 | #if defined ___AsyncIO___ |
||
153 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
154 | #else |
||
155 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
156 | #endif |
||
157 | fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { |
||
162 | #if defined ___AmigaOS___ |
||
163 | // Check if CTRL+C was pressed and abort the program. |
||
164 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
||
165 | PROGRAM_RUN = FALSE; |
||
166 | continue; |
||
167 | } |
||
168 | #endif |
||
169 | |||
170 | if((entry = CreateDatabaseEntry(line)) == NULL) { |
||
171 | fprintf(stderr, "Unable to create database entry.\n"); |
||
172 | free(line->string); |
||
173 | free(line); |
||
174 | line = NULL; |
||
175 | |||
176 | #if defined ___AsyncIO___ |
||
177 | CloseAsync(fp); |
||
178 | #else |
||
179 | fclose(fp); |
||
180 | #endif |
||
181 | return; |
||
182 | } |
||
183 | |||
184 | if(MatchPatternNoCase(pattern, entry->name)) { |
||
185 | fprintf(stdout, "%s\n", entry->path); |
||
186 | } |
||
187 | |||
188 | free(entry->name); |
||
189 | free(entry->path); |
||
190 | free(entry); |
||
191 | entry = NULL; |
||
192 | |||
193 | free(line->string); |
||
194 | free(line); |
||
195 | line = NULL; |
||
196 | } |
||
197 | |||
198 | if(line != NULL) { |
||
199 | free(line->string); |
||
200 | free(line); |
||
201 | line = NULL; |
||
202 | } |
||
203 | |||
204 | #if defined ___AsyncIO___ |
||
205 | CloseAsync(fp); |
||
206 | #else |
||
207 | fclose(fp); |
||
208 | #endif |
||
209 | } |
||
50 | office | 210 | #endif |
49 | office | 211 | |
212 | /* |
||
213 | * |
||
2 | office | 214 | * Search the database for the matching string. |
215 | */ |
||
24 | office | 216 | void Hunt(char *dbFile, char *needle) { |
49 | office | 217 | #if defined ___AmigaOS___ |
218 | UBYTE *pattern; |
||
219 | ULONG size; |
||
220 | |||
221 | #if defined ___NOCASE_FS___ |
||
222 | StrUpr(needle); |
||
223 | #endif |
||
224 | |||
225 | size = strlen(needle) * 2 + 2; |
||
226 | |||
227 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
||
228 | switch(ParsePatternNoCase(needle, pattern, (LONG)size)) { |
||
229 | case 1: // the needle contains wildcards |
||
230 | SearchDatabasePattern(dbFile, pattern); |
||
231 | break; |
||
232 | case 0: // no wildcards contained in needle |
||
233 | SearchDatabaseExact(dbFile, needle); |
||
234 | break; |
||
235 | case -1: // overflow condition |
||
236 | fprintf(stderr, "Pattern '%s' could not be parsed.\n", needle); |
||
237 | break; |
||
238 | } |
||
239 | FreeVec(pattern); |
||
240 | pattern = NULL; |
||
241 | return; |
||
242 | } |
||
243 | #endif |
||
244 | |||
1 | office | 245 | // Search the database for the matching string. |
49 | office | 246 | SearchDatabaseExact(dbFile, needle); |
1 | office | 247 | } |
248 | |||
11 | office | 249 | void usage(char *name) { |
250 | fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); |
||
19 | office | 251 | fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); |
11 | office | 252 | fprintf(stdout, " \n"); |
253 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); |
||
254 | fprintf(stdout, " \n"); |
||
255 | fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); |
||
256 | fprintf(stdout, " Gather tool that should be searched. \n"); |
||
257 | fprintf(stdout, " \n"); |
||
258 | fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); |
||
259 | fprintf(stdout, " \n"); |
||
260 | fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
||
261 | } |
||
262 | |||
1 | office | 263 | int main(int argc, char **argv) { |
264 | int option; |
||
2 | office | 265 | char *dbFile; |
1 | office | 266 | |
267 | // Bind handler to SIGINT. |
||
26 | office | 268 | #if !defined ___AmigaOS___ |
1 | office | 269 | signal(SIGINT, SignalHandler); |
26 | office | 270 | #endif |
1 | office | 271 | |
2 | office | 272 | dbFile = DEFAULT_DATABASE_FILE; |
273 | while((option = getopt(argc, argv, "hd:")) != -1) { |
||
1 | office | 274 | switch(option) { |
2 | office | 275 | case 'd': |
276 | dbFile = optarg; |
||
277 | break; |
||
1 | office | 278 | case 'h': |
11 | office | 279 | usage(argv[0]); |
8 | office | 280 | return 0; |
1 | office | 281 | case '?': |
11 | office | 282 | fprintf(stderr, "Invalid option %c.\n", optopt);; |
37 | office | 283 | return 5; |
1 | office | 284 | } |
285 | } |
||
286 | |||
10 | office | 287 | if(optind >= argc) { |
11 | office | 288 | usage(argv[0]); |
37 | office | 289 | return 5; |
1 | office | 290 | } |
291 | |||
33 | office | 292 | switch(GetFsType(dbFile)) { |
293 | case UNKNOWN: |
||
294 | case DIRECTORY: |
||
295 | fprintf(stderr, "'%s' is not a file.\n", dbFile); |
||
37 | office | 296 | return 10; |
33 | office | 297 | case REGULAR: |
298 | Hunt(dbFile, argv[optind]); |
||
299 | break; |
||
29 | office | 300 | } |
301 | |||
1 | office | 302 | return 0; |
303 | } |