BSD 4_4 release
[unix-history] / usr / src / usr.bin / diff / diff / diffreg.c
index 33988de..2dd37a8 100644 (file)
@@ -1,6 +1,18 @@
-static char sccsid[] = "@(#)diffreg.c 4.12 %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));
 }
 
@@ -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) {
@@ -637,7 +675,7 @@ output()
                }
 #undef c
        }
                }
 #undef c
        }
-       if (opt == D_CONTEXT)
+       if (anychange && opt == D_CONTEXT)
                dump_context_vec();
 }
 
                dump_context_vec();
 }
 
@@ -667,7 +705,6 @@ struct      context_vec     *context_vec_start,
 */
 change(a,b,c,d)
 {
 */
 change(a,b,c,d)
 {
-       int ch;
        int lowa,upb,lowc,upd;
        struct stat stbuf;
 
        int lowa,upb,lowc,upd;
        struct stat stbuf;
 
@@ -690,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
@@ -730,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);
@@ -740,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;
        }
 }
@@ -780,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 */
@@ -818,7 +861,7 @@ char *s;
        }
 
        if (inifdef && !wantelses) {
        }
 
        if (inifdef && !wantelses) {
-               fprintf(stdout, "#endif %s\n", endifname);
+               fprintf(stdout, "#endif /* %s */\n", endifname);
                inifdef = 0;
        }
 }
                inifdef = 0;
        }
 }