HuntnGather – Blame information for rev 4
?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[] = |
||
4 | office | 36 | "\0$VER: Hunt 1.3.1 "__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 | */ |
||
4 | office | 66 | int compare(char *name, char *need) { |
3 | office | 67 | #if defined ___AmigaOS___ |
68 | ULONG size; |
||
69 | char *upn, *upe; |
||
70 | int success; |
||
71 | UBYTE *pattern; |
||
72 | |||
4 | office | 73 | upe = strupr(need); |
3 | office | 74 | upn = strupr(name); |
75 | |||
76 | size = strlen(upe) * 3; |
||
77 | |||
78 | if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { |
||
79 | |||
4 | office | 80 | if(ParsePatternNoCase(upe, pattern, (LONG)size) > 0) { |
3 | office | 81 | success = MatchPatternNoCase(pattern, upn); |
82 | |||
83 | FreeMem(pattern, size); |
||
84 | |||
85 | return success; |
||
86 | } |
||
4 | office | 87 | |
88 | FreeMem(pattern, size); |
||
3 | office | 89 | } |
90 | |||
4 | office | 91 | success = strstr(upn, upe) != NULL; |
92 | free(upn); |
||
93 | free(upe); |
||
94 | return success; |
||
95 | |||
3 | office | 96 | #else |
4 | office | 97 | return strstr(strupr(name), strupr(need)) != NULL; |
3 | office | 98 | #endif |
99 | } |
||
100 | |||
101 | |||
102 | /* |
||
1 | office | 103 | * |
104 | * Search the database for a matching string. |
||
105 | */ |
||
4 | office | 106 | void SearchDatabase(char *dbFile, char* need) { |
1 | office | 107 | FILE *fp; |
108 | char *name; |
||
109 | char *path; |
||
110 | char c; |
||
111 | int i; |
||
112 | int side; |
||
113 | int match; |
||
114 | int total; |
||
115 | |||
116 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
117 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | name = (char *) malloc(sizeof(char)); |
||
122 | path = (char *) malloc(sizeof(char)); |
||
123 | i = 0; |
||
124 | side = 0; |
||
125 | match = FALSE; |
||
126 | total = 0; |
||
127 | while(run && fscanf(fp, "%c", &c) == 1) { |
||
128 | #if defined ___AmigaOS___ |
||
129 | // Check if CTRL+C was pressed and abort the program. |
||
130 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
||
2 | office | 131 | run = FALSE; |
1 | office | 132 | break; |
133 | } |
||
134 | #endif |
||
135 | |||
136 | switch(c) { |
||
137 | case '\n': |
||
138 | ++total; |
||
139 | if(match) { |
||
140 | fprintf(stdout, "%s\n", path); |
||
141 | match = FALSE; |
||
142 | } |
||
143 | if(name != NULL) { |
||
144 | free(name); |
||
145 | name = (char *) malloc(sizeof(char)); |
||
146 | } |
||
147 | --side; |
||
148 | i = 0; |
||
149 | break; |
||
150 | case '\t': |
||
151 | // Case insensitive match. |
||
4 | office | 152 | if(compare(name, need)) { |
1 | office | 153 | match = TRUE; |
154 | } |
||
155 | if(path != NULL) { |
||
156 | free(path); |
||
157 | path = (char *) malloc(sizeof(char)); |
||
158 | } |
||
159 | ++side; |
||
160 | i = 0; |
||
161 | break; |
||
162 | default: |
||
163 | switch(side) { |
||
164 | case 0: |
||
165 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
||
166 | name[i] = c; |
||
167 | name[i + 1] = '\0'; |
||
168 | break; |
||
169 | case 1: |
||
170 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
||
171 | path[i] = c; |
||
172 | path[i + 1] = '\0'; |
||
173 | break; |
||
174 | default: |
||
175 | fprintf(stderr, "Database corrupted.\n"); |
||
176 | break; |
||
177 | } |
||
178 | ++i; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | fclose(fp); |
||
184 | } |
||
185 | |||
2 | office | 186 | /* |
187 | * |
||
188 | * Search the database for the matching string. |
||
189 | */ |
||
4 | office | 190 | void Hunt(char *dbFile, char *need) { |
1 | office | 191 | // Search the database for the matching string. |
4 | office | 192 | SearchDatabase(dbFile, need); |
1 | office | 193 | } |
194 | |||
195 | int main(int argc, char **argv) { |
||
196 | int option; |
||
2 | office | 197 | char *dbFile; |
198 | struct stat path; |
||
1 | office | 199 | |
200 | // Bind handler to SIGINT. |
||
201 | signal(SIGINT, SignalHandler); |
||
202 | |||
2 | office | 203 | dbFile = DEFAULT_DATABASE_FILE; |
204 | while((option = getopt(argc, argv, "hd:")) != -1) { |
||
1 | office | 205 | switch(option) { |
2 | office | 206 | case 'd': |
207 | dbFile = optarg; |
||
208 | break; |
||
1 | office | 209 | case 'h': |
3 | office | 210 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
1 | office | 211 | break; |
212 | case '?': |
||
213 | fprintf(stderr, "Invalid option %c.\n", optopt); |
||
3 | office | 214 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
1 | office | 215 | return 1; |
216 | } |
||
217 | } |
||
218 | |||
219 | if(optind > argc) { |
||
3 | office | 220 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] PATTERN\n", argv[0]); |
1 | office | 221 | return 1; |
222 | } |
||
223 | |||
2 | office | 224 | stat(dbFile, &path); |
225 | if(!S_ISREG(path.st_mode)) { |
||
226 | fprintf(stderr, "%s is not a file.\n", dbFile); |
||
227 | return 1; |
||
228 | } |
||
229 | |||
4 | office | 230 | Hunt(dbFile, argv[optind]); |
1 | office | 231 | |
232 | return 0; |
||
233 | } |