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