HuntnGather – Blame information for rev 38

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