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