HuntnGather – Blame information for rev 43

Subversion Repositories:
Rev:
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  
26 office 34 #define PROGRAM_VERSION "1.7.4"
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 */
24 office 56 void SearchDatabase(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  
98 if(StringMatch(entry->name, needle)) {
99 fprintf(stdout, "%s\n", entry->path);
100 }
101  
102 free(entry->name);
103 free(entry->path);
38 office 104 free(entry);
105 entry = NULL;
106  
40 office 107 free(line->string);
37 office 108 free(line);
38 office 109 line = NULL;
1 office 110 }
111  
37 office 112 if(line != NULL) {
40 office 113 free(line->string);
37 office 114 free(line);
40 office 115 line = NULL;
37 office 116 }
9 office 117  
22 office 118 #if defined ___AsyncIO___
119 CloseAsync(fp);
120 #else
1 office 121 fclose(fp);
22 office 122 #endif
1 office 123 }
124  
2 office 125 /*
126 *
127 * Search the database for the matching string.
128 */
24 office 129 void Hunt(char *dbFile, char *needle) {
1 office 130 // Search the database for the matching string.
24 office 131 SearchDatabase(dbFile, needle);
1 office 132 }
133  
11 office 134 void usage(char *name) {
135 fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name);
19 office 136 fprintf(stdout, "Version: %s \n", PROGRAM_VERSION);
11 office 137 fprintf(stdout, " \n");
138 fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name);
139 fprintf(stdout, " \n");
140 fprintf(stdout, " -d DATABASE A path to a database generated by the \n");
141 fprintf(stdout, " Gather tool that should be searched. \n");
142 fprintf(stdout, " \n");
143 fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n");
144 fprintf(stdout, " \n");
145 fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n");
146 }
147  
1 office 148 int main(int argc, char **argv) {
149 int option;
2 office 150 char *dbFile;
1 office 151  
152 // Bind handler to SIGINT.
26 office 153 #if !defined ___AmigaOS___
1 office 154 signal(SIGINT, SignalHandler);
26 office 155 #endif
1 office 156  
2 office 157 dbFile = DEFAULT_DATABASE_FILE;
158 while((option = getopt(argc, argv, "hd:")) != -1) {
1 office 159 switch(option) {
2 office 160 case 'd':
161 dbFile = optarg;
162 break;
1 office 163 case 'h':
11 office 164 usage(argv[0]);
8 office 165 return 0;
1 office 166 case '?':
11 office 167 fprintf(stderr, "Invalid option %c.\n", optopt);;
37 office 168 return 5;
1 office 169 }
170 }
171  
10 office 172 if(optind >= argc) {
11 office 173 usage(argv[0]);
37 office 174 return 5;
1 office 175 }
176  
33 office 177 switch(GetFsType(dbFile)) {
178 case UNKNOWN:
179 case DIRECTORY:
180 fprintf(stderr, "'%s' is not a file.\n", dbFile);
37 office 181 return 10;
33 office 182 case REGULAR:
183 Hunt(dbFile, argv[optind]);
184 break;
29 office 185 }
186  
1 office 187 return 0;
188 }