Re: Comparing binary files with Diff 3.2 compiled with MinGW

Eli Zaretskii <[email protected]>
Newsgroups gmane.comp.gnu.utils.bugs,gmane.comp.lib.gnulib.bugs
Message-ID <[email protected]>
> From: Bruno Haible <[email protected]>
> Cc: [email protected], Eli Zaretskii <[email protected]>, [email protected]
> Date: Sun, 13 May 2012 04:00:25 +0200
> 
> > 'set_binary_mode' would be OK
> 
> Good. New proposal attached below.

And here's the patch for Diffutils' io.c to go with the new proposal:

2012-05-13  Eli Zaretskii  <[email protected]>

	* src/io.c: Include binary-io.h.
	(sip, read_files): Switch file I/O to binary mode and back as
	appropriate, to support binary files on systems that distinguish
	between text and binary I/O.

--- src/io.c~0	2011-08-15 08:24:38.000000000 +0300
+++ src/io.c	2012-05-13 21:38:39.795500000 +0300
@@ -22,6 +22,7 @@
 #include <cmpbuf.h>
 #include <file-type.h>
 #include <xalloc.h>
+#include <binary-io.h>
 
 /* Rotate an unsigned value to the left.  */
 #define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n)))
@@ -110,12 +111,25 @@ sip (struct file_data *current, bool ski
       if (! skip_test)
 	{
 	  /* Check first part of file to see if it's a binary file.  */
-
-	  /* FIXME: if O_BINARY, this should revert to text mode
-	     if the file is not binary.  */
+	  bool binary_file;
+	  int prev_mode = set_binary_mode (current->desc, O_BINARY);
 
 	  file_block_read (current, current->bufsize);
-	  return binary_file_p (current->buffer, current->buffered);
+	  binary_file = binary_file_p (current->buffer, current->buffered);
+	  if (prev_mode != O_BINARY)
+	    {
+	      /* Revert to text mode and seek back to the beginning to
+		 reread the file.  Use relative seek, since file
+		 descriptors like stdin might not start at offset
+		 zero.  */
+
+	      if (lseek (current->desc, -current->buffered, SEEK_CUR) == -1)
+		pfatal_with_name (current->name);
+	      (void) set_binary_mode (current->desc, O_TEXT);
+              current->buffered = 0;
+              current->eof = false;
+	    }
+	  return binary_file;
 	}
     }
 
@@ -761,7 +775,8 @@ read_files (struct file_data filevec[], 
     }
   if (appears_binary)
     {
-      /* FIXME: If O_BINARY, this should set both files to binary mode.  */
+      (void) set_binary_mode (filevec[0].desc, O_BINARY);
+      (void) set_binary_mode (filevec[1].desc, O_BINARY);
       return true;
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.