-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
297 lines (255 loc) · 8.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <getopt.h>
#include "3rdparty/uthash/src/utlist.h"
#include "3rdparty/uthash/src/uthash.h"
#define TEST_METHOD_STRCMP 1
#define TEST_METHOD_UTHASH 2
#define MIN(a,b) (((a)<(b))?(a):(b))
struct strlist {
char *word;
struct strlist *next;
};
struct strhash {
char *word;
UT_hash_handle hh;
};
struct options {
char *wordlist_file;
int test_method;
};
static struct options options = {
.wordlist_file = NULL,
.test_method = TEST_METHOD_STRCMP
};
static struct option longopts[] = {
{ "wordlist-file", required_argument, NULL, 'f' },
{ "test-method", required_argument, NULL, 't' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
static struct strlist *searchlist = NULL;
static struct strlist *strlist = NULL;
static struct strhash *strhash = NULL;
static const char * test_method_str (int method)
{
switch(method) {
case TEST_METHOD_STRCMP:
return "strcmp";
case TEST_METHOD_UTHASH:
return "uthash";
default:
return "unknown";
}
}
static void display_usage (const char *program)
{
fprintf(stdout, "usage: %s [options]\n", program);
fprintf(stdout, " -f, --wordlist-file : read word lists from file (default: NULL)\n");
fprintf(stdout, " -t, --test-method : choose method to compare (default: %s, options: strcmp, uthash)\n", test_method_str(options.test_method));
fprintf(stdout, " -h. --help : this text\n");
}
static int parse_cmdline (int argc, char *argv[])
{
int c;
const char *test_method = NULL;
while ((c = getopt_long(argc, argv, "f:t:h", longopts, NULL)) != -1) {
switch (c) {
case 'f':
options.wordlist_file = optarg;
break;
case 't':
test_method = optarg;
break;
case 'h':
display_usage(argv[0]);
exit(1);
default:
fprintf(stderr, "unknown option: %d\n", optopt);
return -1;
}
}
if (test_method) {
if (strcmp(test_method, "strcmp") == 0) {
options.test_method = TEST_METHOD_STRCMP;
} else if (strcmp(test_method, "uthash") == 0) {
options.test_method = TEST_METHOD_UTHASH;
} else {
fprintf(stderr, "Unknown test method: %s\n", test_method);
return -1;
}
}
if (options.wordlist_file == NULL) {
fprintf(stderr, "wordlist file must be given\n");
return -1;
}
return 0;
}
static int add_to_search_list (char *token)
{
struct strlist *sl;
if (token == NULL) {
return -1;
}
sl = calloc(1, sizeof(struct strlist));
sl->word = strdup(token);
LL_PREPEND(searchlist, sl);
return 0;
}
static int process_input_strcmp (char *token)
{
struct strlist *sl;
if (token == NULL) {
return -1;
}
sl = calloc(1, sizeof(struct strlist));
sl->word = strdup(token);
LL_PREPEND(strlist, sl);
add_to_search_list(token);
return 0;
}
static int process_input_uthash (char *token)
{
struct strhash *sh;
if (token == NULL) {
return -1;
}
HASH_FIND_STR(strhash, token, sh);
if (sh != NULL) {
/* already in hash */
return 0;
}
sh = calloc(1, sizeof(struct strhash));
sh->word = strdup(token);
HASH_ADD_KEYPTR(hh, strhash, sh->word, strlen(sh->word), sh);
add_to_search_list(token);
return 0;
}
static int process_input (char *token)
{
switch (options.test_method) {
case TEST_METHOD_STRCMP:
return process_input_strcmp(token);
case TEST_METHOD_UTHASH:
return process_input_uthash(token);
default:
return -1;
}
}
static char * search (char *token)
{
struct strlist *sl;
struct strhash *sh;
char *token_found = NULL;
switch(options.test_method) {
case TEST_METHOD_STRCMP:
LL_FOREACH(strlist, sl) {
if (strcmp(sl->word, token) == 0) {
token_found = sl->word;
break;
}
}
break;
case TEST_METHOD_UTHASH:
HASH_FIND_STR(strhash, token, sh);
if (sh) {
token_found = sh->word;
}
break;
default:
break;
}
return token_found;
}
struct timespec timespec_diff (struct timespec before, struct timespec after)
{
struct timespec res;
if (after.tv_sec >= before.tv_sec) {
if ((after.tv_nsec - before.tv_nsec) < 0) {
res.tv_sec = after.tv_sec - before.tv_sec - 1;
res.tv_nsec = 1000000000 + after.tv_nsec - before.tv_nsec;
} else {
res.tv_sec = after.tv_sec - before.tv_sec;
res.tv_nsec = after.tv_nsec - before.tv_nsec;
}
} else {
if ((before.tv_nsec - after.tv_nsec) < 0) {
res.tv_sec = before.tv_sec - after.tv_sec - 1;
res.tv_nsec = 1000000000 + before.tv_nsec - after.tv_nsec;
} else {
res.tv_sec = before.tv_sec - after.tv_sec;
res.tv_nsec = before.tv_nsec - after.tv_nsec;
}
}
return res;
}
int main (int argc, char **argv)
{
FILE *fp;
char buffer[16*1024];
int offset;
size_t read;
char *p;
char *token;
struct timespec before;
struct timespec after;
struct timespec diff;
struct strlist *sl;
int words_total = 0;
int words_completed = 0;
int percent_step;
if (parse_cmdline(argc, argv) < 0) {
display_usage(argv[0]);
return 1;
}
if ((fp = fopen(options.wordlist_file, "r")) == NULL) {
fprintf(stderr, "couldn't open file: %s [%s]\n", options.wordlist_file, strerror(errno));
return 1;
}
offset = 0;
read = 0;
while (!feof(fp)) {
size_t rc = fread(buffer + offset, sizeof(char), sizeof(buffer) - offset - 1, fp);
p = buffer;
while ((token = strsep(&p, "\n"))) {
if (process_input(token) == 0) {
words_total++;
}
if (strstr(p, "\n") == NULL) {
offset = strlen(p);
sprintf(buffer, "%s", p);
break;
}
}
if (rc > 0) {
read += rc;
} else if (rc == 0) {
break;
} else {
fprintf(stderr, "couldn't read from file: %s\n", strerror(errno));
return 1;
}
}
fclose(fp);
percent_step = words_total / 20;
clock_gettime(CLOCK_MONOTONIC, &before);
LL_FOREACH(searchlist, sl) {
char *token = search(sl->word);
if (token == NULL) {
fprintf(stderr, "this shouldn't happen, %s not found in list\n", sl->word);
return 1;
}
if (++words_completed % percent_step == 0) {
fprintf(stdout, "\rCompleted: %%%.0f", 100.0 * words_completed / words_total);
fflush(stdout);
}
}
clock_gettime(CLOCK_MONOTONIC, &after);
diff = timespec_diff(before, after);
fprintf(stdout, "\nTotal Time: %li.%09li seconds\n", diff.tv_sec, diff.tv_nsec);
return 0;
}