4d6a38f
diff -urNp coreutils-6.10-orig/src/dd.c coreutils-6.10/src/dd.c
4d6a38f
--- coreutils-6.10-orig/src/dd.c	2007-12-06 14:22:42.000000000 +0100
4d6a38f
+++ coreutils-6.10/src/dd.c	2008-03-11 12:48:12.000000000 +0100
4d6a38f
@@ -391,6 +391,25 @@ static char const ebcdic_to_ascii[] =
4d6a38f
   '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
4d6a38f
 };
4d6a38f
 
4d6a38f
+/* True if we need to close the standard output *stream*.  */
4d6a38f
+static bool close_stdout_required = true;
4d6a38f
+
4d6a38f
+/* The only reason to close the standard output *stream* is if
4d6a38f
+   parse_long_options fails (as it does for --help or --version).
4d6a38f
+   In any other case, dd uses only the STDOUT_FILENO file descriptor,
4d6a38f
+   and the "cleanup" function calls "close (STDOUT_FILENO)".
4d6a38f
+   Closing the file descriptor and then letting the usual atexit-run
4d6a38f
+   close_stdout function call "fclose (stdout)" would result in a
4d6a38f
+   harmless failure of the close syscall (with errno EBADF).
4d6a38f
+   This function serves solely to avoid the unnecessary close_stdout
4d6a38f
+   call, once parse_long_options has succeeded.  */
4d6a38f
+static void
4d6a38f
+maybe_close_stdout (void)
4d6a38f
+{
4d6a38f
+  if (close_stdout_required)
4d6a38f
+    close_stdout ();
4d6a38f
+}
4d6a38f
+
4d6a38f
 void
4d6a38f
 usage (int status)
4d6a38f
 {
4d6a38f
@@ -1639,12 +1658,14 @@ main (int argc, char **argv)
4d6a38f
   textdomain (PACKAGE);
4d6a38f
 
4d6a38f
   /* Arrange to close stdout if parse_long_options exits.  */
4d6a38f
-  atexit (close_stdout);
4d6a38f
+  atexit (maybe_close_stdout);
4d6a38f
 
4d6a38f
   page_size = getpagesize ();
4d6a38f
 
4d6a38f
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
4d6a38f
 		      usage, AUTHORS, (char const *) NULL);
4d6a38f
+  close_stdout_required = false;
4d6a38f
+
4d6a38f
   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
4d6a38f
     usage (EXIT_FAILURE);
4d6a38f