Index: CMT/HEAD/ChangeLog
===================================================================
--- CMT/HEAD/ChangeLog	(revision 526)
+++ CMT/HEAD/ChangeLog	(revision 527)
@@ -1,2 +1,24 @@
+2009-11-20    <rybkin@lal.in2p3.fr> 414
+
+	* source/cmt_system.h: In class CmtSystem, introduce functions
+	close_ostream, close_stdout in order check whether there are file write
+	errors,	and exit with an error if that is the case
+	* source/cmt_system.cxx: Implement introduced functions, in function
+	CmtSystem::compare_and_update_files, check file stream status
+	* source/cmt_generators.cxx: Set file_access_error error if file cannot be
+	opened for writing, close file streams on writing making use of
+	close_ostream
+	* source/cmt_generator.cxx: idem
+	* source/cmt_parser.cxx: idem, in setup.[c]sh, cleanup.[c]sh scripts, check
+	CMT command exit status, on failure, print the command and exit,
+	for "cmt filter" command, set path_not_found error, when appropriate
+	* source/cmt.cxx: Close stdout with CmtSystem::close_stdout before exiting
+	the program
+	* source/cmt_string.cxx: In function cmt_string::write, check file stream
+	status
+	* src/Makefile.header: Improve a diagnostic message by using the standard
+	macros
+	* mgr/cmt_dependencies.make: Update
+	
 2009-11-13    <rybkin@lal.in2p3.fr> 413
 
Index: CMT/HEAD/mgr/cmt_dependencies.make
===================================================================
--- CMT/HEAD/mgr/cmt_dependencies.make	(revision 526)
+++ CMT/HEAD/mgr/cmt_dependencies.make	(revision 527)
@@ -29,5 +29,5 @@
 cmt_cmtpath_pattern_cxx_dependencies =  ../source/cmt_cmtpath_pattern.cxx   ../source/cmt_vector.h   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_regexp.h   ../source/cmt_system.h   ../source/cmt.h   ../source/cmt_parser.h   ../source/cmt_branch.h   ../source/cmt_fragment.h   ../source/cmt_group.h   ../source/cmt_constituent.h   ../source/cmt_language.h   ../source/cmt_map.h   ../source/cmt_pattern.h   ../source/cmt_include.h   ../source/cmt_script.h   ../source/cmt_scope_section.h   ../source/cmt_use.h   ../source/cmt_tag.h   ../source/cmt_symbol.h   ../source/cmt_awk.h   ../source/cmt_project.h   ../source/cmt_cmtpath_pattern.h   ../source/cmt_database.h   ../source/cmt_syntax.h 
 cmt_sequence_cxx_dependencies =  ../source/cmt_sequence.cxx   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_regexp.h   ../source/cmt_awk.h   ../source/cmt_sequence.h   ../source/cmt_vector.h   ../source/cmt_system.h 
-cmt_generators_cxx_dependencies =  ../source/cmt_generators.cxx   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_vector.h   ../source/cmt_regexp.h   ../source/cmt_system.h   ../source/cmt.h   ../source/cmt_parser.h   ../source/cmt_fragment.h   ../source/cmt_group.h   ../source/cmt_constituent.h   ../source/cmt_language.h   ../source/cmt_awk.h   ../source/cmt_map.h   ../source/cmt_include.h   ../source/cmt_script.h   ../source/cmt_pattern.h   ../source/cmt_scope_section.h   ../source/cmt_use.h   ../source/cmt_deps_builder.h   ../source/cmt_generator.h   ../source/cmt_generators.h   ../source/cmt_tag.h   ../source/cmt_symbol.h   ../source/cmt_log.h 
+cmt_generators_cxx_dependencies =  ../source/cmt_generators.cxx   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_vector.h   ../source/cmt_regexp.h   ../source/cmt_system.h   ../source/cmt.h   ../source/cmt_parser.h   ../source/cmt_fragment.h   ../source/cmt_group.h   ../source/cmt_constituent.h   ../source/cmt_language.h   ../source/cmt_awk.h   ../source/cmt_map.h   ../source/cmt_include.h   ../source/cmt_script.h   ../source/cmt_pattern.h   ../source/cmt_scope_section.h   ../source/cmt_use.h   ../source/cmt_deps_builder.h   ../source/cmt_generator.h   ../source/cmt_generators.h   ../source/cmt_tag.h   ../source/cmt_symbol.h   ../source/cmt_log.h   ../source/cmt_error.h 
 cmt_scope_section_cxx_dependencies =  ../source/cmt_scope_section.cxx   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_vector.h   ../source/cmt_regexp.h   ../source/cmt_system.h   ../source/cmt.h   ../source/cmt_parser.h   ../source/cmt_scope_section.h 
 cmt_log_cxx_dependencies =  ../source/cmt_log.cxx   ../source/cmt_std.h   ../source/cmt_string.h   ../source/cmt_log.h   ../source/cmt_vector.h   ../source/cmt_parser.h   ../source/cmt_regexp.h   ../source/cmt_system.h   ../source/cmt.h 
