HuntnGather – Blame information for rev 9
?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> |
||
2 | office | 8 | #include <dirent.h> |
1 | office | 9 | #include <signal.h> |
10 | #include <ctype.h> |
||
11 | |||
2 | office | 12 | #include <sys/types.h> |
13 | #include <sys/stat.h> |
||
14 | |||
1 | office | 15 | #include <proto/dos.h> |
16 | #include <proto/exec.h> |
||
17 | |||
5 | office | 18 | #if !defined ___HAVE_GETOPT___ |
1 | office | 19 | #include "getopt.h" |
20 | #endif |
||
21 | |||
5 | office | 22 | #if defined ___AmigaOS___ |
23 | /*************************************************************************/ |
||
24 | /* Version string used for querrying the program version. */ |
||
25 | /*************************************************************************/ |
||
26 | TEXT version_string[] = |
||
9 | office | 27 | "\0$VER: Hunt 1.6 "__DATE__" by Wizardry and Steamworks"; |
5 | office | 28 | #endif |
29 | |||
1 | office | 30 | #if !defined TRUE |
31 | #define TRUE 1; |
||
32 | #endif |
||
33 | |||
34 | #if !defined FALSE |
||
35 | #define FALSE 0; |
||
36 | #endif |
||
37 | |||
2 | office | 38 | #define DEFAULT_DATABASE_FILE "S:gather.db" |
39 | |||
1 | office | 40 | int run = TRUE; |
41 | |||
42 | void SignalHandler(int sig) { |
||
43 | // Toggle the run flag to stop execution. |
||
44 | run = FALSE; |
||
45 | } |
||
46 | |||
47 | /* |
||
48 | * Convert string to uppercase. |
||
49 | */ |
||
50 | char *strupr(char *str) { |
||
51 | char *up; |
||
52 | int i; |
||
53 | |||
54 | up = (char *) malloc((strlen(str) + 1) * sizeof(char)); |
||
55 | sprintf(up, "%s", str); |
||
56 | |||
57 | i = strlen(up); |
||
58 | while(--i > -1) { |
||
59 | up[i] = toupper(up[i]); |
||
60 | } |
||
61 | |||
62 | return up; |
||
63 | } |
||
64 | |||
65 | /* |
||
3 | office | 66 | * Compare "name" and "needle" for equality. |
67 | */ |
||
4 | office | 68 | int compare(char *name, char *need) { |
3 | office | 69 | #if defined ___AmigaOS___ |
70 | ULONG size; |
||
71 | char *upn, *upe; |
||
72 | int success; |
||
73 | UBYTE *pattern; |
||
74 | |||
4 | office | 75 | upe = strupr(need); |
3 | office | 76 | upn = strupr(name); |
77 | |||
78 | size = strlen(upe) * 3; |
||
79 | |||
9 | office | 80 | success = FALSE; |
81 | |||
3 | office | 82 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
83 | |||
4 | office | 84 | if(ParsePatternNoCase(upe, pattern, (LONG)size) > 0) { |
3 | office | 85 | success = MatchPatternNoCase(pattern, upn); |
86 | |||
9 | office | 87 | FreeVec(pattern); |
88 | free(upe); |
||
89 | free(upn); |
||
3 | office | 90 | |
91 | return success; |
||
92 | } |
||
4 | office | 93 | |
9 | office | 94 | FreeVec(pattern); |
3 | office | 95 | } |
96 | |||
9 | office | 97 | free(upe); |
4 | office | 98 | free(upn); |
99 | return success; |
||
100 | |||
3 | office | 101 | #else |
9 | office | 102 | char *upn, *upe; |
103 | int success = FALSE; |
||
104 | |||
105 | upe = strupr(need); |
||
106 | upn = strupr(name); |
||
107 | |||
108 | success = strstr(strupr(name), strupr(need)) != NULL; |
||
109 | |||
110 | free(upe); |
||
111 | free(upn); |
||
112 | return success; |
||
3 | office | 113 | #endif |
114 | } |
||
115 | |||
116 | |||
117 | /* |
||
1 | office | 118 | * |
119 | * Search the database for a matching string. |
||
120 | */ |
||
4 | office | 121 | void SearchDatabase(char *dbFile, char* need) { |
1 | office | 122 | FILE *fp; |
123 | char *name; |
||
124 | char *path; |
||
125 | char c; |
||
126 | int i; |
||
127 | int side; |
||
128 | int match; |
||
129 | int total; |
||
130 | |||
131 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
132 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | name = (char *) malloc(sizeof(char)); |
||
137 | path = (char *) malloc(sizeof(char)); |
||
9 | office | 138 | |
1 | office | 139 | i = 0; |
140 | side = 0; |
||
141 | match = FALSE; |
||
142 | total = 0; |
||
9 | office | 143 | |
1 | office | 144 | while(run && fscanf(fp, "%c", &c) == 1) { |
145 | #if defined ___AmigaOS___ |
||
146 | // Check if CTRL+C was pressed and abort the program. |
||
147 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
||
2 | office | 148 | run = FALSE; |
1 | office | 149 | break; |
150 | } |
||
151 | #endif |
||
152 | |||
153 | switch(c) { |
||
154 | case '\n': |
||
155 | ++total; |
||
156 | if(match) { |
||
157 | fprintf(stdout, "%s\n", path); |
||
158 | match = FALSE; |
||
159 | } |
||
160 | if(name != NULL) { |
||
161 | free(name); |
||
162 | name = (char *) malloc(sizeof(char)); |
||
163 | } |
||
164 | --side; |
||
165 | i = 0; |
||
166 | break; |
||
167 | case '\t': |
||
168 | // Case insensitive match. |
||
4 | office | 169 | if(compare(name, need)) { |
1 | office | 170 | match = TRUE; |
171 | } |
||
172 | if(path != NULL) { |
||
173 | free(path); |
||
174 | path = (char *) malloc(sizeof(char)); |
||
175 | } |
||
176 | ++side; |
||
177 | i = 0; |
||
178 | break; |
||
179 | default: |
||
180 | switch(side) { |
||
181 | case 0: |
||
182 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
||
183 | name[i] = c; |
||
184 | name[i + 1] = '\0'; |
||
185 | break; |
||
186 | case 1: |
||
187 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
||
188 | path[i] = c; |
||
189 | path[i + 1] = '\0'; |
||
190 | break; |
||
191 | default: |
||
192 | fprintf(stderr, "Database corrupted.\n"); |
||
193 | break; |
||
194 | } |
||
195 | ++i; |
||
196 | break; |
||
197 | } |
||
198 | } |
||
199 | |||
9 | office | 200 | free(name); |
201 | free(path); |
||
202 | |||
1 | office | 203 | fclose(fp); |
204 | } |
||
205 | |||
2 | office | 206 | /* |
207 | * |
||
208 | * Search the database for the matching string. |
||
209 | */ |
||
4 | office | 210 | void Hunt(char *dbFile, char *need) { |
1 | office | 211 | // Search the database for the matching string. |
4 | office | 212 | SearchDatabase(dbFile, need); |
1 | office | 213 | } |
214 | |||
215 | int main(int argc, char **argv) { |
||
216 | int option; |
||
2 | office | 217 | char *dbFile; |
218 | struct stat path; |
||
1 | office | 219 | |
220 | // Bind handler to SIGINT. |
||
221 | signal(SIGINT, SignalHandler); |
||
222 | |||
2 | office | 223 | dbFile = DEFAULT_DATABASE_FILE; |
224 | while((option = getopt(argc, argv, "hd:")) != -1) { |
||
1 | office | 225 | switch(option) { |
2 | office | 226 | case 'd': |
227 | dbFile = optarg; |
||
228 | break; |
||
1 | office | 229 | case 'h': |
3 | office | 230 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
8 | office | 231 | return 0; |
1 | office | 232 | case '?': |
233 | fprintf(stderr, "Invalid option %c.\n", optopt); |
||
3 | office | 234 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
1 | office | 235 | return 1; |
236 | } |
||
237 | } |
||
238 | |||
239 | if(optind > argc) { |
||
3 | office | 240 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
1 | office | 241 | return 1; |
242 | } |
||
243 | |||
2 | office | 244 | stat(dbFile, &path); |
245 | if(!S_ISREG(path.st_mode)) { |
||
5 | office | 246 | fprintf(stderr, "Database file '%s' is not a file.\n", dbFile); |
2 | office | 247 | return 1; |
248 | } |
||
249 | |||
4 | office | 250 | Hunt(dbFile, argv[optind]); |
1 | office | 251 | |
252 | return 0; |
||
253 | } |