Index: CMT/HEAD/ChangeLog
===================================================================
--- CMT/HEAD/ChangeLog	(revision 597)
+++ CMT/HEAD/ChangeLog	(revision 598)
@@ -1,2 +1,12 @@
+2011-12-11    <rybkin@lal.in2p3.fr> 475
+
+	* source/cmt_error.h: In class CmtError, introduce system_error code
+	* source/cmt_error.cxx: In class TheError, in constructor add introduced
+	system_error code string name
+	* source/cmt_parser.cxx: In class Cmt, in function do_create, check return
+	value of all invoked functions, set error on failure making use of
+	system_error code, and return, so that exit value of "cmt create" command
+	be set according to command success/failure
+
 2011-12-11    <rybkin@lal.in2p3.fr> 474
 
Index: CMT/HEAD/source/cmt_error.cxx
===================================================================
--- CMT/HEAD/source/cmt_error.cxx	(revision 597)
+++ CMT/HEAD/source/cmt_error.cxx	(revision 598)
@@ -30,4 +30,5 @@
     error_names.add() = "version conflict";
     error_names.add() = "file access error";
+    error_names.add() = "system error";
     error_names.add() = "configuration error";
     error_names.add() = "execution error";
Index: CMT/HEAD/source/cmt_error.h
===================================================================
--- CMT/HEAD/source/cmt_error.h	(revision 597)
+++ CMT/HEAD/source/cmt_error.h	(revision 598)
@@ -31,4 +31,5 @@
     version_conflict,
     file_access_error,
+    system_error,
     configuration_error,
     execution_error,
Index: CMT/HEAD/source/cmt_parser.cxx
===================================================================
--- CMT/HEAD/source/cmt_parser.cxx	(revision 597)
+++ CMT/HEAD/source/cmt_parser.cxx	(revision 598)
@@ -3386,4 +3386,9 @@
 
   the_path = CmtSystem::pwd ();
+  if (0 == the_path.size ())
+    {
+      CmtError::set (CmtError::system_error, "pwd empty");
+      return;
+    }
 
   if (offset != "")
@@ -3420,6 +3425,7 @@
       if (!CmtSystem::mkdir (the_path))
         {
-          CmtMessage::error ("Cannot create the path directory");
-	  //          cout << "Cannot create the path directory" << endl;
+	  CmtError::set (CmtError::system_error,
+			 "Cannot create path directory " + the_path);
+          //CmtMessage::error ("Cannot create path directory " + the_path);
           return;
         }
@@ -3427,9 +3433,14 @@
         {
 	  if (CmtMessage::active (Info))
-	    cout << "Installing the path directory" << endl;
-        }
-    }
-
-  CmtSystem::cd (the_path);
+	    cout << "Installing path directory " << the_path << endl;
+        }
+    }
+
+  if (!CmtSystem::cd (the_path))
+    {
+      CmtError::set (CmtError::system_error,
+		     "Cannot cd to path directory " + the_path);
+      return;
+    }
 
   if (!CmtSystem::test_directory (package))
@@ -3437,6 +3448,7 @@
       if (!CmtSystem::mkdir (package))
         {
-          CmtMessage::error ("Cannot create the package directory");
-	  //          cout << "Cannot create the package directory" << endl;
+	  CmtError::set (CmtError::system_error,
+			 "Cannot create package directory " + package);
+	  //          CmtMessage::error ("Cannot create the package directory");
           return;
         }
@@ -3444,5 +3456,5 @@
         {
 	  if (CmtMessage::active (Info))
-	    cout << "Installing the package directory" << endl;
+	    cout << "Installing package directory" << endl;
         }
     }
@@ -3453,5 +3465,11 @@
     }
 
-  CmtSystem::cd (package);
+  if (!CmtSystem::cd (package))
+    {
+      CmtError::set (CmtError::system_error,
+		     "Cannot cd to package directory " + package);
+      return;
+    }
+  //  CmtSystem::cd (package);
 
   Use& current_use = Use::current ();
@@ -3485,6 +3503,7 @@
           if (!CmtSystem::mkdir (version))
             {
-              CmtMessage::error ("Cannot create the version directory");
-	      //              cout << "Cannot create the version directory" << endl;
+              //CmtMessage::error ("Cannot create the version directory");
+	      CmtError::set (CmtError::system_error,
+			     "Cannot create version directory " + version);
               return;
             }
