ANSIfication; bug report 4.3BSD/bin/223
[unix-history] / usr / src / usr.bin / diff / diff / diffreg.c
index dd21a47..0dfe598 100644 (file)
@@ -1,4 +1,4 @@
-static char sccsid[] = "@(#)diffreg.c 4.11 %G%";
+static char sccsid[] = "@(#)diffreg.c 4.18 %G%";
 
 #include "diff.h"
 /*
 
 #include "diff.h"
 /*
@@ -152,18 +152,39 @@ diffreg()
                done();
        }
        chrtran = (iflag? cup2low : clow2low);
                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);
                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);
                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();
                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();
                file2 = copytemp();
+               if (stat(file2, &stb2) < 0) {
+                       fprintf(stderr, "diff: ");
+                       perror(file2);
+                       done();
+               }
+       }
        if ((f1 = fopen(file1, "r")) == NULL) {
                fprintf(stderr, "diff: ");
                perror(file1);
        if ((f1 = fopen(file1, "r")) == NULL) {
                fprintf(stderr, "diff: ");
                perror(file1);
@@ -241,6 +262,8 @@ same:
        done();
 }
 
        done();
 }
 
+char *tempfile = "/tmp/dXXXXX";
+
 char *
 copytemp()
 {
 char *
 copytemp()
 {
@@ -251,7 +274,7 @@ copytemp()
        signal(SIGINT,done);
        signal(SIGPIPE,done);
        signal(SIGTERM,done);
        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: ");
        f = creat(tempfile,0600);
        if (f < 0) {
                fprintf(stderr, "diff: ");
@@ -284,7 +307,7 @@ splice(dir, file)
                tail = file;
        else
                tail++;
                tail = file;
        else
                tail++;
-       sprintf(buf, "%s/%s", dir, tail);
+       (void)sprintf(buf, "%s/%s", dir, tail);
        return (savestr(buf));
 }
 
        return (savestr(buf));
 }
 
@@ -550,6 +573,9 @@ struct line *a;
        struct line *ai;
        register struct line *aim;
        int k;
        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) {
        for(j=1;j<=n;j*= 2)
                m = 2*j - 1;
        for(m/=2;m!=0;m/=2) {
@@ -637,7 +663,7 @@ output()
                }
 #undef c
        }
                }
 #undef c
        }
-       if (opt == D_CONTEXT)
+       if (anychange && opt == D_CONTEXT)
                dump_context_vec();
 }
 
                dump_context_vec();
 }
 
@@ -730,6 +756,16 @@ change(a,b,c,d)
                range(a,b," ");
                putchar('\n');
                break;
                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);
        }
        if(opt == D_NORMAL || opt == D_IFDEF) {
                fetch(ixold,a,b,input[0],"< ", 1);
@@ -930,6 +966,7 @@ dump_context_vec()
        register char   ch;
        register struct context_vec *cvp = context_vec_start;
        register int    lowa, upb, lowc, upd;
        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;
 
        if ( cvp > context_vec_ptr )
                return;
@@ -941,71 +978,75 @@ dump_context_vec()
 
        printf("***************\n*** ");
        range(lowa,upb,",");
 
        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;
                if (cvp->a <= cvp->b) {
                        cvp = context_vec_start;
+                       do_output++;
                        break;
                }
                        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 */
 
        /* 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;
                if (cvp->c <= cvp->d) {
                        cvp = context_vec_start;
+                       do_output++;
                        break;
                }
                        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;
 }
 
        context_vec_ptr = context_vec_start - 1;
 }