Index: CMT/HEAD/source/cmt.cxx
===================================================================
--- CMT/HEAD/source/cmt.cxx	(revision 526)
+++ CMT/HEAD/source/cmt.cxx	(revision 527)
@@ -22,4 +22,6 @@
     }
 
+  CmtSystem::close_stdout ();
+
   return (status);
 }
Index: CMT/HEAD/source/cmt_generator.cxx
===================================================================
--- CMT/HEAD/source/cmt_generator.cxx	(revision 526)
+++ CMT/HEAD/source/cmt_generator.cxx	(revision 527)
@@ -312,5 +312,6 @@
 void CmtGenerator::terminate ()
 {
-  fclose (m_output_file);
+  CmtSystem::close_ostream (m_output_file, m_output_file_name);
+  //  fclose (m_output_file);
 
   //--- Complete the operation --------------
Index: CMT/HEAD/source/cmt_generators.cxx
===================================================================
--- CMT/HEAD/source/cmt_generators.cxx	(revision 526)
+++ CMT/HEAD/source/cmt_generators.cxx	(revision 527)
@@ -10,4 +10,5 @@
 #include "cmt_symbol.h"
 #include "cmt_log.h"
+#include "cmt_error.h"
 #include <assert.h>
 
@@ -1324,4 +1325,8 @@
       terminate ();
     }
+  else
+    {
+      CmtError::set (CmtError::file_access_error, m_output_file_name);
+    }
 }
 
@@ -1557,8 +1562,13 @@
 		  */
                   s.write (file);
-                  fclose (file);
+		  CmtSystem::close_ostream (file, backup);
+		  //  fclose (file);
 
                   need_makefile = true;
                 }
+	      else
+		{
+		  CmtError::set (CmtError::file_access_error, backup);
+		}
             }
         }
@@ -1570,10 +1580,16 @@
       if (file != NULL)
         {
-          fprintf (file, "include $(CMTROOT)/src/Makefile.header\n");
-          fprintf (file, "\n");
-          fprintf (file, "include $(CMTROOT)/src/constituents.make\n");
-          fprintf (file, "\n");
-          fclose (file);
-        }
+          fprintf (file,
+		   "include $(CMTROOT)/src/Makefile.header\n"
+		   "\n"
+		   "include $(CMTROOT)/src/constituents.make\n"
+		   "\n");
+	  CmtSystem::close_ostream (file, makefile);
+	  //          fclose (file);
+        }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, makefile);
+	}
     }
 
@@ -1594,10 +1610,16 @@
       if (file != NULL)
         {
-          fprintf (file, "!include $(CMTROOT)\\src\\NMakefile.header\n");
-          fprintf (file, "\n");
-          fprintf (file, "!include $(CMTROOT)\\src\\constituents.nmake\n");
-          fprintf (file, "\n");
-          fclose (file);
-        }
+          fprintf (file,
+		   "!include $(CMTROOT)\\src\\NMakefile.header\n"
+		   "\n"
+		   "!include $(CMTROOT)\\src\\constituents.nmake\n"
+		   "\n");
+	  CmtSystem::close_ostream (file, makefile);
+	  //          fclose (file);
+        }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, makefile);
+	}
     }
 
