add notice about where devurandom.c is from
[detour.git] / pulsehunter.c
1 #include "common.c"
2
3 int main(int argc, char **argv) {
4   if (argc != 2) puts("invocation: ./pulsehunter <bits>"), exit(1);
5   char *bits = argv[1];
6   int nbits = strlen(bits);
7
8   if (chdir("out")) perror("unable to enter directory 'out'"), exit(1);
9   DIR *d = opendir(".");
10   if (!d) perror("unable to open directory 'out'"), exit(1);
11   for (struct dirent *e = (errno=0,readdir(d)); e; e = (errno=0,readdir(d))) {
12     if (e->d_name[0] == '.') continue;
13     size_t len;
14     char *data = slurp_file(e->d_name, &len);
15     if (!data) { perror("error while slurping dirent"); continue; }
16     int maxbits = 0;
17     for (int i=0; i<((int)len)-nbits; i++) {
18       int matching = 0;
19       for (int j=0; j<nbits; j++) if (bits[j]==data[i+j]) matching++;
20       if (matching > maxbits) maxbits = matching;
21     }
22     printf("%d\t%s\n", maxbits, e->d_name);
23     free(data);
24   }
25   if (errno) perror("error while reading directory 'out'"), exit(1);
26   return 0;
27 }