Changes to build successfully with GCC 3.02


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1503 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/analyze/analyze.cpp b/tools/analyze/analyze.cpp
index 9a53bd4..9241216 100644
--- a/tools/analyze/analyze.cpp
+++ b/tools/analyze/analyze.cpp
@@ -28,6 +28,10 @@
 #include "llvm/Analysis/FindUsedTypes.h"
 #include "Support/CommandLine.h"
 #include <algorithm>
+#include <iostream>
+using std::cout;
+using std::cerr;
+using std::pair;
 
 static void PrintMethod(Method *M) {
   cout << M;
@@ -38,7 +42,7 @@
 }
 
 static void PrintClassifiedExprs(Method *M) {
-  cout << "Classified expressions for: " << M->getName() << endl;
+  cout << "Classified expressions for: " << M->getName() << "\n";
   Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
   for (; I != E; ++I) {
     cout << *I;
@@ -61,7 +65,7 @@
       if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
       break;
     }
-    cout << endl << endl;
+    cout << "\n\n";
   }
 }
 
diff --git a/tools/as/as.cpp b/tools/as/as.cpp
index ee664f1..0fbd1e6 100644
--- a/tools/as/as.cpp
+++ b/tools/as/as.cpp
@@ -16,6 +16,7 @@
 #include "Support/CommandLine.h"
 #include <fstream>
 #include <string>
+#include <memory>
 
 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
@@ -28,46 +29,55 @@
   ostream *Out = 0;
   try {
     // Parse the file now...
-    Module *C = ParseAssemblyFile(InputFilename);
-    if (C == 0) {
+    std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename));
+    if (C.get() == 0) {
       cerr << "assembly didn't read correctly.\n";
       return 1;
     }
   
     if (DumpAsm)
-      cerr << "Here's the assembly:\n" << C;
+      cerr << "Here's the assembly:\n" << C.get();
 
     if (OutputFilename != "") {   // Specified an output filename?
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+             << "Use -f command line argument to force output\n";
+        return 1;
+      }
+      Out = new std::ofstream(OutputFilename.c_str());
     } else {
       if (InputFilename == "-") {
 	OutputFilename = "-";
 	Out = &cout;
       } else {
-	string IFN = InputFilename;
+	std::string IFN = InputFilename;
 	int Len = IFN.length();
 	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
 	  // Source ends in .ll
-	  OutputFilename = string(IFN.begin(), IFN.end()-3);
+	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
         } else {
 	  OutputFilename = IFN;   // Append a .bc to it
 	}
 	OutputFilename += ".bc";
-	Out = new ofstream(OutputFilename.c_str(), 
-			   (Force ? 0 : ios::noreplace)|ios::out);
-      }
-  
-      if (!Out->good()) {
-        cerr << "Error opening " << OutputFilename << "!\n";
-	delete C;
-	return 1;
+
+        if (!Force && !std::ifstream(OutputFilename.c_str())) {
+          // If force is not specified, make sure not to overwrite a file!
+          cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+               << "Use -f command line argument to force output\n";
+          return 1;
+        }
+
+	Out = new std::ofstream(OutputFilename.c_str());
       }
     }
+  
+    if (!Out->good()) {
+      cerr << "Error opening " << OutputFilename << "!\n";
+      return 1;
+    }
    
-    WriteBytecodeToFile(C, *Out);
-
-    delete C;
+    WriteBytecodeToFile(C.get(), *Out);
   } catch (const ParseException &E) {
     cerr << E.getMessage() << endl;
     return 1;
diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp
index 2a7eb4e..55e8d5d 100644
--- a/tools/dis/dis.cpp
+++ b/tools/dis/dis.cpp
@@ -24,6 +24,8 @@
 #include "Support/PostOrderIterator.h"
 #include "Support/CommandLine.h"
 #include <fstream>
+#include <iostream>
+using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
 enum OutputMode {
@@ -47,7 +49,7 @@
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
   Module *C = ParseBytecodeFile(InputFilename);
   if (C == 0) {
@@ -56,31 +58,41 @@
   }
   
   if (OutputFilename != "") {   // Specified an output filename?
-    Out = new ofstream(OutputFilename.c_str(), 
-		       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename
+           << "': File exists! Sending to standard output.\n";
+    } else {
+      Out = new std::ofstream(OutputFilename.c_str());
+    }
   } else {
     if (InputFilename == "-") {
       OutputFilename = "-";
-      Out = &cout;
     } else {
-      string IFN = InputFilename;
+      std::string IFN = InputFilename;
       int Len = IFN.length();
       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
 	// Source ends in .bc
-	OutputFilename = string(IFN.begin(), IFN.end()-3);
+	OutputFilename = std::string(IFN.begin(), IFN.end()-3);
       } else {
 	OutputFilename = IFN;   // Append a .ll to it
       }
       OutputFilename += ".ll";
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename
+             << "': File exists! Sending to standard output.\n";
+      } else {
+        Out = new std::ofstream(OutputFilename.c_str());
+      }
     }
   }
 
   if (!Out->good()) {
     cerr << "Error opening " << OutputFilename
 	 << ": sending to stdout instead!\n";
-    Out = &cout;
+    Out = &std::cout;
   }
 
   // All that dis does is write the assembly out to a file... which is exactly
