Index: CMT/HEAD/ChangeLog
===================================================================
--- CMT/HEAD/ChangeLog	(revision 591)
+++ CMT/HEAD/ChangeLog	(revision 592)
@@ -1,2 +1,11 @@
+2011-11-30    <rybkin@lal.in2p3.fr> 469
+
+	* source/cmt_system.cxx: In class CmtSystem functions, check return value
+	of system functions getcwd, chdir, stat, unlink, symlink, rmdir, mkdir,
+	tmpnam,	putenv, as well as of cmt_string class read function and propagate
+	it via return value, or via setting error, and/or by printing error message
+	* mgr/fragments/dependencies: Remove dependencies Makefile (and stamps)
+	for constituent clean target
+	
 2011-11-29    <rybkin@lal.in2p3.fr> 468
 
Index: CMT/HEAD/mgr/fragments/dependencies
===================================================================
--- CMT/HEAD/mgr/fragments/dependencies	(revision 591)
+++ CMT/HEAD/mgr/fragments/dependencies	(revision 592)
@@ -11,3 +11,6 @@
 endif
 endif
+
+${CONSTITUENT}clean ::
+	$(cleanup_silent) \rm -rf $(bin)${CONSTITUENT}_deps $(bin)${CONSTITUENT}_dependencies.make
 #-- end of dependencies -------------------
Index: CMT/HEAD/source/cmt_system.cxx
===================================================================
--- CMT/HEAD/source/cmt_system.cxx	(revision 591)
+++ CMT/HEAD/source/cmt_system.cxx	(revision 592)
@@ -56,5 +56,9 @@
   char dir[PATH_MAX];
   //  char dir[256];
-  getcwd (dir, sizeof (dir));
+  if (NULL == ::getcwd (dir, sizeof (dir)))
+    {
+      perror ("getcwd failure");
+      return NULL;
+    }
   strcpy ((char*) name, dir);
   return ((char*) name);
@@ -72,5 +76,5 @@
   char buffer[PATH_MAX] = "";
   //  char buffer[256] = "";
-  char* ptr = 0;
+  //  char* ptr = 0;
   char* pwd_env = 0;
 
@@ -85,5 +89,10 @@
   else
     {
-      ptr = getcwd (buffer, sizeof (buffer));
+      if (NULL == ::getcwd (buffer, sizeof (buffer)))
+	{
+	  perror ("getcwd failure");
+	  static const cmt_string empty;
+	  return empty;
+	}
     }
 
@@ -100,4 +109,5 @@
     {
       cmt_string d = CmtSystem::pwd ();
+      if (0 == d.size ()) return false;
       d += file_separator ();
       d += dir;
@@ -116,10 +126,21 @@
   compress_path (s_dir);
 
-  if (chdir (s_dir.c_str ()) == 0) 
-    {
-      putenv ("PWD", s_dir);
-      return (true);
-    }
-
+  if (::chdir (s_dir.c_str ()) == 0) 
+    {
+      if (putenv ("PWD", s_dir))
+	return (true);
+      else
+	return (false);
+    }
+  else
+    {
+      if (errno != ENOENT
+#ifndef WIN32
+	  && errno != ENOTDIR
+#endif
+	  )
+	perror ("chdir failure: " + s_dir);
+      return (false);
+    }
   return (false);
 }
@@ -259,12 +280,41 @@
 {
   cmt_string here = CmtSystem::pwd ();
+  if (0 == here.size ()) return false;
   if  (test_directory(path))
     {
-      CmtSystem::cd (path);
+      if (!CmtSystem::cd (path)) return false;
       result = CmtSystem::pwd ();
-      CmtSystem::cd (here);
+      if (0 == result.size ()) return false;
+      if (!CmtSystem::cd (here))
+	{
+	  result.erase (0);
+	  return false;
+	}
       return true;
     }
   return false;
+}
+
+//--------------------------------------------------
+static cmt_string get_full_path (const cmt_string& name)
+{
+  cmt_string path;
+  if (CmtSystem::absolute_path (name)) return name;
+  else
+    {
+      char buffer[PATH_MAX];
+      if (NULL == ::getcwd (buffer, sizeof (buffer)))
+	{
+	  perror ("getcwd failure");
+	  path = name;
+	}
+      else
+	{
+	  path += buffer;
+	  path += CmtSystem::file_separator ();
+	  path += name;
+	}
+    }
+  return path;
 }
 