@@ -1747,4 +1769,8 @@
       terminate ();
     }
+  else
+    {
+      CmtError::set (CmtError::file_access_error, m_output_file_name);
+    }
 
   m_output_file_name = msdevdir + "all.dspnew";
@@ -1755,4 +1781,8 @@
       dsp_all_fragment.wincopy (m_output_file, 1, &m_PACKAGE);
       terminate ();
+    }
+  else
+    {
+      CmtError::set (CmtError::file_access_error, m_output_file_name);
     }
 
@@ -2429,9 +2459,14 @@
       make_setup_fragment.copy (m_output_file, 1, &m_PACKAGE);
 
-      fclose (m_output_file);
+      CmtSystem::close_ostream (m_output_file, new_file_name);
+      //  fclose (m_output_file);
 
       //--- Complete the operation --------------
 
       commit (new_file_name);
+    }
+  else
+    {
+      CmtError::set (CmtError::file_access_error, new_file_name);
     }
 
@@ -2637,7 +2672,12 @@
       constituents_trailer_fragment.copy (m_output_file, 0);
 
-      fclose (m_output_file);
+      CmtSystem::close_ostream (m_output_file, new_file_name);
+      //  fclose (m_output_file);
 
       commit (new_file_name);
+    }
+  else
+    {
+      CmtError::set (CmtError::file_access_error, new_file_name);
     }
 }
@@ -2967,13 +3007,14 @@
   FILE* f = fopen (m_output_file_name.c_str (), "wb");
 
-  if (f == 0)
-    {
-      CmtMessage::error ("Cannot open " + m_output_file_name + " for write");
-      //      cerr << "Cannot open " << m_output_file_name << " for write" << endl;
+  if (f != 0)
+    {
+      dependencies.write (f);
+      CmtSystem::close_ostream (f, m_output_file_name);
+      //          fclose (f);
     }
   else
     {
-      dependencies.write (f);
-      fclose (f);
+      CmtError::set (CmtError::file_access_error, m_output_file_name);
+      //      CmtMessage::error ("Cannot open " + m_output_file_name + " for write");
     }
 
Index: CMT/HEAD/source/cmt_parser.cxx
===================================================================
--- CMT/HEAD/source/cmt_parser.cxx	(revision 526)
+++ CMT/HEAD/source/cmt_parser.cxx	(revision 527)
@@ -2505,5 +2505,5 @@
   //  configure_current_package ();
 
-  Generator::build_default_makefile ();
+  //  Generator::build_default_makefile ();
 
   CmtSystem::cmt_string_vector makes;
@@ -2581,4 +2581,6 @@
   install_cleanup_scripts ();
   install_native_version_file ();
+
+  Generator::build_default_makefile ();
 
   CmtSystem::cd ("..");
@@ -3199,6 +3201,6 @@
   if (!CmtSystem::test_file (input))
     {
-      CmtMessage::error ("File " + input + " not found");
-      //      cerr << "#CMT> File " << input << " not found" << endl;
+      CmtError::set (CmtError::path_not_found, input);
+      //      CmtMessage::error ("File " + input + " not found");
       return;
     }
@@ -3213,13 +3215,14 @@
 
   FILE* file = fopen (output, "wb");
-  if (file == NULL)
-    {
-      CmtMessage::error ("Cannot write filtered file " + output);
-      //      cerr << "#CMT> Cannot write filtered file " << output << endl;
+  if (file != NULL)
+    {
+      text.write (file);
+      CmtSystem::close_ostream (file, output);
+      //      fclose (file);
     }
   else
     {
-      text.write (file);
-      fclose (file);
+      CmtError::set (CmtError::file_access_error, output);
+      //      CmtMessage::error ("Cannot write filtered file " + output);
     }
 }
@@ -4812,11 +4815,23 @@
               fprintf (f, "if $status != 0 then\n  set tempfile=/tmp/cmt.$$\nendif\n");
               fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s "
-                       "-pack=%s -version=%s -path=%s $* >${tempfile}; "
-                       "source ${tempfile}\n",
+                       "-pack=%s -version=%s -path=%s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_package.c_str (),
                        version.c_str (),
                        Me.m_current_path.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+	      fprintf (f,
+		       "if ( $status == 0 ) then\n"
+		       "  source ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo \"${CMTROOT}/mgr/cmt cleanup -%s "
+                       "-pack=%s -version=%s -path=%s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "endif\n",
+                       suffix[i].c_str (),
+                       Me.m_current_package.c_str (),
+                       version.c_str (),
+                       Me.m_current_path.c_str ());
             }
           else if (mode[i] == Sh)
