BSD 4_4 release
[unix-history] / usr / src / usr.bin / diff / diff / diffreg.c
index 9be4f39..2dd37a8 100644 (file)
@@ -1,6 +1,18 @@
-static char sccsid[] = "@(#)diffreg.c 4.10 %G%";
+/*-
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This module is believed to contain source code proprietary to AT&T.
+ * Use and redistribution is subject to the Berkeley Software License
+ * Agreement and your Software Agreement with AT&T (Western Electric).
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)diffreg.c  8.1 (Berkeley) 6/6/93";
+#endif /* not lint */
 
 #include "diff.h"
 
 #include "diff.h"
+#include "pathnames.h"
 /*
  * diff - compare two files.
  */
 /*
  * diff - compare two files.
  */
@@ -152,18 +164,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 ((stb1.st_mode & S_IFMT) != S_IFREG || !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 ((stb2.st_mode & S_IFMT) != S_IFREG || !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 +274,8 @@ same:
        done();
 }
 
        done();
 }
 
+char tempfile[] = _PATH_TMP;
+
 char *
 copytemp()
 {
 char *
 copytemp()
 {
@@ -251,7 +286,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 +319,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));
 }
 
@@ -448,7 +483,7 @@ check()
        register int i, j;
        int jackpot;
        long ctold, ctnew;
        register int i, j;
        int jackpot;
        long ctold, ctnew;
-       char c,d;
+       register int c,d;
 
        if ((input[0] = fopen(file1,"r")) == NULL) {
                perror(file1);
 
        if ((input[0] = fopen(file1,"r")) == NULL) {
                perror(file1);
@@ -550,6 +585,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) {
@@ -590,8 +628,7 @@ int *b;
 
 skipline(f)
 {
 
 skipline(f)
 {
-       register i;
-       char c;
+       register i, c;
 
        for(i=1;(c=getc(input[f]))!='\n';i++)
                if (c < 0)
 
        for(i=1;(c=getc(input[f]))!='\n';i++)
                if (c < 0)
@@ -638,7 +675,7 @@ output()
                }
 #undef c
        }
                }
 #undef c
        }
-       if (opt == D_CONTEXT)
+       if (anychange && opt == D_CONTEXT)
                dump_context_vec();
 }
 
                dump_context_vec();
 }
 
@@ -668,7 +705,6 @@ struct      context_vec     *context_vec_start,
 */
 change(a,b,c,d)
 {
 */
 change(a,b,c,d)
 {
-       char ch;
        int lowa,upb,lowc,upd;
        struct stat stbuf;
 
        int lowa,upb,lowc,upd;
        struct stat stbuf;
 
@@ -691,10 +727,6 @@ change(a,b,c,d)
                        context_vec_ptr = context_vec_start - 1;
                }
        }
                        context_vec_ptr = context_vec_start - 1;
                }
        }
-       if (a <= b && c <= d)
-               ch = 'c';
-       else
-               ch = (a <= b) ? 'd' : 'a';
        if(opt == D_CONTEXT) {
                /*
                 * if this new change is within 'context' lines of
        if(opt == D_CONTEXT) {
                /*
                 * if this new change is within 'context' lines of
@@ -731,6 +763,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);
@@ -741,7 +783,7 @@ change(a,b,c,d)
        if ((opt ==D_EDIT || opt == D_REVERSE) && c<=d)
                prints(".\n");
        if (inifdef) {
        if ((opt ==D_EDIT || opt == D_REVERSE) && c<=d)
                prints(".\n");
        if (inifdef) {
-               fprintf(stdout, "#endif %s\n", endifname);
+               fprintf(stdout, "#endif /* %s */\n", endifname);
                inifdef = 0;
        }
 }
                inifdef = 0;
        }
 }
@@ -781,7 +823,7 @@ char *s;
                return;
        if (opt == D_IFDEF) {
                if (inifdef)
                return;
        if (opt == D_IFDEF) {
                if (inifdef)
-                       fprintf(stdout, "#else %s%s\n", oneflag && oldfile==1 ? "!" : "", ifdef2);
+                       fprintf(stdout, "#else /* %s%s */\n", oneflag && oldfile==1 ? "!" : "", ifdef2);
                else {
                        if (oneflag) {
                                /* There was only one ifdef given */
                else {
                        if (oneflag) {
                                /* There was only one ifdef given */
@@ -819,11 +861,12 @@ char *s;
        }
 
        if (inifdef && !wantelses) {
        }
 
        if (inifdef && !wantelses) {
-               fprintf(stdout, "#endif %s\n", endifname);
+               fprintf(stdout, "#endif /* %s */\n", endifname);
                inifdef = 0;
        }
 }
 
                inifdef = 0;
        }
 }
 
+#define POW2                   /* define only if HALFLONG is 2**n */
 #define HALFLONG 16
 #define low(x) (x&((1L<<HALFLONG)-1))
 #define high(x)        (x>>HALFLONG)
 #define HALFLONG 16
 #define low(x) (x&((1L<<HALFLONG)-1))
 #define high(x)        (x>>HALFLONG)
@@ -848,13 +891,23 @@ register FILE *f;
                        for(shift=0;(t=getc(f))!='\n';shift+=7) {
                                if(t==-1)
                                        return(0);
                        for(shift=0;(t=getc(f))!='\n';shift+=7) {
                                if(t==-1)
                                        return(0);
-                               sum += (long)chrtran[t] << (shift%=HALFLONG);
+                               sum += (long)chrtran[t] << (shift
+#ifdef POW2
+                                   &= HALFLONG - 1);
+#else
+                                   %= HALFLONG);
+#endif
                        }
                else
                        for(shift=0;(t=getc(f))!='\n';shift+=7) {
                                if(t==-1)
                                        return(0);
                        }
                else
                        for(shift=0;(t=getc(f))!='\n';shift+=7) {
                                if(t==-1)
                                        return(0);
-                               sum += (long)t << (shift%=HALFLONG);
+                               sum += (long)t << (shift
+#ifdef POW2
+                                   &= HALFLONG - 1);
+#else
+                                   %= HALFLONG);
+#endif
                        }
        } else {
                for(shift=0;;) {
                        }
        } else {
                for(shift=0;;) {
@@ -870,7 +923,12 @@ register FILE *f;
                                        shift += 7;
                                        space = 0;
                                }
                                        shift += 7;
                                        space = 0;
                                }
-                               sum += (long)chrtran[t] << (shift%=HALFLONG);
+                               sum += (long)chrtran[t] << (shift
+#ifdef POW2
+                                   &= HALFLONG - 1);
+#else
+                                   %= HALFLONG);
+#endif
                                shift += 7;
                                continue;
                        case '\n':
                                shift += 7;
                                continue;
                        case '\n':
@@ -915,6 +973,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;
@@ -926,71 +985,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;
 }