HuntnGather – Diff between revs 38 and 40

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 38 Rev 40
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // 2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT //
3 /////////////////////////////////////////////////////////////////////////// 3 ///////////////////////////////////////////////////////////////////////////
4   4  
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 #include <ctype.h> 8 #include <ctype.h>
9 #if !defined ___AmigaOS___ 9 #if !defined ___AmigaOS___
10 #include <signal.h> 10 #include <signal.h>
11 #include <dirent.h> 11 #include <dirent.h>
12 #include <sys/stat.h> 12 #include <sys/stat.h>
13 #endif 13 #endif
14   14  
15 #include <sys/types.h> 15 #include <sys/types.h>
16   16  
17 #if defined ___AmigaOS___ 17 #if defined ___AmigaOS___
18   18  
19 #include <proto/dos.h> 19 #include <proto/dos.h>
20 #include <proto/exec.h> 20 #include <proto/exec.h>
21 #include <proto/utility.h> 21 #include <proto/utility.h>
22 #endif 22 #endif
23   23  
24 #if defined ___AsyncIO___ 24 #if defined ___AsyncIO___
25 #include <asyncio.h> 25 #include <asyncio.h>
26 #endif 26 #endif
27   27  
28 #if !defined ___HAVE_GETOPT___ 28 #if !defined ___HAVE_GETOPT___
29 #include "/shared/getopt.h" 29 #include "/shared/getopt.h"
30 #endif 30 #endif
31   31  
32 #include "/shared/utilities.h" 32 #include "/shared/utilities.h"
33   33  
34 #define PROGRAM_VERSION "1.7.4" 34 #define PROGRAM_VERSION "1.7.4"
35   35  
36 #if defined ___AmigaOS___ 36 #if defined ___AmigaOS___
37 /*************************************************************************/ 37 /*************************************************************************/
38 /* Version string used for querrying the program version. */ 38 /* Version string used for querrying the program version. */
39 /*************************************************************************/ 39 /*************************************************************************/
40 TEXT version_string[] = 40 TEXT version_string[] =
41 "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks"; 41 "\0$VER: Hunt " PROGRAM_VERSION " "__DATE__" by Wizardry and Steamworks";
42 #endif 42 #endif
43   43  
44 int PROGRAM_RUN = TRUE; 44 int PROGRAM_RUN = TRUE;
45 int PROGRAM_VERBOSE = FALSE; 45 int PROGRAM_VERBOSE = FALSE;
46   46  
47 void SignalHandler(int sig) { 47 void SignalHandler(int sig) {
48 // Toggle the run flag to stop execution. 48 // Toggle the run flag to stop execution.
49 PROGRAM_RUN = FALSE; 49 PROGRAM_RUN = FALSE;
50 } 50 }
51   51  
52 /* 52 /*
53 * 53 *
54 * Search the database for a matching string. 54 * Search the database for a matching string.
55 */ 55 */
56 void SearchDatabase(char *dbFile, char* needle) { 56 void SearchDatabase(char *dbFile, char* needle) {
57 #if defined ___AsyncIO___ 57 #if defined ___AsyncIO___
58 struct AsyncFile *fp; 58 struct AsyncFile *fp;
59 #else 59 #else
60 FILE *fp; 60 FILE *fp;
61 #endif 61 #endif
62 dbEntry *entry; 62 dbEntry *entry;
63 dbLine *line = NULL; 63 dbLine *line = NULL;
64   64  
65 // Open database file for reading. 65 // Open database file for reading.
66 #if defined ___AsyncIO___ 66 #if defined ___AsyncIO___
67 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 67 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
68 #else 68 #else
69 if((fp = fopen(dbFile, "r")) == NULL) { 69 if((fp = fopen(dbFile, "r")) == NULL) {
70 #endif 70 #endif
71 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); 71 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
72 return; 72 return;
73 } 73 }
74   74  
75 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) { 75 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
76 #if defined ___AmigaOS___ 76 #if defined ___AmigaOS___
77 // Check if CTRL+C was pressed and abort the program. 77 // Check if CTRL+C was pressed and abort the program.
78 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 78 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
79 PROGRAM_RUN = FALSE; 79 PROGRAM_RUN = FALSE;
80 continue; 80 continue;
81 } 81 }
82 #endif 82 #endif
83   83  
84 if((entry = CreateDatabaseEntry(line)) == NULL) { 84 if((entry = CreateDatabaseEntry(line)) == NULL) {
85 fprintf(stderr, "Unable to create database entry.\n"); 85 fprintf(stderr, "Unable to create database entry.\n");
86 free(line->content); 86 free(line->string);
87 free(line); 87 free(line);
-   88 line = NULL;
88 #if defined ___AsyncIO___ 89 #if defined ___AsyncIO___
89 CloseAsync(fp); 90 CloseAsync(fp);
90 #else 91 #else
91 fclose(fp); 92 fclose(fp);
92 #endif 93 #endif
93 return; 94 return;
94 } 95 }
95   96  
96 if(StringMatch(entry->name, needle)) { 97 if(StringMatch(entry->name, needle)) {
97 fprintf(stdout, "%s\n", entry->path); 98 fprintf(stdout, "%s\n", entry->path);
98 } 99 }
99   100  
100 free(entry->name); 101 free(entry->name);
101 free(entry->path); 102 free(entry->path);
102 free(entry); 103 free(entry);
103 entry = NULL; 104 entry = NULL;
104   105  
105 free(line->content); 106 free(line->string);
106 free(line); 107 free(line);
107 line = NULL; 108 line = NULL;
108 } 109 }
109   110  
110 if(line != NULL) { 111 if(line != NULL) {
111 free(line->content); 112 free(line->string);
112 free(line); 113 free(line);
-   114 line = NULL;
113 } 115 }
114   116  
115 #if defined ___AsyncIO___ 117 #if defined ___AsyncIO___
116 CloseAsync(fp); 118 CloseAsync(fp);
117 #else 119 #else
118 fclose(fp); 120 fclose(fp);
119 #endif 121 #endif
120 } 122 }
121   123  
122 /* 124 /*
123 * 125 *
124 * Search the database for the matching string. 126 * Search the database for the matching string.
125 */ 127 */
126 void Hunt(char *dbFile, char *needle) { 128 void Hunt(char *dbFile, char *needle) {
127 // Search the database for the matching string. 129 // Search the database for the matching string.
128 SearchDatabase(dbFile, needle); 130 SearchDatabase(dbFile, needle);
129 } 131 }
130   132  
131 void usage(char *name) { 133 void usage(char *name) {
132 fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name); 134 fprintf(stdout, "Hunt & Gather - %s, a file index search tool. \n", name);
133 fprintf(stdout, "Version: %s \n", PROGRAM_VERSION); 135 fprintf(stdout, "Version: %s \n", PROGRAM_VERSION);
134 fprintf(stdout, " \n"); 136 fprintf(stdout, " \n");
135 fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name); 137 fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN \n", name);
136 fprintf(stdout, " \n"); 138 fprintf(stdout, " \n");
137 fprintf(stdout, " -d DATABASE A path to a database generated by the \n"); 139 fprintf(stdout, " -d DATABASE A path to a database generated by the \n");
138 fprintf(stdout, " Gather tool that should be searched. \n"); 140 fprintf(stdout, " Gather tool that should be searched. \n");
139 fprintf(stdout, " \n"); 141 fprintf(stdout, " \n");
140 fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n"); 142 fprintf(stdout, "PATTERN is an AmigaOS DOS pattern to match file names. \n");
141 fprintf(stdout, " \n"); 143 fprintf(stdout, " \n");
142 fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); 144 fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n");
143 } 145 }
144   146  
145 int main(int argc, char **argv) { 147 int main(int argc, char **argv) {
146 int option; 148 int option;
147 char *dbFile; 149 char *dbFile;
148   150  
149 // Bind handler to SIGINT. 151 // Bind handler to SIGINT.
150 #if !defined ___AmigaOS___ 152 #if !defined ___AmigaOS___
151 signal(SIGINT, SignalHandler); 153 signal(SIGINT, SignalHandler);
152 #endif 154 #endif
153   155  
154 dbFile = DEFAULT_DATABASE_FILE; 156 dbFile = DEFAULT_DATABASE_FILE;
155 while((option = getopt(argc, argv, "hd:")) != -1) { 157 while((option = getopt(argc, argv, "hd:")) != -1) {
156 switch(option) { 158 switch(option) {
157 case 'd': 159 case 'd':
158 dbFile = optarg; 160 dbFile = optarg;
159 break; 161 break;
160 case 'h': 162 case 'h':
161 usage(argv[0]); 163 usage(argv[0]);
162 return 0; 164 return 0;
163 case '?': 165 case '?':
164 fprintf(stderr, "Invalid option %c.\n", optopt);; 166 fprintf(stderr, "Invalid option %c.\n", optopt);;
165 return 5; 167 return 5;
166 } 168 }
167 } 169 }
168   170  
169 if(optind >= argc) { 171 if(optind >= argc) {
170 usage(argv[0]); 172 usage(argv[0]);
171 return 5; 173 return 5;
172 } 174 }
173   175  
174 switch(GetFsType(dbFile)) { 176 switch(GetFsType(dbFile)) {
175 case UNKNOWN: 177 case UNKNOWN:
176 case DIRECTORY: 178 case DIRECTORY:
177 fprintf(stderr, "'%s' is not a file.\n", dbFile); 179 fprintf(stderr, "'%s' is not a file.\n", dbFile);
178 return 10; 180 return 10;
179 case REGULAR: 181 case REGULAR:
180 Hunt(dbFile, argv[optind]); 182 Hunt(dbFile, argv[optind]);
181 break; 183 break;
182 } 184 }
183   185  
184 return 0; 186 return 0;
185 } 187 }
186   188