"diff" gets confused if one argument is a directory

Guy Harris guy at sun.uucp
Mon Jul 22 10:27:45 AEST 1985


Index:	bin/diff/diffreg.c 4.2BSD (probably also 4.3BSD)

Description:
	If you do a "diff" where one file is a binary file, and the other
	file is a directory, it will probably report the files as
	different even if the file in the directory in question being
	compared with the other file is identical to it.

Repeat-By:
	mkdir /tmp/x
	cp <binary file> /tmp/x
	cd /tmp/x
	diff <binary file> .
	
Fix:
	Have "diff" redo the "stat" on the file in the directory in question,
	so that when it compares the sizes of the two files, it compares
	the sizes of the files rather than the size of the file and the size
	of the containing directory.

Here's a "diff -c" listing - your line numbers may differ:

*** diffreg.c.orig	Mon Jun  3 21:59:38 1985
--- diffreg.c	Sun Jul 21 17:24:25 1985
***************
*** 111,117
  		done();
  	}
  	dummy = malloc(1);
! 	if ((stb1.st_mode & S_IFMT) == S_IFDIR)
  		file1 = splice(file1, file2);
  	else if ((stb2.st_mode & S_IFMT) == S_IFDIR)
  		file2 = splice(file2, file1);

--- 111,117 -----
  		done();
  	}
  	dummy = malloc(1);
! 	if ((stb1.st_mode & S_IFMT) == S_IFDIR) {
  		file1 = splice(file1, file2);
  		if (stat(file1, &stb1) < 0) {
  			fprintf(stderr, "diff: ");
***************
*** 113,119
  	dummy = malloc(1);
  	if ((stb1.st_mode & S_IFMT) == S_IFDIR)
  		file1 = splice(file1, file2);
! 	else if ((stb2.st_mode & S_IFMT) == S_IFDIR)
  		file2 = splice(file2, file1);
  	else if (!strcmp(file1, "-")) {
  		if (!strcmp(file2, "-")) {

--- 113,124 -----
  	dummy = malloc(1);
  	if ((stb1.st_mode & S_IFMT) == S_IFDIR) {
  		file1 = splice(file1, file2);
! 		if (stat(file1, &stb1) < 0) {
! 			fprintf(stderr, "diff: ");
! 			perror(file1);
! 			done();
! 		}
! 	} else if ((stb2.st_mode & S_IFMT) == S_IFDIR) {
  		file2 = splice(file2, file1);
  		if (stat(file2, &stb2) < 0) {
  			fprintf(stderr, "diff: ");
***************
*** 115,121
  		file1 = splice(file1, file2);
  	else if ((stb2.st_mode & S_IFMT) == S_IFDIR)
  		file2 = splice(file2, file1);
! 	else if (!strcmp(file1, "-")) {
  		if (!strcmp(file2, "-")) {
  			fprintf(stderr, "diff: can't specify - -\n");
  			done();

--- 120,131 -----
  		}
  	} else if ((stb2.st_mode & S_IFMT) == S_IFDIR) {
  		file2 = splice(file2, 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();
***************
*** 121,127
  			done();
  		}
  		file1 = copytemp();
! 	} else if (!strcmp(file2, "-"))
  		file2 = copytemp();
  	if ((f1 = fopen(file1, "r")) == NULL) {
  		fprintf(stderr, "diff: ");

--- 131,142 -----
  			done();
  		}
  		file1 = copytemp();
! 		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: ");
***************
*** 123,128
  		file1 = copytemp();
  	} else if (!strcmp(file2, "-"))
  		file2 = copytemp();
  	if ((f1 = fopen(file1, "r")) == NULL) {
  		fprintf(stderr, "diff: ");
  		perror(file1);

--- 138,149 -----
  		}
  	} 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);



More information about the Comp.bugs.4bsd.ucb-fixes mailing list