Vincent Lefevre wrote:
POSIX actually specifies the behavior:

Ouch, right you are. I recall reading POSIX and coming to the opposite conclusion, that the behavior was not specified. Perhaps this was long ago and input files were required to be text files.

Anyway, since it's incompatible with other 'diff' implementations and with POSIX, and since there's not a particularly strong reason to differ, let's change to be compatible. I applied the attached patch to the savannah master and am taking the liberty of marking this as done; please give it a try.
From b96508741158c1ff9a52e724a2b1d4f6e5ab2c36 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Fri, 31 Jan 2014 17:15:26 -0800
Subject: [PATCH] diff: exit with status 1, not 2, when binary files differ

Problem reported by Vincent Lefevre in <http://bugs.gnu.org/16608>.
* NEWS:
* doc/diffutils.texi (Binary, Invoking diff): Document this.
* src/analyze.c (briefly_report): Return void, not int.
All uses changed.  Do not futz with exit status.  Simplify.
* tests/binary: Adjust to match new behavior.
---
 NEWS               |  3 +++
 doc/diffutils.texi | 15 +++------------
 src/analyze.c      | 28 +++++++++-------------------
 tests/binary       |  4 ++--
 4 files changed, 17 insertions(+), 33 deletions(-)

diff --git a/NEWS b/NEWS
index 79517f2..aff7a9d 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@ GNU diffutils NEWS                                    -*- 
outline -*-
 
 ** Bug fixes
 
+  When binary files differ, diff now exits with status 1 as POSIX requires.
+  Formerly it exited with status 2.
+
   Unless the --ignore-file-name-case option is used, diff now
   considers file names to be equal only if they are byte-for-byte
   equivalent.  This fixes a bug where diff in an English locale might
diff --git a/doc/diffutils.texi b/doc/diffutils.texi
index 2d238dc..1d48f54 100644
--- a/doc/diffutils.texi
+++ b/doc/diffutils.texi
@@ -426,6 +426,8 @@ binary (a non-text file), it normally treats that pair of 
files much as
 if the summary output format had been selected (@pxref{Brief}), and
 reports only that the binary files are different.  This is because line
 by line comparisons are usually not meaningful for binary files.
+This does not count as trouble, even though the resulting output does
+not capture all the differences.
 
 @command{diff} determines whether a file is text or binary by checking the
 first few bytes in the file; the exact number of bytes is system
@@ -450,14 +452,6 @@ You can also force @command{diff} to report only whether 
files differ
 (but not how).  Use the @option{--brief} (@option{-q}) option for
 this.
 
-Normally, differing binary files count as trouble because the
-resulting @command{diff} output does not capture all the differences.
-This trouble causes @command{diff} to exit with status 2.  However,
-this trouble cannot occur with the @option{--text} (@option{-a})
-option, or with the @option{--brief} (@option{-q}) option, as these
-options both cause @command{diff} to generate a form of output that
-represents differences as requested.
-
 In operating systems that distinguish between text and binary files,
 @command{diff} normally reads and writes all data as text.  Use the
 @option{--binary} option to force @command{diff} to read and write binary
@@ -3705,10 +3699,7 @@ argument by itself treats the remaining arguments as 
file names even if
 they begin with @samp{-}.
 
 An exit status of 0 means no differences were found, 1 means some
-differences were found, and 2 means trouble.  Normally, differing
-binary files count as trouble, but this can be altered by using the
-@option{--text} (@option{-a}) option, or the @option{-q} or
-@option{--brief} option.
+differences were found, and 2 means trouble.
 
 @menu
 * diff Options:: Summary of options to @command{diff}.
diff --git a/src/analyze.c b/src/analyze.c
index 9dc9347..1886e7e 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -445,26 +445,16 @@ build_script (struct file_data const filevec[])
   return script;
 }
 
-/* If CHANGES, briefly report that two files differed.
-   Return 2 if trouble, CHANGES otherwise.  */
-static int
+/* If CHANGES, briefly report that two files differed.  */
+static void
 briefly_report (int changes, struct file_data const filevec[])
 {
   if (changes)
-    {
-      char const *label0 = file_label[0] ? file_label[0] : filevec[0].name;
-      char const *label1 = file_label[1] ? file_label[1] : filevec[1].name;
-
-      if (brief)
-       message ("Files %s and %s differ\n", label0, label1);
-      else
-       {
-         message ("Binary files %s and %s differ\n", label0, label1);
-         changes = 2;
-       }
-    }
-
-  return changes;
+    message ((brief
+             ? _("Files %s and %s differ\n")
+             : _("Binary files %s and %s differ\n")),
+            file_label[0] ? file_label[0] : filevec[0].name,
+            file_label[1] ? file_label[1] : filevec[1].name);
 }
 
 /* Report the differences of two files.  */
@@ -536,7 +526,7 @@ diff_2_files (struct comparison *cmp)
            }
        }
 
-      changes = briefly_report (changes, cmp->file);
+      briefly_report (changes, cmp->file);
     }
   else
     {
@@ -635,7 +625,7 @@ diff_2_files (struct comparison *cmp)
        changes = (script != 0);
 
       if (brief)
-       changes = briefly_report (changes, cmp->file);
+       briefly_report (changes, cmp->file);
       else
        {
          if (changes || !no_diff_means_no_output)
diff --git a/tests/binary b/tests/binary
index d7b3943..0110f6e 100755
--- a/tests/binary
+++ b/tests/binary
@@ -9,8 +9,8 @@ fail=0
 
 printf '\0'|diff - /dev/null > out 2> err
 
-# diff must exit with status 2, stdout as above, and no stderr.
-test $? = 2 || fail=1
+# diff must exit with status 1, stdout as above, and no stderr.
+test $? = 1 || fail=1
 compare out-exp out || fail=1
 compare /dev/null err || fail=1
 
-- 
1.8.5.3

Reply via email to