-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordscapes.c
183 lines (151 loc) · 3 KB
/
wordscapes.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
/* benchmark
* letters b94908b 239bba2 -O2
* 5 1.2s
* 6 8.2s
* 7 58s
* 8 7m 55s
* 9 ~ 1h 4m 0.195s
* 10 ~ 8h 32m 1.56s 0.7s
* 11 ~ 68h 16m 17.4s 7.6s
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define YES 1
#define NO 0
#define MAX_WORD_LEN 35 /*this could safely be 10, IFF you ensure you discard words >10 chars properly */
// TODO: alert if input or template > MAX_WORD_LEN
#define DICT "/usr/share/dict/cracklib-small"
typedef struct tree {
struct tree *p[26]; // a-z
int last;
} Tree;
void add(Tree *root, char *word);
int is_in_tree(Tree *root, char *word);
Tree *new_Tree();
void usage();
void die(char *e);
void check(char *str);
void perm(char *pre, char *str);
void parse_dict();
char *argv0;
char *template = NULL;
Tree *dict, *found;
void add(Tree *root, char *word)
{
char *p = word;
Tree *t = root;
int index;
for( ; *p != '\0'; p++, t = t->p[index]) {
index = *p-'a';
if(index < 0 || index > 25)
return; // unsupported character
if(t->p[index] == NULL)
t->p[index] = new_Tree();
}
t->last = YES;
}
int is_in_tree(Tree *root, char *word)
{
int index;
char *p = word;
Tree *t = root;
for( ; *p != '\0'; p++, t = t->p[index]) {
index = *p - 'a';
if(t->p[index] == NULL)
return NO;
}
return t->last == YES ? YES : NO;
}
Tree *new_Tree()
{
Tree *ret = malloc(sizeof(Tree));
if(ret == NULL)
die("Couldn't allocate new_Tree");
memset(ret,0,sizeof(*ret));
// ret->last=NO;
return ret;
}
void usage()
{
printf("Usage: %s word [template]\n\n"
"Examples:\n"
"%s pin\n"
"pin\n"
"nip\n\n"
"%s pin n__\n"
"nip\n", argv0, argv0, argv0);
}
void die(char *e)
{
fputs(e, stderr);
exit(1);
}
void check(char *str)
{
if(is_in_tree(dict,str)==YES && is_in_tree(found,str)==NO) {
puts(str);
add(found,str);
}
}
void perm(char *pre, char *str)
{
if(template != NULL) {
if(strlen(pre)>strlen(template))
return;
for(int i = 0; i < strlen(pre); i++) {
if(template[i]=='_')
continue;
if(template[i]!=pre[i])
return;
}
}
int x=strlen(pre);
if(x>2) {
if(template == NULL || strlen(template) == strlen(pre))
check(pre);
}
for(int i = 0; i < strlen(str); i++) {
if(str[i]=='_')
continue;
pre[x]=str[i];
pre[x+1]='\0';
str[i]='_';
perm(pre,str);
str[i]=pre[x];
}
}
void parse_dict()
{
FILE *dictfile;
dict = new_Tree();
dictfile = fopen(DICT, "r");
if(!dictfile)
die("Couldn't open "DICT);
// FIXME can read char-by-char and add to dict directly
char buf[MAX_WORD_LEN];
while(fgets(buf, sizeof(buf), dictfile))
{
if(buf[strlen(buf)-1]=='\n')
buf[strlen(buf)-1]='\0';
add(dict, buf);
}
fclose(dictfile);
}
int main(int argc, char *argv[])
{
argv0=*argv;
if(argc != 2 && argc != 3) {
usage();
return 1;
}
if(argc==3)
template = argv[2];
parse_dict();
found = new_Tree();
char *pre = calloc(strlen(argv[1])+1, sizeof(char));
if(!pre)
die("Couldn't allocate pre");
perm(pre, argv[1]);
return 0;
}