@@ -3492,5 +3511,5 @@
             {
 	      if (CmtMessage::active (Info))
-		cout << "Installing the version directory" << endl;
+		cout << "Installing version directory" << endl;
             }
         }
@@ -3501,5 +3520,11 @@
         }
 
-      CmtSystem::cd (version);
+      if (!CmtSystem::cd (version))
+	{
+	  CmtError::set (CmtError::system_error,
+			 "Cannot cd to version directory " + version);
+	  return;
+	}
+      //      CmtSystem::cd (version);
     }
   else
@@ -3515,6 +3540,7 @@
           if (!CmtSystem::mkdir ("cmt"))
             {
-	      CmtMessage::error ("Cannot create the cmt directory");
-//              cout << "Cannot create the cmt directory" << endl;
+	      CmtError::set (CmtError::system_error,
+			     "Cannot create cmt directory");
+	      //CmtMessage::error ("Cannot create the cmt directory");
               return;
             }
@@ -3533,5 +3559,5 @@
 
 	      if (CmtMessage::active (Info))
-		cout << "Installing the cmt directory" << endl;
+		cout << "Installing cmt directory" << endl;
             }
         }
@@ -3576,6 +3602,7 @@
       if (!CmtSystem::mkdir ("src"))
         {
-          CmtMessage::error ("Cannot create the src directory");
-	  //          cout << "Cannot create the src directory" << endl;
+	  CmtError::set (CmtError::system_error,
+			 "Cannot create src directory");
+          //CmtMessage::error ("Cannot create the src directory");
           return;
         }
@@ -3583,5 +3610,5 @@
         {
 	  if (CmtMessage::active (Info))
-	    cout << "Installing the src directory" << endl;
+	    cout << "Installing src directory" << endl;
         }
     }
@@ -3596,8 +3623,20 @@
     case cmt_style:
       //    case no_version_style:
-      CmtSystem::cd ("cmt");
+      if (!CmtSystem::cd ("cmt"))
+	{
+	  CmtError::set (CmtError::system_error,
+			 "Cannot cd to package cmt directory ");
+	  return;
+	}
+      //CmtSystem::cd ("cmt");
       break;
     case mgr_style:
-      CmtSystem::cd ("mgr");
+      if (!CmtSystem::cd ("mgr"))
+	{
+	  CmtError::set (CmtError::system_error,
+			 "Cannot cd to package mgr directory ");
+	  return;
+	}
+      //CmtSystem::cd ("mgr");
       break;
     }
@@ -3609,10 +3648,20 @@
       // create an empty requirement file.
       ofstream f ("requirements");
-      if (f)
-        {
+      if (!f)
+	{
+	  CmtError::set (CmtError::file_access_error, "requirements");
+	  return;
+	}
+      f.exceptions (ios::failbit | ios::badbit);
+      try
+	{
           f << "package " << package << endl;
           f << endl;
-          f.close ();
-        }
+	  f.close (); // ios_base::failbit
+	}
+      catch (const ios::failure& e)
+	{
+	  CmtSystem::close_ostream (NULL, "requirements: " + cmt_string (e.what ()));
+	}
     }
 
@@ -3621,9 +3670,21 @@
       //      ofstream f ("version.cmt");
       ofstream f (Package::get_version_file_name ());
-      if (f)
-        {
+      if (!f)
+	{
+	  CmtError::set (CmtError::file_access_error,
+			 Package::get_version_file_name ());
+	  return;
+	}
+      f.exceptions (ios::failbit | ios::badbit);
+      try
+	{
           f << version << endl;
-          f.close ();
-        }
+	  f.close (); // ios_base::failbit
+	}
+      catch (const ios::failure& e)
+	{
+	  CmtSystem::close_ostream (NULL, Package::get_version_file_name () +
+				    + ": " + cmt_string (e.what ()));
+	}
     }
 
@@ -3632,4 +3693,9 @@
   Me.m_current_path    = the_path;
   Me.m_current_dir     = CmtSystem::pwd ();
+  if (0 == Me.m_current_dir.size ())
+    {
+      CmtError::set (CmtError::system_error, "pwd empty");
+      return;
+    }
 
   do_config (ap);
