summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/System/Program.h4
-rw-r--r--lib/Bytecode/Reader/ReaderWrappers.cpp2
-rw-r--r--lib/Bytecode/Writer/Writer.cpp6
-rw-r--r--lib/System/Unix/Program.inc8
-rw-r--r--lib/System/Win32/Program.inc14
5 files changed, 34 insertions, 0 deletions
diff --git a/include/llvm/System/Program.h b/include/llvm/System/Program.h
index 2f33ea5746..0a237bccbc 100644
--- a/include/llvm/System/Program.h
+++ b/include/llvm/System/Program.h
@@ -74,6 +74,10 @@ namespace sys {
///< this function will wait until the child finishes or forever if
///< it doesn't.
);
+ // These methods change the specified standard stream (stdin or stdout) to
+ // binary mode.
+ static void ChangeStdinToBinary();
+ static void ChangeStdoutToBinary();
};
}
}
diff --git a/lib/Bytecode/Reader/ReaderWrappers.cpp b/lib/Bytecode/Reader/ReaderWrappers.cpp
index 1ea5c0262f..956d1ede69 100644
--- a/lib/Bytecode/Reader/ReaderWrappers.cpp
+++ b/lib/Bytecode/Reader/ReaderWrappers.cpp
@@ -19,6 +19,7 @@
#include "llvm/Instructions.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/System/MappedFile.h"
+#include "llvm/System/Program.h"
#include <cerrno>
#include <iostream>
#include <memory>
@@ -132,6 +133,7 @@ namespace {
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
: BytecodeReader(H)
{
+ sys::Program::ChangeStdinToBinary();
char Buffer[4096*4];
// Read in all of the data from stdin, we cannot mmap stdin...
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index 83e8a57acf..4dc80d1809 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -29,6 +29,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/Compressor.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/System/Program.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include <cstring>
@@ -1217,6 +1218,11 @@ void llvm::WriteBytecodeToFile(const Module *M, std::ostream &Out,
bool compress ) {
assert(M && "You can't write a null module!!");
+ // Make sure that std::cout is put into binary mode for systems
+ // that care.
+ if (&Out == std::cout)
+ sys::Program::ChangeStdoutToBinary();
+
// Create a vector of unsigned char for the bytecode output. We
// reserve 256KBytes of space in the vector so that we avoid doing
// lots of little allocations. 256KBytes is sufficient for a large
diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc
index 47810bdbeb..01eefe0d52 100644
--- a/lib/System/Unix/Program.inc
+++ b/lib/System/Unix/Program.inc
@@ -227,4 +227,12 @@ Program::ExecuteAndWait(const Path& path,
}
+void Program::ChangeStdinToBinary(){
+ // Do nothing, as Unix doesn't differentiate between text and binary.
+}
+
+void Program::ChangeStdoutToBinary(){
+ // Do nothing, as Unix doesn't differentiate between text and binary.
+}
+
}
diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc
index 95f56b231f..c29adf0bd1 100644
--- a/lib/System/Win32/Program.inc
+++ b/lib/System/Win32/Program.inc
@@ -12,8 +12,10 @@
//===----------------------------------------------------------------------===//
#include "Win32.h"
+#include <cstdio>
#include <malloc.h>
#include <io.h>
+#include <fcntl.h>
//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only Win32 specific code
@@ -218,4 +220,16 @@ Program::ExecuteAndWait(const Path& path,
return status;
}
+void Program::ChangeStdinToBinary(){
+ int result = _setmode( _fileno(stdin), _O_BINARY );
+ if( result == -1 )
+ throw std::string("Cannot set input mode on stdin to binary.");
+}
+
+void Program::ChangeStdoutToBinary(){
+ int result = _setmode( _fileno(stdout), _O_BINARY );
+ if( result == -1 )
+ throw std::string("Cannot set output mode on stdout to binary.");
+}
+
}