-static char sccsid[] = "@(#)diffreg.c 4.11 %G%";
+static char sccsid[] = "@(#)diffreg.c 4.18 %G%";
#include "diff.h"
/*
done();
}
chrtran = (iflag? cup2low : clow2low);
- if ((stb1.st_mode & S_IFMT) == S_IFDIR)
+ if ((stb1.st_mode & S_IFMT) == S_IFDIR) {
file1 = splice(file1, file2);
- else if ((stb2.st_mode & S_IFMT) == S_IFDIR)
+ if (stat(file1, &stb1) < 0) {
+ fprintf(stderr, "diff: ");
+ perror(file1);
+ done();
+ }
+ } else if ((stb2.st_mode & S_IFMT) == S_IFDIR) {
file2 = splice(file2, file1);
- else if (!strcmp(file1, "-")) {
+ if (stat(file2, &stb2) < 0) {
+ fprintf(stderr, "diff: ");
+ perror(file2);
+ done();
+ }
+ } else if (!strcmp(file1, "-")) {
if (!strcmp(file2, "-")) {
fprintf(stderr, "diff: can't specify - -\n");
done();
}
file1 = copytemp();
- } else if (!strcmp(file2, "-"))
+ if (stat(file1, &stb1) < 0) {
+ fprintf(stderr, "diff: ");
+ perror(file1);
+ done();
+ }
+ } else if (!strcmp(file2, "-")) {
file2 = copytemp();
+ if (stat(file2, &stb2) < 0) {
+ fprintf(stderr, "diff: ");
+ perror(file2);
+ done();
+ }
+ }
if ((f1 = fopen(file1, "r")) == NULL) {
fprintf(stderr, "diff: ");
perror(file1);
done();
}
+char *tempfile = "/tmp/dXXXXX";
+
char *
copytemp()
{
signal(SIGINT,done);
signal(SIGPIPE,done);
signal(SIGTERM,done);
- tempfile = mktemp("/tmp/dXXXXX");
+ mktemp(tempfile);
f = creat(tempfile,0600);
if (f < 0) {
fprintf(stderr, "diff: ");
tail = file;
else
tail++;
- sprintf(buf, "%s/%s", dir, tail);
+ (void)sprintf(buf, "%s/%s", dir, tail);
return (savestr(buf));
}
struct line *ai;
register struct line *aim;
int k;
+
+ if (n == 0)
+ return;
for(j=1;j<=n;j*= 2)
m = 2*j - 1;
for(m/=2;m!=0;m/=2) {
}
#undef c
}
- if (opt == D_CONTEXT)
+ if (anychange && opt == D_CONTEXT)
dump_context_vec();
}
range(a,b," ");
putchar('\n');
break;
+ case D_NREVERSE:
+ if (a>b)
+ printf("a%d %d\n",b,d-c+1);
+ else {
+ printf("d%d %d\n",a,b-a+1);
+ if (!(c>d))
+ /* add changed lines */
+ printf("a%d %d\n",b, d-c+1);
+ }
+ break;
}
if(opt == D_NORMAL || opt == D_IFDEF) {
fetch(ixold,a,b,input[0],"< ", 1);
register char ch;
register struct context_vec *cvp = context_vec_start;
register int lowa, upb, lowc, upd;
+ register int do_output;
if ( cvp > context_vec_ptr )
return;
printf("***************\n*** ");
range(lowa,upb,",");
- printf(" ****\n--- ");
- range(lowc,upd,",");
- printf(" ----\n");
+ printf(" ****\n");
/*
- * output changes to the "old" file. The first loop is a
- * hack that suppresses the output if there were no changes to
- * the "old" file (we'll see the "old" lines as context in the
- * "new" list).
+ * output changes to the "old" file. The first loop suppresses
+ * output if there were no changes to the "old" file (we'll see
+ * the "old" lines as context in the "new" list).
*/
- while (cvp <= context_vec_ptr) {
+ do_output = 0;
+ for ( ; cvp <= context_vec_ptr; cvp++)
if (cvp->a <= cvp->b) {
cvp = context_vec_start;
+ do_output++;
break;
}
- cvp++;
- }
- while (cvp <= context_vec_ptr) {
- a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d;
-
- if (a <= b && c <= d)
- ch = 'c';
- else
- ch = (a <= b) ? 'd' : 'a';
-
- if (ch == 'a')
- fetch(ixold,lowa,b,input[0]," ");
- else {
- fetch(ixold,lowa,a-1,input[0]," ");
- fetch(ixold,a,b,input[0],ch == 'c' ? "!<" : "-<");
+
+ if ( do_output ) {
+ while (cvp <= context_vec_ptr) {
+ a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d;
+
+ if (a <= b && c <= d)
+ ch = 'c';
+ else
+ ch = (a <= b) ? 'd' : 'a';
+
+ if (ch == 'a')
+ fetch(ixold,lowa,b,input[0]," ");
+ else {
+ fetch(ixold,lowa,a-1,input[0]," ");
+ fetch(ixold,a,b,input[0],ch == 'c' ? "! " : "- ");
+ }
+ lowa = b + 1;
+ cvp++;
}
- lowa = b + 1;
- cvp++;
+ fetch(ixold, b+1, upb, input[0], " ");
}
- fetch(ixold, b+1, upb, input[0], " ");
/* output changes to the "new" file */
- printf("---------------\n");
+ printf("--- ");
+ range(lowc,upd,",");
+ printf(" ----\n");
- cvp = context_vec_start;
- while (cvp <= context_vec_ptr) {
+ do_output = 0;
+ for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
if (cvp->c <= cvp->d) {
cvp = context_vec_start;
+ do_output++;
break;
}
- cvp++;
- }
- while (cvp <= context_vec_ptr) {
- a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d;
-
- if (a <= b && c <= d)
- ch = 'c';
- else
- ch = (a <= b) ? 'd' : 'a';
-
- if (ch == 'd')
- fetch(ixnew,lowc,d,input[1]," ");
- else {
- fetch(ixnew,lowc,c-1,input[1]," ");
- fetch(ixnew,c,d,input[1],ch == 'c' ? "!>" : "+>");
+
+ if (do_output) {
+ while (cvp <= context_vec_ptr) {
+ a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d;
+
+ if (a <= b && c <= d)
+ ch = 'c';
+ else
+ ch = (a <= b) ? 'd' : 'a';
+
+ if (ch == 'd')
+ fetch(ixnew,lowc,d,input[1]," ");
+ else {
+ fetch(ixnew,lowc,c-1,input[1]," ");
+ fetch(ixnew,c,d,input[1],ch == 'c' ? "! " : "+ ");
+ }
+ lowc = d + 1;
+ cvp++;
}
- lowc = d + 1;
- cvp++;
+ fetch(ixnew, d+1, upd, input[1], " ");
}
- fetch(ixnew, d+1, upd, input[1], " ");
context_vec_ptr = context_vec_start - 1;
}