@@ -275,15 +325,19 @@
     {
       cmt_string here = CmtSystem::pwd ();
-      CmtSystem::cd (path);
+      if (0 == here.size ()) return false;
+      if (!CmtSystem::cd (path)) return false;
       //
       char buffer[PATH_MAX] = "";
       //      char buffer[256] = "";
-      char* ptr = 0;
-      ptr = getcwd (buffer, sizeof (buffer));
+      if (NULL == ::getcwd (buffer, sizeof (buffer)))
+	{
+	  perror ("getcwd failure: " + get_full_path (path));
+	  return false;
+	}
+      if (!CmtSystem::cd (here)) return false;
       const char* t = &buffer[0];
       result = t;
       //      cerr << "realpath_> path: " << path << " result: " << result << endl;
       //
-      CmtSystem::cd (here);
       return true;
     }
@@ -347,5 +401,5 @@
   int status;
 
-  status = stat (name.c_str (), &file_stat);
+  status = ::stat (name.c_str (), &file_stat);
 
   //cerr << "status(stat) for " << name << " : " << status << " st_mode=" << file_stat.st_mode << endl;
@@ -364,4 +418,9 @@
   else
     {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name));
+	  CmtError::set (CmtError::file_access_error, get_full_path (name));
+	}
       return (false);
     }
@@ -374,5 +433,5 @@
   int status;
 
-  status = stat (name.c_str (), &file_stat);
+  status = ::stat (name.c_str (), &file_stat);
 
   if (status == 0)
@@ -389,4 +448,9 @@
   else
     {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name));
+	  CmtError::set (CmtError::file_access_error, get_full_path (name));
+	}
       return (false);
     }
@@ -412,4 +476,9 @@
   else
     {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name1));
+	  CmtError::set (CmtError::file_access_error, get_full_path (name1));
+	}
       return (false);
     }
@@ -426,4 +495,9 @@
   else
     {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name2));
+	  CmtError::set (CmtError::file_access_error, get_full_path (name2));
+	}
       return (false);
     }
@@ -437,6 +511,14 @@
   static cmt_string s2;
 
-  s1.read (name1);
-  s2.read (name2);
+  if (!s1.read (name1))
+    {
+      CmtError::set (CmtError::file_access_error, name1);
+      return false;
+    }
+  if (!s2.read (name2))
+    {
+      CmtError::set (CmtError::file_access_error, name2);
+      return false;
+    }
 
   return ((s1 == s2));
@@ -453,5 +535,9 @@
     {
       static cmt_string s;
-      s.read (name);
+      if (!s.read (name))
+	{
+	  CmtError::set (CmtError::file_access_error, name);
+	  return false;
+	}
       //unlink  (name);            
       FILE* f = fopen (name, "wb");
@@ -495,5 +581,5 @@
   int status;
 
-  status = stat (name1.c_str (), &file_stat1);
+  status = ::stat (name1.c_str (), &file_stat1);
 
   if (status == 0)
@@ -507,11 +593,17 @@
   else
     {
+      if (errno != ENOENT)
+	perror ("stat failure: " + get_full_path (name1));
       // name1 does not exist
       return (false);
     }
 
-  s1.read (name1);
-
-  status = stat (name2.c_str (), &file_stat2);
+  if (!s1.read (name1))
+    {
+      CmtError::set (CmtError::file_access_error, name1);
+      return false;
+    }
+
+  status = ::stat (name2.c_str (), &file_stat2);
 
   if (status == 0)
@@ -525,11 +617,30 @@
       if (((int) file_stat1.st_size) == ((int) file_stat2.st_size))
         {
-          s2.read (name2);
+          if (!s2.read (name2))
+	    {
+	      CmtError::set (CmtError::file_access_error, name2);
+	      return false;
+	    }
           if (s1 == s2)
             {
-              unlink (name1);
-              return (true);
+              if (0 == ::unlink (name1))
+		return (true);
+	      else
+		{
+		  if (errno != ENOENT)
+		    perror ("unlink failure: " + get_full_path (name1));
+		  return (false);
+		}
             }
         }
+    }
+  else
+    {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name2));
+	  return (false);
+	}
+      // name2 does not exist
     }
 
@@ -543,7 +654,15 @@
 	return (false);
 
-      unlink (name1);
-
-      return (true);
+      if (0 == ::unlink (name1))
+	return (true);
+      else
+	{
+	  if (errno != ENOENT)
+	    {
+	      perror ("unlink failure: " + get_full_path (name1));
+	      return (false);
+	    }
+	  return (true);
+	}
     }
   else
