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