Browse code

initial commit

John Hawthorn authored on 11/07/2014 08:36:31
Showing 2 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+LIBS=
2
+CFLAGS+=-Wall -g
3
+TARGET=hawthfuzz
4
+OBJECTS=hawthfuzz.o
5
+
6
+$(TARGET): $(OBJECTS)
7
+	$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
8
+
9
+clean:
10
+	$(RM) $(TARGET) *.o
0 11
new file mode 100644
... ...
@@ -0,0 +1,66 @@
1
+#include <stdio.h>
2
+#include <string.h>
3
+#include <stdlib.h>
4
+
5
+double match(const char *needle, const char *haystack){
6
+	while(*needle){
7
+		while(*needle == *haystack++)
8
+			needle++;
9
+		if(!*haystack)
10
+			return 1;
11
+	}
12
+	return 0;
13
+}
14
+
15
+#define INITIAL_CAPACITY 1
16
+int choices_capacity = 0;
17
+int choices_n = 0;
18
+const char **choices = NULL;
19
+
20
+void resize_choices(int new_capacity){
21
+	choices = realloc(choices, new_capacity * sizeof(const char *));
22
+	int i = choices_capacity;
23
+	for(; i < new_capacity; i++){
24
+		choices[i] = NULL;
25
+	}
26
+	choices_capacity = new_capacity;
27
+}
28
+
29
+void add_choice(const char *choice){
30
+	if(choices_n == choices_capacity){
31
+		resize_choices(choices_capacity * 2);
32
+	}
33
+	choices[choices_n++] = choice;
34
+}
35
+
36
+void read_choices(){
37
+	char *line = NULL;
38
+	size_t len = 0;
39
+	ssize_t read;
40
+
41
+	while ((read = getline(&line, &len, stdin)) != -1) {
42
+		char *nl;
43
+		if((nl = strchr(line, '\n')))
44
+			*nl = '\0';
45
+
46
+		add_choice(line);
47
+
48
+		line = NULL;
49
+	}
50
+	free(line);
51
+}
52
+
53
+void run_search(char *needle){
54
+	int i;
55
+	for(i = 0; i < choices_n; i++){
56
+		double rank = match(needle, choices[i]);
57
+		printf("'%s' =~ '%s'\t%f\n", needle, choices[i], rank);
58
+	}
59
+}
60
+
61
+int main(int argc, char *argv[]){
62
+	resize_choices(INITIAL_CAPACITY);
63
+	read_choices();
64
+	run_search(argv[1]);
65
+	return 0;
66
+}