HuntnGather – Blame information for rev 40

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