@@ -563,5 +682,5 @@
   int status;
 
-  status = stat (name.c_str (), &file_stat);
+  status = ::stat (name.c_str (), &file_stat);
 
   if (status == 0)
@@ -571,4 +690,9 @@
   else
     {
+      if (errno != ENOENT)
+	{
+	  perror ("stat failure: " + get_full_path (name));
+	  return (-1);
+	}
       return (0);
     }
@@ -657,6 +781,11 @@
                                 const cmt_string& newname)
 {
-  ::unlink (newname.c_str ());
-
+  if (-1 == ::unlink (newname)
+      && errno != ENOENT)
+    {
+      perror ("unlink failure: " + get_full_path (newname));
+      return (false);
+    }
+  
 #ifdef WIN32
   int status = 1;
@@ -666,5 +795,11 @@
 
   if (status == 0) return (true);
-  return (false);
+  else
+    {
+#ifndef WIN32
+      perror ("symlink failure: " + newname + "->" + oldname);
+#endif
+      return (false);
+    }
 }
 
@@ -672,12 +807,16 @@
 bool CmtSystem::remove_file (const cmt_string& name)
 {
-  if (::unlink (name) != 0)
+  if (::unlink (name) == 0)
+    {
+      return (true);
+    }
+  else
     {
       //      CmtMessage::error ("Cannot remove file " + name);
       //      cerr << "#CMT> Cannot remove file " << name << endl;
+      if (errno != ENOENT)
+	perror ("unlink failure: " + get_full_path (name));
       return (false);
     }
-
-  return (true);
 }
 
@@ -710,4 +849,5 @@
   if (status != 0)
     {
+      perror ("rmdir failure: " + name);
       char num[32]; sprintf (num, "%d", errno);
       CmtMessage::error ("Cannot remove directory " + name + " errno=" + num);
@@ -768,15 +908,12 @@
 #ifdef WIN32
           if (::_mkdir (full_path.c_str ()) != 0)
+#else
+          if (::mkdir (full_path.c_str (), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0)
+#endif
             {
 	      // cerr << "CMT> cannot create directory " << full_path << endl;
+	      perror ("mkdir failure: " + full_path);
               return (false);
             }
-#else
-          if (::mkdir (full_path.c_str (), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0)
-            {
-	      // cerr << "CMT> cannot create directory " << full_path << endl;
-              return (false);
-            }
-#endif
         }
     }
@@ -1174,5 +1311,5 @@
   else
     {
-      perror ("uname");
+      perror ("uname failure");
     }
 
@@ -1220,9 +1357,16 @@
 cmt_string CmtSystem::get_temporary_name ()
 {
-  cmt_string name;
-
-  name = ::tmpnam (NULL);
-
-  return (name);
+  //  cmt_string name;
+  char * ptr;
+  if ((ptr = ::tmpnam (NULL)) == NULL)
+    {
+      perror ("tmpnam failure");
+      static const cmt_string empty;
+      return empty;
+    }
+  //  name = ::tmpnam (NULL);
+
+  return ptr;
+  //  return (name);
 }
 
@@ -1349,5 +1493,13 @@
 
     if (status == 0) return (true);
-    else return (false);
+    else
+      {
+#ifdef WIN32
+	CmtMessage::error ("putenv failure: " + v);
+#else
+	perror ("putenv failure: " + v);
+#endif
+	return (false);
+      }
   }
 
@@ -1494,5 +1646,9 @@
   static cmt_string text;
 
-  text.read (file_name);
+  if (!text.read (file_name))
+    {
+      CmtError::set (CmtError::file_access_error, file_name);
+      return;
+    }
 
   int pos = text.find ("CMTPATH");
@@ -1685,5 +1841,5 @@
       if (status == -1) // error reported by pclose ()
 	{
-	  perror ("pclose");
+	  perror ("pclose failure");
 	  if (errno == ECHILD)
 	    {
@@ -2509,7 +2665,7 @@
 {
 #ifdef _WIN32
-  cmt_string result = getenv ("USERNAME");
+  cmt_string result = ::getenv ("USERNAME");
 #else
-  cmt_string result = getenv ("USER");
+  cmt_string result = ::getenv ("USER");
 #endif
 
@@ -2861,4 +3017,2 @@
   return (in (o));
 }
-
-
