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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* fcomp.c:
This code has been adapted from:
Software Practice and Experience
Vol 15, pp. 1025-1040, November 1985.
"A File Comparison Program"
by Miller and Myers
http://tx0.org/7eq
The modifications basically allow calling the diff routine
from a separate program, upping the max lines, and modernizing
some portions of the code. Please don't ask me about the
theory involved, I do not understand it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prism.h"
#define MAXLINES 15000
#define ORIGIN MAXLINES
#define INSERT 1
#define DELETE 2
struct edit {
struct edit *link;
int op;
int line1;
int line2;
};
static char **A, **B;
static void fatal(const char *msg)
{
printf(" ERR: %s\n",msg);
}
static void exceed(int d)
{
printf(" ERR: the files differ in at least %d lines\n",d);
}
static int in_file(const char *filename, char **P)
{
char *buf, *save;
FILE *fp, *fopen();
int lines = 0;
if ((fp = fopen(filename,"r")) == NULL)
{
fatal("cannot open file");
return PRET_READERROR;
}
buf = (char *)malloc(256*sizeof(char));
if(buf == NULL)
return PRET_ERROR;
while(fgets(buf,100,fp) != NULL)
{
if (lines >= MAXLINES) {
fatal("file is to large for diff");
return PRET_ERROR;
}
save = (char *)malloc((strlen(buf)+1)*sizeof(char));
if (save == NULL) {
fatal("not enough room to save the files");
return PRET_ERROR;
}
P[lines++] = save;
strcpy(save, buf);
}
fclose(fp);
free(buf);
return(lines);
}
static void put_scr(struct edit *start, const char *fname1, const char *fname2)
{
struct edit *ep, *behind, *ahead, *a, *b;
int change;
ahead = start;
ep = NULL;
while (ahead != NULL)
{
behind = ep;
ep = ahead;
ahead = ahead->link;
ep->link = behind;
}
while( ep != NULL && 0 == 0)
{
b = ep;
if (ep->op == INSERT)
//printf("Inserted after line %d:\n",ep->line1);
printf("line %s:%d || %s:%d\n", fname1, ep->line1, fname2, ep->line2);
else
{
do
{
a = b;
b = b->link;
} while (b!=NULL && b->op == DELETE && b->line1 == a->line1+1);
change = (b!=NULL && b->op == INSERT && b->line1 == a->line1);
if (a == ep)
printf("line %s:%d || %s:%d\n", fname1, ep->line1, fname2, ep->line2);
//printf("line %d:\n",ep->line1);
else
printf("line %s:%d-%d || %s:%d-%d\n", fname1, ep->line1, a->line1, fname2, ep->line2, a->line2);
//printf("lines %d-%d:\n",ep->line1,a->line1);
do
{
printf("-%s",A[ep->line1-1]);
ep = ep->link;
} while (ep != b);
if (!change) {
printf("\n");
continue;
}
}
do
{
printf("+%s",B[ep->line2-1]);
ep = ep->link;
} while (ep != NULL && ep->op == INSERT && ep->line1 == b->line1);
printf("\n");
}
}
int compare_files(const char *fname1, const char *fname2)
{
int max_d,
m,
n,
lower,
upper,
d,
k,
row,
col;
int *last_d;
struct edit **script;
struct edit *new;
last_d = (int *)malloc((2*MAXLINES+1)*sizeof(int));
script = (struct edit **)malloc((2*MAXLINES+1)*sizeof(struct edit *));
A = (char **)malloc(MAXLINES*sizeof(char *));
B = (char **)malloc(MAXLINES*sizeof(char *));
if(A == NULL || B == NULL) return PRET_ERROR;
memset(A, 0, MAXLINES);
memset(B, 0, MAXLINES);
max_d = 2*MAXLINES;
m = in_file(fname1,A);
n = in_file(fname2,B);
for (row=0 ; row<m && row < n && strcmp(A[row],B[row]) == 0 ; ++row)
;
last_d[ORIGIN] = row;
script[ORIGIN] = NULL;
lower = (row == m) ? ORIGIN + 1 : ORIGIN - 1;
upper = (row == n) ? ORIGIN - 1 : ORIGIN + 1;
if (lower > upper)
{
printf(" the files are identical\n");
return PRET_OK;
}
for (d = 1 ; d <= max_d ; ++d)
{
for ( k = lower ; k <= upper ; k +=2)
{
new = (struct edit *) malloc(sizeof(struct edit));
if (new == NULL) {
exceed(d);
return PRET_ERROR;
}
if (k == ORIGIN-d || k != ORIGIN+d && last_d[k+1] >= last_d[k-1])
{
row = last_d[k+1]+1;
new->link = script[k+1];
new->op = DELETE;
}
else
{
row = last_d[k-1];
new->link = script[k-1];
new->op = INSERT;
}
new->line1 = row;
new->line2 = col = row + k - ORIGIN;
script[k] = new;
while(row < m && col < n && strcmp(A[row],B[col]) == 0)
{
++row;
++col;
}
last_d[k] = row;
if (row == m && col == n)
{
put_scr(script[k], fname1, fname2);
return PRET_OK;
}
if (row == m)
lower = k+2;
if (col == n)
upper = k-2;
}
--lower;
++upper;
}
exceed(d);
return PRET_ERROR;
}
|