Fix order of 2nd and 3rd arguments to fread so the count of the
[unix-history] / usr / src / usr.bin / diff / diff / diffreg.c
index 474ca5f..255305e 100644 (file)
@@ -1,4 +1,4 @@
-static char sccsid[] = "@(#)diffreg.c 4.2 %G%";
+static char sccsid[] = "@(#)diffreg.c 4.6 %G%";
 
 #include "diff.h"
 /*
 
 #include "diff.h"
 /*
@@ -97,7 +97,9 @@ long  *ixnew;         /* will be overlaid on file[1] */
 
 diffreg()
 {
 
 diffreg()
 {
-       register int k;
+       register int i, j;
+       FILE *f1, *f2;
+       char buf1[BUFSIZ], buf2[BUFSIZ];
 
        if (hflag) {
                diffargv[0] = "diffh";
 
        if (hflag) {
                diffargv[0] = "diffh";
@@ -119,8 +121,51 @@ diffreg()
                file1 = copytemp();
        } else if (!strcmp(file2, "-"))
                file2 = copytemp();
                file1 = copytemp();
        } else if (!strcmp(file2, "-"))
                file2 = copytemp();
-       prepare(0, file1);
-       prepare(1, file2);
+       if ((f1 = fopen(file1, "r")) == NULL) {
+               fprintf(stderr, "diff: ");
+               perror(file1);
+               fclose(f1);
+               done();
+       }
+       if ((f2 = fopen(file2, "r")) == NULL) {
+               fprintf(stderr, "diff: ");
+               perror(file2);
+               fclose(f1);
+               fclose(f2);
+               done();
+       }
+       if (stb1.st_size != stb2.st_size)
+               goto notsame;
+       for (;;) {
+               i = fread(buf1, 1, BUFSIZ, f1);
+               j = fread(buf2, 1, BUFSIZ, f2);
+               if (i < 0 || j < 0 || i != j)
+                       goto notsame;
+               if (i == 0 && j == 0) {
+                       fclose(f1);
+                       fclose(f2);
+                       status = 0;             /* files don't differ */
+                       goto same;
+               }
+               for (j = 0; j < i; j++)
+                       if (buf1[j] != buf2[j])
+                               goto notsame;
+       }
+notsame:
+       /*
+        *      Files certainly differ at this point; set status accordingly
+        */
+       status = 1;
+       if (!asciifile(f1) || !asciifile(f2)) {
+               printf("Binary files %s and %s differ\n", file1, file2);
+               fclose(f1);
+               fclose(f2);
+               done();
+       }
+       prepare(0, f1);
+       prepare(1, f2);
+       fclose(f1);
+       fclose(f2);
        prune();
        sort(sfile[0],slen[0]);
        sort(sfile[1],slen[1]);
        prune();
        sort(sfile[0],slen[0]);
        sort(sfile[1],slen[1]);
@@ -135,12 +180,12 @@ diffreg()
 
        klist = (int *)talloc((slen[0]+2)*sizeof(int));
        clist = (struct cand *)talloc(sizeof(cand));
 
        klist = (int *)talloc((slen[0]+2)*sizeof(int));
        clist = (struct cand *)talloc(sizeof(cand));
-       k = stone(class, slen[0], member, klist);
+       i = stone(class, slen[0], member, klist);
        free((char *)member);
        free((char *)class);
 
        J = (int *)talloc((len[0]+2)*sizeof(int));
        free((char *)member);
        free((char *)class);
 
        J = (int *)talloc((len[0]+2)*sizeof(int));
-       unravel(klist[k]);
+       unravel(klist[i]);
        free((char *)clist);
        free((char *)klist);
 
        free((char *)clist);
        free((char *)klist);
 
@@ -149,6 +194,7 @@ diffreg()
        check();
        output();
        status = anychange;
        check();
        output();
        status = anychange;
+same:
        if (opt == D_CONTEXT && anychange == 0)
                printf("No differences encountered\n");
        done();
        if (opt == D_CONTEXT && anychange == 0)
                printf("No differences encountered\n");
        done();
@@ -201,24 +247,21 @@ splice(dir, file)
        return (savestr(buf));
 }
 
        return (savestr(buf));
 }
 
-prepare(i, arg)
-char *arg;
+prepare(i, fd)
+       int i;
+       FILE *fd;
 {
        register struct line *p;
        register j,h;
 {
        register struct line *p;
        register j,h;
-       if((input[i] = fopen(arg,"r")) == NULL){
-               fprintf(stderr, "diff: ");
-               perror(arg);
-               done();
-       }
+
+       fseek(fd, (long)0, 0);
        p = (struct line *)talloc(3*sizeof(line));
        p = (struct line *)talloc(3*sizeof(line));
-       for(j=0; h=readhash(input[i]);) {
+       for(j=0; h=readhash(fd);) {
                p = (struct line *)ralloc((char *)p,(++j+3)*sizeof(line));
                p[j].value = h;
        }
        len[i] = j;
        file[i] = p;
                p = (struct line *)ralloc((char *)p,(++j+3)*sizeof(line));
                p[j].value = h;
        }
        len[i] = j;
        file[i] = p;
-       fclose(input[i]);
 }
 
 prune()
 }
 
 prune()
@@ -362,8 +405,15 @@ check()
        int jackpot;
        long ctold, ctnew;
        char c,d;
        int jackpot;
        long ctold, ctnew;
        char c,d;
-       input[0] = fopen(file1,"r");
-       input[1] = fopen(file2,"r");
+
+       if ((input[0] = fopen(file1,"r")) == NULL) {
+               perror(file1);
+               done();
+       }
+       if ((input[1] = fopen(file2,"r")) == NULL) {
+               perror(file2);
+               done();
+       }
        j = 1;
        ixold[0] = ixnew[0] = 0;
        jackpot = 0;
        j = 1;
        ixold[0] = ixnew[0] = 0;
        jackpot = 0;
@@ -468,7 +518,11 @@ int *b;
 skipline(f)
 {
        register i;
 skipline(f)
 {
        register i;
-       for(i=1;getc(input[f])!='\n';i++) ;
+       char c;
+
+       for(i=1;(c=getc(input[f]))!='\n';i++)
+               if (c < 0)
+                       return(i);
        return(i);
 }
 
        return(i);
 }
 
@@ -713,3 +767,27 @@ FILE *f;
        sum = low(sum) + high(sum);
        return((short)low(sum) + (short)high(sum));
 }
        sum = low(sum) + high(sum);
        return((short)low(sum) + (short)high(sum));
 }
+
+#include <a.out.h>
+
+asciifile(f)
+       FILE *f;
+{
+       char buf[BUFSIZ];
+       register int cnt;
+       register char *cp;
+
+       fseek(f, (long)0, 0);
+       cnt = fread(buf, 1, BUFSIZ, f);
+       if (cnt >= sizeof (struct exec)) {
+               struct exec hdr;
+               hdr = *(struct exec *)buf;
+               if (!N_BADMAG(hdr))
+                       return (0);
+       }
+       cp = buf;
+       while (--cnt >= 0)
+               if (*cp++ & 0200)
+                       return (0);
+       return (1);
+}