HuntnGather – Blame information for rev 2
?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 | /* |
||
64 | * |
||
65 | * Search the database for a matching string. |
||
66 | */ |
||
2 | office | 67 | void SearchDatabase(char *dbFile, char* string) { |
1 | office | 68 | FILE *fp; |
69 | char *name; |
||
70 | char *path; |
||
71 | char c; |
||
72 | int i; |
||
73 | int side; |
||
74 | int match; |
||
75 | int total; |
||
76 | |||
77 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
78 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | name = (char *) malloc(sizeof(char)); |
||
83 | path = (char *) malloc(sizeof(char)); |
||
84 | i = 0; |
||
85 | side = 0; |
||
86 | match = FALSE; |
||
87 | total = 0; |
||
88 | while(run && fscanf(fp, "%c", &c) == 1) { |
||
89 | #if defined ___AmigaOS___ |
||
90 | // Check if CTRL+C was pressed and abort the program. |
||
91 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
||
2 | office | 92 | run = FALSE; |
1 | office | 93 | break; |
94 | } |
||
95 | #endif |
||
96 | |||
97 | switch(c) { |
||
98 | case '\n': |
||
99 | ++total; |
||
100 | if(match) { |
||
101 | fprintf(stdout, "%s\n", path); |
||
102 | match = FALSE; |
||
103 | } |
||
104 | if(name != NULL) { |
||
105 | free(name); |
||
106 | name = (char *) malloc(sizeof(char)); |
||
107 | } |
||
108 | --side; |
||
109 | i = 0; |
||
110 | break; |
||
111 | case '\t': |
||
112 | // Case insensitive match. |
||
113 | if(strstr(strupr(name), strupr(string)) != NULL) { |
||
114 | match = TRUE; |
||
115 | } |
||
116 | if(path != NULL) { |
||
117 | free(path); |
||
118 | path = (char *) malloc(sizeof(char)); |
||
119 | } |
||
120 | ++side; |
||
121 | i = 0; |
||
122 | break; |
||
123 | default: |
||
124 | switch(side) { |
||
125 | case 0: |
||
126 | name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
||
127 | name[i] = c; |
||
128 | name[i + 1] = '\0'; |
||
129 | break; |
||
130 | case 1: |
||
131 | path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
||
132 | path[i] = c; |
||
133 | path[i + 1] = '\0'; |
||
134 | break; |
||
135 | default: |
||
136 | fprintf(stderr, "Database corrupted.\n"); |
||
137 | break; |
||
138 | } |
||
139 | ++i; |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | fclose(fp); |
||
145 | } |
||
146 | |||
2 | office | 147 | /* |
148 | * |
||
149 | * Search the database for the matching string. |
||
150 | */ |
||
1 | office | 151 | void Hunt(char *dbFile, char *needle) { |
152 | // Search the database for the matching string. |
||
2 | office | 153 | SearchDatabase(dbFile, needle); |
1 | office | 154 | } |
155 | |||
156 | int main(int argc, char **argv) { |
||
157 | int option; |
||
2 | office | 158 | char *dbFile; |
159 | struct stat path; |
||
1 | office | 160 | |
161 | // Bind handler to SIGINT. |
||
162 | signal(SIGINT, SignalHandler); |
||
163 | |||
2 | office | 164 | dbFile = DEFAULT_DATABASE_FILE; |
165 | while((option = getopt(argc, argv, "hd:")) != -1) { |
||
1 | office | 166 | switch(option) { |
2 | office | 167 | case 'd': |
168 | dbFile = optarg; |
||
169 | break; |
||
1 | office | 170 | case 'h': |
2 | office | 171 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
1 | office | 172 | break; |
173 | case '?': |
||
174 | fprintf(stderr, "Invalid option %c.\n", optopt); |
||
2 | office | 175 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
1 | office | 176 | return 1; |
177 | } |
||
178 | } |
||
179 | |||
180 | if(optind > argc) { |
||
2 | office | 181 | fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]); |
1 | office | 182 | return 1; |
183 | } |
||
184 | |||
2 | office | 185 | stat(dbFile, &path); |
186 | if(!S_ISREG(path.st_mode)) { |
||
187 | fprintf(stderr, "%s is not a file.\n", dbFile); |
||
188 | return 1; |
||
189 | } |
||
190 | |||
1 | office | 191 | Hunt("S:gather.db", argv[1]); |
192 | |||
193 | return 0; |
||
194 | } |