Opened 19 years ago

Closed 19 years ago

Last modified 9 years ago

#4556 closed defect (fixed)

BUG: diff <(echo ab) <(echo cd) and same files problem

Reported by: vincent-opdarw@… Owned by: blb@…
Priority: Normal Milestone:
Component: ports Version: 1.0
Keywords: Cc:
Port: diffutils

Description (last modified by ryandesign (Ryan Carsten Schmidt))

When doing "diff <(echo ab) <(echo cd)" under zsh, diff sees /dev/fd/15 and /dev/fd/16 as the same file. Indeed, "stat <(echo ab) <(echo cd)" gives the same values for both. The following patch is a workaround that fixes the problem:

--- diff.c.bak	2002-03-24 07:35:28.000000000 +0000
+++ diff.c	2005-08-21 13:19:28.000000000 +0000
@@ -1208,7 +1208,9 @@
 	       && cmp.file[1].desc != NONEXISTENT
 	       && 0 < same_file (&cmp.file[0].stat, &cmp.file[1].stat)
 	       && same_file_attributes (&cmp.file[0].stat,
-					&cmp.file[1].stat)))
+					&cmp.file[1].stat)
+	       && strncmp (cmp.file[0].name, "/dev/fd/", 8) != 0
+	       && strncmp (cmp.file[1].name, "/dev/fd/", 8) != 0))
 	   && no_diff_means_no_output)
     {
       /* The two named files are actually the same physical file.

Change History (10)

comment:1 Changed 19 years ago by vincent-opdarw@…

Testing the file names may not be the best idea (though this would work in the case <(...) <(...), which is the most common AFAIK). As I've been told in the bug-report mailing-list for gnu-utils, there could be a symlink to /dev/fd. Another possibility is to test if one of the files is a fifo.

I've reported the problem to Apple since /dev/fd/* is not POSIX-compliant.

comment:2 Changed 19 years ago by blb@…

This is against the diffutils port right?

I wonder if this is a shell or diff problem; I note the stock /usr/bin/diff seems to behave the same since it is, after all, GNU diff 2.8.1. I believe FreeBSD has patched some things (bash2 for example) to work around devfs problems...

comment:3 Changed 19 years ago by vincent-opdarw@…

The shell opens a fd, but it is not responsible for the attributes such as the device and the inode numbers. So, this is not a shell problem. But even on other OS, I think diff doesn't behave correctly. However the problem is much more important under Mac OS X. Here's what I said in the discussion in gnu-utils bug mailing-list:

From <http://www.opengroup.org/onlinepubs/007908799/xcu/diff.html>: "The diff utility will compare the contents of file1 and file2 and write to standard output a list of changes necessary to convert file1 into file2. This list should be minimal. No output will be produced if the files are identical."

It really says "compare the contents of file1 and file2". So, even if the files are identical, the contents should be compared. The 3rd sentence just gives precision concerning the first two, and is just about output (not about whether the files are read or not).

Last edited 9 years ago by ryandesign (Ryan Carsten Schmidt) (previous) (diff)

comment:4 Changed 19 years ago by mww@…

Summary: diff <(echo ab) <(echo cd) and same files problemBUG: diff <(echo ab) <(echo cd) and same files problem

comment:5 Changed 19 years ago by vincent-opdarw@…

(In reply to comment #1)

I've reported the problem to Apple since /dev/fd/* is not POSIX-compliant.

My bug report seems to have been lost (no trace, no bug id). I've submitted it again. The bug id is 4341734.

comment:6 Changed 19 years ago by vincent-opdarw@…

Mac OS X 10.4.5 still has this bug. IMHO, the patch should be applied in DP until the bug is fixed by Apple.

comment:7 Changed 19 years ago by blb@…

Should the original patch in this bug (against diff.c) be used, or the later one in your discussion (at <http://lists.gnu.org/archive/html/bug-gnu-utils/2005-08/msg00106.html>) against system.h?

comment:8 Changed 19 years ago by vincent-opdarw@…

The latter one (i.e. adding the nanoseconds test to same_file_attributes in system.h) is better.

comment:9 Changed 19 years ago by blb@…

Resolution: fixed
Status: newclosed

Patch has been committed, thanks for the work on this and sorry it took so long.

comment:10 Changed 9 years ago by ryandesign (Ryan Carsten Schmidt)

Description: modified (diff)
Port: diffutils added
Note: See TracTickets for help on using tickets.