This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
101 lines (83 loc) · 2.09 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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include "matcher.h"
struct globalArgs_t {
int dotfiles; // -d
int limit; // -l
char *manifest; // -m
char *search;
} globalArgs;
static const char *optString = "dl:m:h?";
static const struct option longOpts[] = {
{ "no-dotfiles", no_argument, NULL, 'd' },
{ "limit", required_argument, NULL, 'l' },
{ "manifest", required_argument, NULL, 'm' },
{ "help", no_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};
/* Display program usage, and exit. */
void display_usage(void)
{
puts("Usage: matcher [--no-dotfiles] [--limit num] [--manifest filename] <query>\n");
exit( EXIT_FAILURE );
}
void parse_arguments(int argc, char *argv[])
{
int opt = 0;
int longIndex = 0;
globalArgs.dotfiles = 1;
globalArgs.limit = 10;
globalArgs.manifest = NULL;
globalArgs.search = NULL;
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
while (opt != -1) {
switch (opt) {
case 'l':
globalArgs.limit = atoi(optarg);
break;
case 'd':
globalArgs.dotfiles = 0;
break;
case 'm':
globalArgs.manifest = optarg;
break;
case 'h':
case '?':
display_usage();
break;
default:
break;
}
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
}
globalArgs.search = (optind < argc) ? argv[optind] : "";
int i = 0;
while(globalArgs.search[i] != '\0'){
globalArgs.search[i] = tolower(globalArgs.search[i]);
i++;
}
}
int main(int argc, char *argv[])
{
parse_arguments(argc, argv);
char *strings[20000];
int num_strings = 0;
FILE *fp = stdin;
if (globalArgs.manifest) {
fp = fopen(globalArgs.manifest, "r");
}
for (num_strings = 0; num_strings < 20000; num_strings++) {
strings[num_strings] = malloc(1024 * sizeof(char));
fgets(strings[num_strings], 1023, fp);
if (feof(fp)) break;
}
fclose(fp);
score_list(globalArgs.search,
strings,
num_strings,
globalArgs.dotfiles,
globalArgs.limit);
return 0;
}