@@ -4829,11 +4844,23 @@
               fprintf (f, "if test ! $? = 0 ; then tempfile=/tmp/cmt.$$; fi\n");
               fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s "
-                       "-pack=%s -version=%s -path=%s $* >${tempfile}; "
-                       ". ${tempfile}\n",
+                       "-pack=%s -version=%s -path=%s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_package.c_str (),
                        version.c_str (),
                        Me.m_current_path.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+              fprintf (f,
+		       "if test $? = 0 ; then\n"
+                       "  . ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo >&2 \"${CMTROOT}/mgr/cmt cleanup -%s "
+                       "-pack=%s -version=%s -path=%s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "fi\n",
+                       suffix[i].c_str (),
+                       Me.m_current_package.c_str (),
+                       version.c_str (),
+                       Me.m_current_path.c_str ());
             }
           else if (mode[i] == Bat)
@@ -4846,5 +4873,6 @@
           fprintf (f, "\n");
 
-          fclose (f);
+          CmtSystem::close_ostream (f, file_name);
+	  //          fclose (f);
 
           cmt_string old_file_name = "cleanup";
@@ -4854,4 +4882,8 @@
           CmtSystem::compare_and_update_files (file_name, old_file_name);
         }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, file_name);
+	}
     }
 }
@@ -4932,6 +4964,5 @@
               fprintf (f, "if $status != 0 then\n  set tempfile=/tmp/cmt.$$\nendif\n");
               fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
-                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}; "
-                       "source ${tempfile}\n",
+                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_package.c_str (),
@@ -4939,5 +4970,19 @@
                        Me.m_current_path.c_str (),
 		       no_cleanup_opt.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+	      fprintf (f,
+		       "if ( $status == 0 ) then\n"
+		       "  source ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo \"${CMTROOT}/mgr/cmt setup -%s "
+                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "endif\n",
+                       suffix[i].c_str (),
+                       Me.m_current_package.c_str (),
+                       version.c_str (),
+                       Me.m_current_path.c_str (),
+		       no_cleanup_opt.c_str ());
             }
           else if (mode[i] == Sh)
@@ -4957,6 +5002,5 @@
               fprintf (f, "if test ! $? = 0 ; then tempfile=/tmp/cmt.$$; fi\n");
               fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
-                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}; "
-                       ". ${tempfile}\n",
+                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_package.c_str (),
@@ -4964,5 +5008,19 @@
                        Me.m_current_path.c_str (),
 		       no_cleanup_opt.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+              fprintf (f,
+		       "if test $? = 0 ; then\n"
+                       "  . ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo >&2 \"${CMTROOT}/mgr/cmt setup -%s "
+                       "-pack=%s -version=%s -path=%s %s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "fi\n",
+                       suffix[i].c_str (),
+                       Me.m_current_package.c_str (),
+                       version.c_str (),
+                       Me.m_current_path.c_str (),
+		       no_cleanup_opt.c_str ());
             }
           else if (mode[i] == Bat)
@@ -4978,5 +5036,6 @@
           fprintf (f, "\n");
 
-          fclose (f);
+          CmtSystem::close_ostream (f, file_name);
+	  //          fclose (f);
 
           cmt_string old_file_name = "setup";
@@ -4986,4 +5045,8 @@
           CmtSystem::compare_and_update_files (file_name, old_file_name);
         }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, file_name);
