-
Notifications
You must be signed in to change notification settings - Fork 42
/
comp.c
157 lines (137 loc) · 2.73 KB
/
comp.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
/*
* comp.c - written by Rob Pike, modified by Loki.
*
* This program doesn't quite do what the specification says.
* It treats all filenames on the command line which begin with '-'
* as options, but since the only option is '-t' any other names
* result in a usage message.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int thresh = 20;
typedef struct Sig Sig;
struct Sig
{
int nval;
unsigned long *val;
};
Sig *load(char*);
int compare(Sig*, Sig*);
void usage(void)
{
fprintf(stderr, "usage: comp [-t threshold%%] sigfile othersigfile ...\n");
fprintf(stderr, "defaults: threshold=20%%\n");
exit(2);
}
int main(int argc, char *argv[])
{
int i, j, nfiles, start, percent;
char *s;
Sig **sig;
for (start=1; start < argc; start++) {
if (argv[start][0] != '-')
break; /* finished handling options */
switch (argv[start][1]) {
case 't':
s = argv[++start];
if (s == NULL)
usage();
thresh = atoi(s);
if (thresh < 0 || thresh > 100)
usage();
break;
default:
usage();
}
}
nfiles = argc - start;
if (nfiles < 2)
usage();
sig = malloc(nfiles * sizeof(Sig*));
for (i=0; i < nfiles; i++)
sig[i] = load(argv[i+start]);
for (i=0; i < nfiles; i++)
for (j=i+1; j < nfiles; j++) {
percent = compare(sig[i], sig[j]);
if (percent >= thresh)
printf("%s and %s: %d%%\n", argv[i+start], argv[j+start], percent);
}
return 0;
}
/* ulcmp: compare *p1 and *p2 */
int ulcmp(const void *p1, const void *p2)
{
unsigned long v1, v2;
v1 = *(unsigned long *) p1;
v2 = *(unsigned long *) p2;
if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}
Sig* load(char *file)
{
FILE *f;
int nv, na;
unsigned long *v, x;
char buf[512], *p;
Sig *sig;
f = fopen(file, "r");
if (f == NULL) {
fprintf(stderr, "comp: can't open %s:", file);
perror(NULL);
exit(2);
}
v = NULL;
na = 0;
nv = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
p = NULL;
x = strtoul(buf, &p, 16);
if (p==NULL || p==buf){
fprintf(stderr, "comp: bad signature file %s\n", file);
exit(2);
}
if (nv == na) {
na += 100;
v = realloc(v, na*sizeof(unsigned long));
}
v[nv++] = x;
}
fclose(f);
qsort(v, nv, sizeof(v[0]), ulcmp);
sig = malloc(sizeof(Sig));
sig->nval = nv;
sig->val = v;
return sig;
}
int compare(Sig *s0, Sig *s1)
{
int i0, i1, nboth;
unsigned long v;
i0 = 0;
i1 = 0;
nboth = 0;
while (i0 < s0->nval && i1 < s1->nval) {
if (s0->val[i0] == s1->val[i1]) {
v = s0->val[i0];
while (i0 < s0->nval && v == s0->val[i0]) {
i0++;
nboth++;
}
while (i1 < s1->nval && v == s1->val[i1]) {
i1++;
nboth++;
}
continue;
}
if (s0->val[i0] < s1->val[i1])
i0++;
else
i1++;
}
return 100 * nboth / (s0->nval + s1->nval);
}