bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

diffutils d.8.1 cmp sends EOF message to stderr; should be to stdout


From: D. Hugh Redelmeier
Subject: diffutils d.8.1 cmp sends EOF message to stderr; should be to stdout
Date: Sun, 19 Jun 2005 16:58:31 -0400 (EDT)

According to cmp(1) and info cmp:

1   The files are different; this includes the case where one file is
    identical to the first part of the other.  In the latter case, if the
    -s option has not been specified, cmp writes to standard output that
    EOF was reached in the shorter file (before any differences were
    found).

It turns out that the message is actually sent to stderr, not stdout:
   540            if (comparison_type != type_status)
   541              {
   542                /* See POSIX 1003.1-2001 for this format.  */
   543                fprintf (stderr, _("cmp: EOF on %s\n"), file[read1 < 
read0]);
   544              }

Not only is this a mismatch between the documentation and the code, it
interacts badly with an optimization elsewhere in the code.  The
optimization is that if stdout is redirected to /dev/null, the code
pretends a -s flag was used.

Look at this rather surprising behaviour:

        $ cmp 0 00
        cmp: EOF on 0

(as expected)

But let's check if it is stdout or stderr:

        $ cmp 0 00 1>/dev/null
        $ cmp 0 00 2>/dev/null
        $ 

Huh??  No output!

        $ cmp 0 00 1>90
        cmp: EOF on 0
        $ cat 90
        $ cmp 0 00 2>90
        $ cat 90
        cmp: EOF on 0
        $

So: the output is on stderr, but is suppressed if stdout is redirected
to /dev/null.

The obvious fix (including a clearer comment):
===================================================================
RCS file: RCS/cmp.c,v
retrieving revision 1.1
diff -u -r1.1 cmp.c
--- cmp.c       2005/06/18 20:35:40     1.1
+++ cmp.c       2005/06/19 20:48:29
@@ -302,7 +302,7 @@
       && file_position (0) == file_position (1))
     return EXIT_SUCCESS;
 
-  /* If output is redirected to the null device, we may assume `-s'.  */
+  /* If stdout is redirected to the null device, we may assume `-s'.  */
 
   if (comparison_type != type_status)
     {
@@ -540,7 +540,7 @@
          if (comparison_type != type_status)
            {
              /* See POSIX 1003.1-2001 for this format.  */
-             fprintf (stderr, _("cmp: EOF on %s\n"), file[read1 < read0]);
+             printf (_("cmp: EOF on %s\n"), file[read1 < read0]);
            }
 
          return EXIT_FAILURE;
================ end ================




reply via email to

[Prev in Thread] Current Thread [Next in Thread]