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