+	}
     }
 }
@@ -5049,9 +5112,18 @@
               fprintf (f, "set tempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n");
               fprintf (f, "if $status != 0 then\n  set tempfile=/tmp/cmt.$$\nendif\n");
-              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}; "
-                       "source ${tempfile}\n",
+              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_path.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+	      fprintf (f,
+		       "if ( $status == 0 ) then\n"
+		       "  source ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo \"${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "endif\n",
+                       suffix[i].c_str (),
+                       Me.m_current_path.c_str ());
             }
           else if (mode[i] == Sh)
@@ -5063,9 +5135,18 @@
               fprintf (f, "tempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n");
               fprintf (f, "if test ! $? = 0 ; then tempfile=/tmp/cmt.$$; fi\n");
-              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}; "
-                       ". ${tempfile}\n",
+              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_path.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+              fprintf (f,
+		       "if test $? = 0 ; then\n"
+                       "  . ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo >&2 \"${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "fi\n",
+                       suffix[i].c_str (),
+                       Me.m_current_path.c_str ());
             }
           else
@@ -5078,5 +5159,6 @@
           fprintf (f, "\n");
 
-          fclose (f);
+          CmtSystem::close_ostream (f, file_name);
+	  //          fclose (f);
 
           cmt_string old_file_name = "cleanup";
@@ -5086,4 +5168,8 @@
           CmtSystem::compare_and_update_files (file_name, old_file_name);
         }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, file_name);
+	}
     }
 }
@@ -5104,14 +5190,18 @@
 	    cerr << "Creating native_version file." << endl;    
 
-	  FILE* f = fopen ("new.native_version.cmt", "wb");
-
+	  cmt_string file_name ("new.native_version.cmt");
+	  FILE* f = fopen (file_name.c_str (), "wb");
 	  if (f != NULL)
 	    {
 	      fprintf (f, "%s\n", value.c_str ());
+	      CmtSystem::close_ostream (f, file_name);
+	      //          fclose (f);
+	      
+	      CmtSystem::compare_and_update_files ("new.native_version.cmt", "native_version.cmt");
 	    }      
-
-	  fclose (f);
-
-	  CmtSystem::compare_and_update_files ("new.native_version.cmt", "native_version.cmt");
+	  else
+	    {
+	      CmtError::set (CmtError::file_access_error, file_name);
+	    }
 	} 
     }    
@@ -5184,10 +5274,20 @@
               fprintf (f, "set tempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n");
               fprintf (f, "if $status != 0 then\n  set tempfile=/tmp/cmt.$$\nendif\n");
-              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}; "
-                       "source ${tempfile}\n",
+              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_path.c_str (),
 		       no_cleanup_opt.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+	      fprintf (f,
+		       "if ( $status == 0 ) then\n"
+		       "  source ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo \"${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "endif\n",
+                       suffix[i].c_str (),
+                       Me.m_current_path.c_str (),
+		       no_cleanup_opt.c_str ());
             }
           else if (mode[i] == Sh)
@@ -5203,10 +5303,20 @@
               fprintf (f, "tempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n");
               fprintf (f, "if test ! $? = 0 ; then tempfile=/tmp/cmt.$$; fi\n");
-              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}; "
-                       ". ${tempfile}\n",
+              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}\n",
                        suffix[i].c_str (),
                        Me.m_current_path.c_str (),
 		       no_cleanup_opt.c_str ());
-              fprintf (f, "/bin/rm -f ${tempfile}\n");
+              fprintf (f,
+		       "if test $? = 0 ; then\n"
+                       "  . ${tempfile}\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "else\n"
+		       "  echo >&2 \"${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${tempfile}\"\n"
+		       "  /bin/rm -f ${tempfile}\n"
+		       "  false\n"
+		       "fi\n",
+                       suffix[i].c_str (),
+                       Me.m_current_path.c_str (),
+		       no_cleanup_opt.c_str ());
             }
           else
@@ -5220,5 +5330,6 @@
           fprintf (f, "\n");
 