@@ -100,20 +112,20 @@
       switch (WriteMode) {
       case dfo:                   // Depth First ordering
 	copy(df_begin(M), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rdfo:            // Reverse Depth First ordering
 	copy(df_begin(M, true), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case po:                    // Post Order
 	copy(po_begin(M), po_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rpo: {           // Reverse Post Order
 	ReversePostOrderTraversal RPOT(M);
 	copy(RPOT.begin(), RPOT.end(),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       }
       default:
@@ -124,6 +136,6 @@
   }
   delete C;
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }
diff --git a/tools/gccas/gccas.cpp b/tools/gccas/gccas.cpp
index b9e3a62..073b248 100644
--- a/tools/gccas/gccas.cpp
+++ b/tools/gccas/gccas.cpp
@@ -44,17 +44,17 @@
   }
   
   if (OutputFilename == "") {   // Didn't specify an output filename?
-    string IFN = InputFilename;
+    std::string IFN = InputFilename;
     int Len = IFN.length();
     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
-      OutputFilename = string(IFN.begin(), IFN.end()-2);
+      OutputFilename = std::string(IFN.begin(), IFN.end()-2);
     } else {
       OutputFilename = IFN;   // Append a .o to it
     }
     OutputFilename += ".o";
   }
 
-  Out = new ofstream(OutputFilename.c_str(), ios::out);
+  Out = new std::ofstream(OutputFilename.c_str(), ios::out);
   if (!Out->good()) {
     cerr << "Error opening " << OutputFilename << "!\n";
     return 1;
@@ -63,7 +63,7 @@
   // In addition to just parsing the input from GCC, we also want to spiff it up
   // a little bit.  Do this now.
   //
-  vector<Pass*> Passes;
+  std::vector<Pass*> Passes;
   Passes.push_back(new opt::DeadCodeElimination());  // Remove Dead code/vars
   Passes.push_back(new CleanupGCCOutput());          // Fix gccisms
   Passes.push_back(new InductionVariableSimplify()); // Simplify indvars
diff --git a/tools/link/link.cpp b/tools/link/link.cpp
index 83141c8..ad4cbdb 100644
--- a/tools/link/link.cpp
+++ b/tools/link/link.cpp
@@ -19,6 +19,7 @@
 #include <fstream>
 #include <memory>
 #include <sys/types.h>     // For FileExists
+typedef int blksize_t;   // SYS/TYPES is broken!!!
 #include <sys/stat.h>
 
 
@@ -32,7 +33,7 @@
 
 
 // FileExists - Return true if the specified string is an openable file...
-static inline bool FileExists(const string &FN) {
+static inline bool FileExists(const std::string &FN) {
   struct stat StatBuf;
   return stat(FN.c_str(), &StatBuf) != -1;
 }
@@ -40,9 +41,9 @@
 // LoadFile - Read the specified bytecode file in and return it.  This routine
 // searches the link path for the specified file to try to find it...
 //
-static inline std::auto_ptr<Module> LoadFile(const string &FN) {
-  string Filename = FN;
-  string ErrorMessage;
+static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
+  std::string Filename = FN;
+  std::string ErrorMessage;
 
   unsigned NextLibPathIdx = 0;
   bool FoundAFile = false;
@@ -81,7 +82,7 @@
   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
 
   unsigned BaseArg = 0;
-  string ErrorMessage;
+  std::string ErrorMessage;
 
   // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
   if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
@@ -94,7 +95,7 @@
   if (Composite.get() == 0) return 1;
 
   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
-    auto_ptr<Module> M(LoadFile(InputFilenames[i]));
+    std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
     if (M.get() == 0) return 1;
 
     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
@@ -111,8 +112,13 @@
 
   ostream *Out = &cout;  // Default to printing to stdout...
   if (OutputFilename != "-") {
-    Out = new ofstream(OutputFilename.c_str(), 
-		       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+           << "Use -f command line argument to force output\n";
+      return 1;
+    }
+    Out = new std::ofstream(OutputFilename.c_str());
     if (!Out->good()) {
       cerr << "Error opening '" << OutputFilename << "'!\n";
       return 1;
@@ -122,6 +128,6 @@
   if (Verbose) cerr << "Writing bytecode...\n";
   WriteBytecodeToFile(Composite.get(), *Out);
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }
diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp
index 7c8c3a6..6b51f79 100644
--- a/tools/llc/llc.cpp
+++ b/tools/llc/llc.cpp
@@ -19,6 +19,7 @@
 #include <memory>
 #include <string>
 #include <fstream>
+using std::string;
 
 cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
 cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
@@ -78,10 +79,10 @@
 
 class EmitAssembly : public Pass {
   const TargetMachine &Target;   // Target to compile for
-  ostream *Out;                  // Stream to print on
+  std::ostream *Out;             // Stream to print on
   bool DeleteStream;             // Delete stream in dtor?
 public:
-  inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
+  inline EmitAssembly(const TargetMachine &T, std::ostream *O, bool D)
     : Target(T), Out(O), DeleteStream(D) {}
 
 
@@ -105,20 +106,20 @@
   
   // Allocate a target... in the future this will be controllable on the
   // command line.
-  auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
+  std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
   assert(target.get() && "Could not allocate target machine!");
 
   TargetMachine &Target = *target.get();
   
   // Load the module to be compiled...
-  auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
+  std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
   if (M.get() == 0) {
     cerr << "bytecode didn't read correctly.\n";
     return 1;
   }
 
   // Build up all of the passes that we want to do to the module...
-  vector<Pass*> Passes;
+  std::vector<Pass*> Passes;
 
   // Hoist constants out of PHI nodes into predecessor BB's
   Passes.push_back(new HoistPHIConstants());
@@ -135,8 +136,15 @@
     assert(InputFilename != "-" &&
            "files on stdin not supported with tracing");
     string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
-    ostream *os = new ofstream(traceFileName.c_str(), 
-                               (Force ? 0 : ios::noreplace)|ios::out);
+
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+           << "Use -f command line argument to force output\n";
+      return 1;
+    }
+
+    std::ostream *os = new std::ofstream(traceFileName.c_str());
     if (!os->good()) {
       cerr << "Error opening " << traceFileName
            << "! SKIPPING OUTPUT OF TRACE CODE\n";
@@ -161,19 +169,31 @@
 
   if (!DoNotEmitAssembly) {                // If asm output is enabled...
     // Figure out where we are going to send the output...
-    ostream *Out = 0;
+    std::ostream *Out = 0;
     if (OutputFilename != "") {   // Specified an output filename?
-      Out = new ofstream(OutputFilename.c_str(), 
-                         (Force ? 0 : ios::noreplace)|ios::out);
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+             << "Use -f command line argument to force output\n";
+        return 1;
+      }
+      Out = new std::ofstream(OutputFilename.c_str());
     } else {
       if (InputFilename == "-") {
         OutputFilename = "-";
-        Out = &cout;
+        Out = &std::cout;
       } else {
         string OutputFilename = GetFileNameRoot(InputFilename); 
         OutputFilename += ".s";
-        Out = new ofstream(OutputFilename.c_str(), 
-                           (Force ? 0 : ios::noreplace)|ios::out);
+
+        if (!Force && !std::ifstream(OutputFilename.c_str())) {
+          // If force is not specified, make sure not to overwrite a file!
+          cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+               << "Use -f command line argument to force output\n";
+          return 1;
+        }
+
+        Out = new std::ofstream(OutputFilename.c_str());
         if (!Out->good()) {
           cerr << "Error opening " << OutputFilename << "!\n";
           delete Out;
@@ -183,7 +203,7 @@
     }
     
     // Output assembly language to the .s file
-    Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
+    Passes.push_back(new EmitAssembly(Target, Out, Out != &std::cout));
   }
   
   // Run our queue of passes all at once now, efficiently.  This form of
diff --git a/tools/llvm-as/as.cpp b/tools/llvm-as/as.cpp
index ee664f1..0fbd1e6 100644
--- a/tools/llvm-as/as.cpp
+++ b/tools/llvm-as/as.cpp
@@ -16,6 +16,7 @@
 #include "Support/CommandLine.h"
 #include <fstream>
 #include <string>
+#include <memory>
 
 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
@@ -28,46 +29,55 @@
   ostream *Out = 0;
   try {
     // Parse the file now...
-    Module *C = ParseAssemblyFile(InputFilename);
-    if (C == 0) {
+    std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename));
+    if (C.get() == 0) {
       cerr << "assembly didn't read correctly.\n";
       return 1;
     }
   
     if (DumpAsm)
-      cerr << "Here's the assembly:\n" << C;
+      cerr << "Here's the assembly:\n" << C.get();
 
     if (OutputFilename != "") {   // Specified an output filename?
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+             << "Use -f command line argument to force output\n";
+        return 1;
+      }
+      Out = new std::ofstream(OutputFilename.c_str());
     } else {
       if (InputFilename == "-") {
 	OutputFilename = "-";
 	Out = &cout;
       } else {
-	string IFN = InputFilename;
+	std::string IFN = InputFilename;
 	int Len = IFN.length();
 	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
 	  // Source ends in .ll
-	  OutputFilename = string(IFN.begin(), IFN.end()-3);
+	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
         } else {
 	  OutputFilename = IFN;   // Append a .bc to it
 	}
 	OutputFilename += ".bc";
-	Out = new ofstream(OutputFilename.c_str(), 
-			   (Force ? 0 : ios::noreplace)|ios::out);
-      }
-  
-      if (!Out->good()) {
-        cerr << "Error opening " << OutputFilename << "!\n";
-	delete C;
-	return 1;
+
+        if (!Force && !std::ifstream(OutputFilename.c_str())) {
+          // If force is not specified, make sure not to overwrite a file!
+          cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+               << "Use -f command line argument to force output\n";
+          return 1;
+        }
+
+	Out = new std::ofstream(OutputFilename.c_str());
       }
     }
+  
+    if (!Out->good()) {
+      cerr << "Error opening " << OutputFilename << "!\n";
+      return 1;
+    }
    
-    WriteBytecodeToFile(C, *Out);
-
-    delete C;
+    WriteBytecodeToFile(C.get(), *Out);
   } catch (const ParseException &E) {
     cerr << E.getMessage() << endl;
     return 1;
diff --git a/tools/llvm-as/llvm-as.cpp b/tools/llvm-as/llvm-as.cpp
index ee664f1..0fbd1e6 100644
--- a/tools/llvm-as/llvm-as.cpp
+++ b/tools/llvm-as/llvm-as.cpp
@@ -16,6 +16,7 @@
 #include "Support/CommandLine.h"
 #include <fstream>
 #include <string>
+#include <memory>
 
 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
@@ -28,46 +29,55 @@
   ostream *Out = 0;
   try {
     // Parse the file now...
-    Module *C = ParseAssemblyFile(InputFilename);
-    if (C == 0) {
+    std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename));
+    if (C.get() == 0) {
       cerr << "assembly didn't read correctly.\n";
       return 1;
     }
   
     if (DumpAsm)
-      cerr << "Here's the assembly:\n" << C;
+      cerr << "Here's the assembly:\n" << C.get();
 
     if (OutputFilename != "") {   // Specified an output filename?
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+             << "Use -f command line argument to force output\n";
+        return 1;
+      }
+      Out = new std::ofstream(OutputFilename.c_str());
     } else {
       if (InputFilename == "-") {
 	OutputFilename = "-";
 	Out = &cout;
       } else {
-	string IFN = InputFilename;
+	std::string IFN = InputFilename;
 	int Len = IFN.length();
 	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
 	  // Source ends in .ll
-	  OutputFilename = string(IFN.begin(), IFN.end()-3);
+	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
         } else {
 	  OutputFilename = IFN;   // Append a .bc to it
 	}
 	OutputFilename += ".bc";
-	Out = new ofstream(OutputFilename.c_str(), 
-			   (Force ? 0 : ios::noreplace)|ios::out);
-      }
-  
-      if (!Out->good()) {
-        cerr << "Error opening " << OutputFilename << "!\n";
-	delete C;
-	return 1;
+
+        if (!Force && !std::ifstream(OutputFilename.c_str())) {
+          // If force is not specified, make sure not to overwrite a file!
+          cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+               << "Use -f command line argument to force output\n";
+          return 1;
+        }
+
+	Out = new std::ofstream(OutputFilename.c_str());
       }
     }
+  
+    if (!Out->good()) {
+      cerr << "Error opening " << OutputFilename << "!\n";
+      return 1;
+    }
    
-    WriteBytecodeToFile(C, *Out);
-
-    delete C;
+    WriteBytecodeToFile(C.get(), *Out);
   } catch (const ParseException &E) {
     cerr << E.getMessage() << endl;
     return 1;
diff --git a/tools/llvm-dis/dis.cpp b/tools/llvm-dis/dis.cpp
index 2a7eb4e..55e8d5d 100644
--- a/tools/llvm-dis/dis.cpp
+++ b/tools/llvm-dis/dis.cpp
@@ -24,6 +24,8 @@
 #include "Support/PostOrderIterator.h"
 #include "Support/CommandLine.h"
 #include <fstream>
+#include <iostream>
+using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
 enum OutputMode {
@@ -47,7 +49,7 @@
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
   Module *C = ParseBytecodeFile(InputFilename);
   if (C == 0) {
@@ -56,31 +58,41 @@
   }
   
   if (OutputFilename != "") {   // Specified an output filename?
-    Out = new ofstream(OutputFilename.c_str(), 
-		       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename
+           << "': File exists! Sending to standard output.\n";
+    } else {
+      Out = new std::ofstream(OutputFilename.c_str());
+    }
   } else {
     if (InputFilename == "-") {
       OutputFilename = "-";
-      Out = &cout;
     } else {
-      string IFN = InputFilename;
+      std::string IFN = InputFilename;
       int Len = IFN.length();
       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
 	// Source ends in .bc
-	OutputFilename = string(IFN.begin(), IFN.end()-3);
+	OutputFilename = std::string(IFN.begin(), IFN.end()-3);
       } else {
 	OutputFilename = IFN;   // Append a .ll to it
       }
       OutputFilename += ".ll";
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename
+             << "': File exists! Sending to standard output.\n";
+      } else {
+        Out = new std::ofstream(OutputFilename.c_str());
+      }
     }
   }
 
   if (!Out->good()) {
     cerr << "Error opening " << OutputFilename
 	 << ": sending to stdout instead!\n";
-    Out = &cout;
+    Out = &std::cout;
   }
 
   // All that dis does is write the assembly out to a file... which is exactly
@@ -100,20 +112,20 @@
       switch (WriteMode) {
       case dfo:                   // Depth First ordering
 	copy(df_begin(M), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rdfo:            // Reverse Depth First ordering
 	copy(df_begin(M, true), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case po:                    // Post Order
 	copy(po_begin(M), po_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rpo: {           // Reverse Post Order
 	ReversePostOrderTraversal RPOT(M);
 	copy(RPOT.begin(), RPOT.end(),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       }
       default:
@@ -124,6 +136,6 @@
   }
   delete C;
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }
diff --git a/tools/llvm-dis/llvm-dis.cpp b/tools/llvm-dis/llvm-dis.cpp
index 2a7eb4e..55e8d5d 100644
--- a/tools/llvm-dis/llvm-dis.cpp
+++ b/tools/llvm-dis/llvm-dis.cpp
@@ -24,6 +24,8 @@
 #include "Support/PostOrderIterator.h"
 #include "Support/CommandLine.h"
 #include <fstream>
+#include <iostream>
+using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
 enum OutputMode {
@@ -47,7 +49,7 @@
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
   Module *C = ParseBytecodeFile(InputFilename);
   if (C == 0) {
@@ -56,31 +58,41 @@
   }
   
   if (OutputFilename != "") {   // Specified an output filename?
-    Out = new ofstream(OutputFilename.c_str(), 
-		       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename
+           << "': File exists! Sending to standard output.\n";
+    } else {
+      Out = new std::ofstream(OutputFilename.c_str());
+    }
   } else {
     if (InputFilename == "-") {
       OutputFilename = "-";
-      Out = &cout;
     } else {
-      string IFN = InputFilename;
+      std::string IFN = InputFilename;
       int Len = IFN.length();
       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
 	// Source ends in .bc
-	OutputFilename = string(IFN.begin(), IFN.end()-3);
+	OutputFilename = std::string(IFN.begin(), IFN.end()-3);
       } else {
 	OutputFilename = IFN;   // Append a .ll to it
       }
       OutputFilename += ".ll";
-      Out = new ofstream(OutputFilename.c_str(), 
-			 (Force ? 0 : ios::noreplace)|ios::out);
+
+      if (!Force && !std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename
+             << "': File exists! Sending to standard output.\n";
+      } else {
+        Out = new std::ofstream(OutputFilename.c_str());
+      }
     }
   }
 
   if (!Out->good()) {
     cerr << "Error opening " << OutputFilename
 	 << ": sending to stdout instead!\n";
-    Out = &cout;
+    Out = &std::cout;
   }
 
   // All that dis does is write the assembly out to a file... which is exactly
@@ -100,20 +112,20 @@
       switch (WriteMode) {
       case dfo:                   // Depth First ordering
 	copy(df_begin(M), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rdfo:            // Reverse Depth First ordering
 	copy(df_begin(M, true), df_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case po:                    // Post Order
 	copy(po_begin(M), po_end(M),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       case rpo: {           // Reverse Post Order
 	ReversePostOrderTraversal RPOT(M);
 	copy(RPOT.begin(), RPOT.end(),
-	     ostream_iterator<BasicBlock*>(*Out, "\n"));
+	     std::ostream_iterator<BasicBlock*>(*Out, "\n"));
 	break;
       }
       default:
@@ -124,6 +136,6 @@
   }
   delete C;
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }
diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp
index 83141c8..ad4cbdb 100644
--- a/tools/llvm-link/llvm-link.cpp
+++ b/tools/llvm-link/llvm-link.cpp
@@ -19,6 +19,7 @@
 #include <fstream>
 #include <memory>
 #include <sys/types.h>     // For FileExists
+typedef int blksize_t;   // SYS/TYPES is broken!!!
 #include <sys/stat.h>
 
 
@@ -32,7 +33,7 @@
 
 
 // FileExists - Return true if the specified string is an openable file...
-static inline bool FileExists(const string &FN) {
+static inline bool FileExists(const std::string &FN) {
   struct stat StatBuf;
   return stat(FN.c_str(), &StatBuf) != -1;
 }
@@ -40,9 +41,9 @@
 // LoadFile - Read the specified bytecode file in and return it.  This routine
 // searches the link path for the specified file to try to find it...
 //
-static inline std::auto_ptr<Module> LoadFile(const string &FN) {
-  string Filename = FN;
-  string ErrorMessage;
+static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
+  std::string Filename = FN;
+  std::string ErrorMessage;
 
   unsigned NextLibPathIdx = 0;
   bool FoundAFile = false;
@@ -81,7 +82,7 @@
   assert(InputFilenames.size() > 0 && "OneOrMore is not working");
 
   unsigned BaseArg = 0;
-  string ErrorMessage;
+  std::string ErrorMessage;
 
   // TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
   if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
@@ -94,7 +95,7 @@
   if (Composite.get() == 0) return 1;
 
   for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
-    auto_ptr<Module> M(LoadFile(InputFilenames[i]));
+    std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
     if (M.get() == 0) return 1;
 
     if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
@@ -111,8 +112,13 @@
 
   ostream *Out = &cout;  // Default to printing to stdout...
   if (OutputFilename != "-") {
-    Out = new ofstream(OutputFilename.c_str(), 
-		       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+           << "Use -f command line argument to force output\n";
+      return 1;
+    }
+    Out = new std::ofstream(OutputFilename.c_str());
     if (!Out->good()) {
       cerr << "Error opening '" << OutputFilename << "'!\n";
       return 1;
@@ -122,6 +128,6 @@
   if (Verbose) cerr << "Writing bytecode...\n";
   WriteBytecodeToFile(Composite.get(), *Out);
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }
diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp
index 9dfb51a..8a2208d 100644
--- a/tools/opt/opt.cpp
+++ b/tools/opt/opt.cpp
@@ -122,10 +122,16 @@
   for (unsigned i = 0; i < OptimizationList.size(); ++i)
     RunOptimization(M.get(), OptimizationList[i]);
 
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
   if (OutputFilename != "") {
-    Out = new ofstream(OutputFilename.c_str(), 
-                       (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && !std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename << "': File exists!\n"
+           << "Use -f command line argument to force output\n";
+      return 1;
+    }
+    Out = new std::ofstream(OutputFilename.c_str());
+
     if (!Out->good()) {
       cerr << "Error opening " << OutputFilename << "!\n";
       return 1;