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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* Tcl command procedure to compare two files and report whether
* they are identical.
*
* P. Mackerras 17/7/96.
*
* $Id: filecmp.c,v 1.3 2001/08/15 11:12:44 paulus Exp $
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <tcl.h>
#include <sys/fcntl.h>
#include <unistd.h> /* close, read */
#define BSIZE 32768
#define MAXTAGLEN 512 /* max tag length for sanity, < BSIZE */
#ifdef NO_MEMMOVE
#define memmove(t, f, n) bcopy(f, t, n)
#endif
char *rcs_ignores[] = {
"Log", /* must be at index 0! */
"Id",
"Revision",
"Source",
"Author",
"Date",
"State",
"Header",
NULL
};
char *comment_leaders[] = {
" *",
"#",
NULL
};
/*
* See if the text starting at p, for n chars, looks like an RCS tag.
* We already know p[0] == '$' and n >= 4.
* Return 0 if it isn't a tag, -1 if it looks like a tag but is incomplete,
* or the length of the tag.
*/
int
tag_length(p, n)
char *p;
int n;
{
int i, j, k, l;
int maybe;
char *t, *cl;
/* check through list of tags */
for (i = 0; (t = rcs_ignores[i]) != NULL; ++i) {
for (j = 1; *t != 0; ++j, ++t) {
if (j >= n)
return -1;
if (p[j] != *t)
break;
}
if (*t == 0)
break;
}
if (t == NULL) /* if tag wasn't in list */
return 0;
if (j >= n) /* if we got to the end of the buffer */
return -1;
if (p[j] == '$') /* for the \dollar Id\dollar case */
return j + 1;
if (p[j] != ':') /* tag word must be followed by $ or : */
return 0;
while (++j < n) { /* if by :, matching $ must appear before \n */
if (p[j] == '\n')
return 0; /* got \n without $: not a tag */
if (p[j] == '$') {
if (i != 0) /* got $: if the tag wasn't \dollar Log, */
return j + 1; /* that's the end of the tag */
/* here we skip lines that start with ' * ' or '# '. */
for (;;) {
/* skip to \n */
while (++j < n && p[j] != '\n')
;
if (++j >= n)
break;
maybe = 0;
l = n - j;
if (l >= 3 && p[j] == ' ' && p[j+1] == '*' && p[j+2] != '/')
continue; /* C-style comment leader */
if (p[j] == '#')
continue; /* Script comment leader */
if (l < 3 && p[j] == ' ' && (l < 2 || p[j] == '*'))
maybe = 1; /* may be C-style comment leader */
if (maybe)
break;
return j; /* we've found the end of the tag */
}
/* Incomplete tag following \dollar Log;
the complete tag could be quite long. */
if (n >= BSIZE)
return 0;
return -1;
}
}
if (n > MAXTAGLEN) /* incomplete tag too long: not a tag */
return 0;
return -1; /* incomplete tag */
}
int rcscmp(char *p1, int *k1p, char *p2, int *k2p, int e1, int e2)
{
int k1 = *k1p, k2 = *k2p;
int i, t1, t2;
for (;;) {
for (i = 0; i < k1 && i < k2; ++i) {
if (p1[i] != p2[i])
return 0;
if (p1[i] == '$')
break;
}
p1 += i;
k1 -= i;
p2 += i;
k2 -= i;
/* 4 == strlen("<dollar>Id<dollar>") */
if (k1 < 4 && !e1 || k2 < 4 && !e2)
break;
if (k1 < 4 || k2 < 4) {
/* near the end of one or both files */
if (k1 != k2 || memcmp(p1, p2, k1) != 0)
return 0;
k1 = k2 = 0;
break;
}
/* check that the tags look the same (1st 2 chars at least) */
if (p1[1] != p2[1] || p1[2] != p2[2])
return 0;
t1 = tag_length(p1, k1);
if (t1 < 0 && e1)
t1 = 0;
if (t1 != 0) {
t2 = tag_length(p2, k2);
if (t2 < 0 && e2)
t2 = 0;
}
if (t1 == 0 || t2 == 0) {
/* one or other string isn't a tag */
p1 += 3;
k1 -= 3;
p2 += 3;
k2 -= 3;
continue;
}
if (t1 < 0 || t2 < 0)
break; /* one or other tag is incomplete */
p1 += t1;
k1 -= t1;
p2 += t2;
k2 -= t2;
if (k1 == 0 || k2 == 0)
break;
}
*k1p = k1;
*k2p = k2;
return 1;
}
static char bktag[] = "BK Id: ";
#define BKTAGLEN 7
int bkcmp(char *p1, int *k1p, char *p2, int *k2p, int e1, int e2)
{
int k1 = *k1p, k2 = *k2p;
int i, match, t1, t2;
for (;;) {
match = 0;
for (i = 0; i < k1 && i < k2; ++i) {
if (p1[i] != p2[i])
return 0;
if (p1[i] == bktag[match]) {
if (++match == BKTAGLEN) {
++i;
break;
}
} else {
match = (p1[i] == bktag[0]);
}
}
p1 += i;
k1 -= i;
p2 += i;
k2 -= i;
if (match < BKTAGLEN) {
/* we have run out of one or other buffer */
if (k1 == 0 && e1 || k2 == 0 && e2) {
if (k1 == k2)
break;
return 0;
}
k1 += match;
k2 += match;
break;
}
/* found a tag, skip to eol on both files */
for (t1 = 0; t1 < k1; ++t1)
if (p1[t1] == '\n')
break;
for (t2 = 0; t2 < k2; ++t2)
if (p2[t2] == '\n')
break;
if (t1 < k1 && t2 < k2) {
p1 += t1;
k1 -= t1;
p2 += t2;
k2 -= t2;
continue;
}
/* ran out before eol on one or both files */
if (t1 == k1 && e1 || t2 == k2 && e2) {
k1 -= t1;
k2 -= t2;
if (k1 == k2)
break;
return 0;
}
/* not at eof on either file */
k1 += BKTAGLEN;
k2 += BKTAGLEN;
break;
}
*k1p = k1;
*k2p = k2;
return 1;
}
int
FileCmpCmd(clientData, interp, argc, argv)
ClientData clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int i, fi;
int f1, f2;
char *b1, *b2;
int n1, n2, same;
int k1, k2, t1, t2;
int rcsflag, bkflag;
int e1, e2;
fi = 1;
rcsflag = 0;
bkflag = 0;
if (argc > 1 && strcmp(argv[1], "-rcs") == 0) {
++fi;
rcsflag = 1;
} else if (argc > 1 && strcmp(argv[1], "-bk") == 0) {
++fi;
bkflag = 1;
}
if (argc != fi + 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ?-rcs? file1 file2\"", (char *) NULL);
return TCL_ERROR;
}
if ((f1 = open(argv[fi], O_RDONLY)) == -1) {
Tcl_AppendResult(interp, "can't open \"", argv[fi], "\": ",
Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if ((f2 = open(argv[fi+1], O_RDONLY)) == -1) {
Tcl_AppendResult(interp, "can't open \"", argv[fi+1], "\": ",
Tcl_PosixError(interp), (char *) NULL);
close(f1);
return TCL_ERROR;
}
b1 = (char *) ckalloc(BSIZE);
b2 = (char *) ckalloc(BSIZE);
same = 1;
k1 = k2 = 0;
e1 = e2 = 0;
for (;;) {
/* Here we already have k1 chars in b1 and k2 chars in b2. */
if (!e1 && k1 < BSIZE) {
n1 = read(f1, b1 + k1, BSIZE - k1);
if (n1 < 0) {
Tcl_AppendResult(interp, "error reading \"", argv[fi],
"\": ", Tcl_PosixError(interp),
(char *) NULL);
break;
}
k1 += n1;
if (k1 < BSIZE)
e1 = 1;
}
if (!e2 && k2 < BSIZE) {
n2 = read(f2, b2 + k2, BSIZE - k2);
if (n2 < 0) {
Tcl_AppendResult(interp, "error reading \"", argv[fi+1],
"\": ", Tcl_PosixError(interp),
(char *) NULL);
break;
}
k2 += n2;
if (k2 < BSIZE)
e2 = 1;
}
if (k1 == 0 && k2 == 0)
break;
t1 = k1;
t2 = k2;
if (rcsflag)
same = rcscmp(b1, &k1, b2, &k2, e1, e2);
else if (bkflag)
same = bkcmp(b1, &k1, b2, &k2, e1, e2);
else {
if (k1 != k2 || memcmp(b1, b2, k1) != 0)
same = 0;
k1 = k2 = 0;
}
if (!same || (e1 && e2))
break;
if (k1 > 0)
memmove(b1, b1 + t1 - k1, k1);
if (k2 > 0)
memmove(b2, b2 + t2 - k2, k2);
}
close(f1);
close(f2);
ckfree(b1);
ckfree(b2);
if (n1 < 0 || n2 < 0)
return TCL_ERROR;
sprintf(interp->result, "%d", same);
return TCL_OK;
}
int
Filecmp_Init(interp)
Tcl_Interp *interp;
{
Tcl_CreateCommand(interp, "filecmp", FileCmpCmd, NULL, NULL);
return TCL_OK;
}
|