-          fclose (f);
+          CmtSystem::close_ostream (f, file_name);
+	  //          fclose (f);
 
           cmt_string old_file_name = "setup";
@@ -5228,4 +5339,8 @@
           CmtSystem::compare_and_update_files (file_name, old_file_name);
         }
+      else
+	{
+	  CmtError::set (CmtError::file_access_error, file_name);
+	}
     }
 }
@@ -5623,5 +5738,6 @@
     {
       int code = CmtError::get_last_error_code ();
-      if (!Me.m_quiet) CmtError::print ();
+      CmtError::print ();
+      //      if (!Me.m_quiet) CmtError::print ();
       clear ();
 
@@ -6396,5 +6512,6 @@
 	}   
       
-      if (!Me.m_quiet) CmtError::print ();
+      CmtError::print ();
+      //      if (!Me.m_quiet) CmtError::print ();
       clear ();
       return (code);
Index: CMT/HEAD/source/cmt_string.cxx
===================================================================
--- CMT/HEAD/source/cmt_string.cxx	(revision 526)
+++ CMT/HEAD/source/cmt_string.cxx	(revision 527)
@@ -759,5 +759,8 @@
     {
       write (f);
-      fclose (f);
+      if (ferror (f))
+	return (false);
+      if (fclose (f))
+	return (false);
       return (true);
     }
Index: CMT/HEAD/source/cmt_system.cxx
===================================================================
--- CMT/HEAD/source/cmt_system.cxx	(revision 526)
+++ CMT/HEAD/source/cmt_system.cxx	(revision 527)
@@ -528,5 +528,8 @@
     {
       s1.write (f);
-      fclose (f);
+      if (ferror (f))
+	return (false);
+      if (fclose (f))
+	return (false);
 
       unlink (name1);
@@ -2419,7 +2422,37 @@
 }
 
-
-
-
+//----------------------------------------------------------
+void CmtSystem::close_ostream (FILE *stream, const cmt_string& name)
+{
+  cmt_string msg ("Cannot write");
+  cmt_string n = (name != "") ? " " + name : "";
+  if (stream == NULL)
+    {
+      CmtMessage::error (msg + n);
+      exit (EXIT_FAILURE);
+    }
+
+  bool prev_fail = ferror (stream);
+  bool fclose_fail = fclose (stream);
+
+  if (prev_fail || fclose_fail)
+    {
+      int err = fclose_fail ? errno : 0;
+      cmt_string e = err ? ": " + cmt_string (strerror (err)) : "";
+      CmtMessage::error (msg + n + e);
+      exit (EXIT_FAILURE);
+    }
+}
+
+//----------------------------------------------------------
+void CmtSystem::close_stdout (void)
+{
+  /* 
+   * The idea and, to some extent, implementation of this function
+   * was borrowed from the GNU core utilities of the Free Software Foundation
+   * http://www.gnu.org/software/coreutils/
+   */
+  close_ostream (stdout, "stdout");
+}
 
 //----------------------------------------------------------
Index: CMT/HEAD/source/cmt_system.h
===================================================================
--- CMT/HEAD/source/cmt_system.h	(revision 526)
+++ CMT/HEAD/source/cmt_system.h	(revision 527)
@@ -145,4 +145,7 @@
   static cmt_string get_makefile_suffix ();
 
+  static void close_ostream (FILE *stream, const cmt_string& name = "" );
+  static void close_stdout (void);
+
 };
 
Index: CMT/HEAD/src/Makefile.header
===================================================================
--- CMT/HEAD/src/Makefile.header	(revision 526)
+++ CMT/HEAD/src/Makefile.header	(revision 527)
@@ -79,6 +79,8 @@
 	  retval=$$?; \
 	  if test $${retval} != 0; then \
-	  echo "#CMT> Error: $@: Cannot generate" >&2; \
-	  /bin/rm -f $@; fi; \
+	    test -z "$(cmtmsg)" || \
+	    echo "$(CMTMSGPREFIX)" "(Makefile.header) Cannot generate $@" >&2; \
+	    \rm -f $@; \
+	  fi; \
 	  exit $${retval}
 
