0030091: Configuration - allow cross-compilation from Linux (case sensitive filesyste...
[occt.git] / src / OSD / OSD_Process.cxx
index 9eb168b..47178c9 100644 (file)
@@ -14,7 +14,6 @@
 
 #ifndef _WIN32
 
-
 #include <OSD_Environment.hxx>
 #include <OSD_OSDError.hxx>
 #include <OSD_Path.hxx>
@@ -36,13 +35,6 @@ OSD_Process::OSD_Process(){
 }
 
 
-void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
-                        const Standard_Boolean /*ShowWindow*/)
-{
- system(cmd.ToCString());
-}
-
-
 void OSD_Process::TerminalType(TCollection_AsciiString& Name){
 TCollection_AsciiString which="TERM";
 OSD_Environment term (which,"");
@@ -85,18 +77,10 @@ Standard_Integer OSD_Process::ProcessId(){
  return (getpid());
 }
 
-
-Standard_Integer OSD_Process::UserId(){
- return (getuid());
-}
-
-
-TCollection_AsciiString OSD_Process::UserName(){
- struct passwd *infos;
- infos = getpwuid(getuid()); 
- TCollection_AsciiString result=infos->pw_name;
-
- return(result);
+TCollection_AsciiString OSD_Process::UserName()
+{
+  struct passwd *anInfos = getpwuid (getuid());
+  return TCollection_AsciiString (anInfos ? anInfos->pw_name : "");
 }
 
 Standard_Boolean OSD_Process::IsSuperUser (){
@@ -190,9 +174,6 @@ Standard_Integer OSD_Process::Error()const{
 #endif
 #include <windows.h>
 
-#ifdef SetCurrentDirectory
-# undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
-#endif
 #include <OSD_Process.hxx>
 
 #include <OSD_Path.hxx>
@@ -200,56 +181,19 @@ Standard_Integer OSD_Process::Error()const{
 #include <Standard_PExtCharacter.hxx>
 #include <TCollection_ExtendedString.hxx>
 
-#include <OSD_WNT_1.hxx>
-#include <LMCONS.H> /// pour UNLEN  ( see MSDN about GetUserName() )
-
-
-#pragma warning( disable : 4700 )
+#include <OSD_WNT.hxx>
+#include <lmcons.h> // for UNLEN - maximum user name length GetUserName()
 
 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
 
-OSD_Process :: OSD_Process () {
-
-}  // end constructor
-
-void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
-                           const Standard_Boolean ShowWindow /* = Standard_True */) {
-
- STARTUPINFO         si;
- PROCESS_INFORMATION pi;
-
- ZeroMemory (  &si, sizeof ( STARTUPINFO )  );
-
- si.cb = sizeof ( STARTUPINFO );
- //============================================
- //---> Added by Stephane Routelous ( stephane.routelous@altavista.net )       [16.03.01]
- //---> Reason : to allow to hide the window
- if ( !ShowWindow )
- {
-        si.dwFlags             = STARTF_USESHOWWINDOW;
-        si.wShowWindow = SW_HIDE;      
- }
- //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net )   [16.03.01]
- //============================================
-
- if (!CreateProcess (
-      NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
-                    )
- )
-
-  _osd_wnt_set_error ( myError, OSD_WProcess );
-
- else {
-  CloseHandle ( pi.hThread );
-
-  WaitForSingleObject ( pi.hProcess, INFINITE );
-
-  CloseHandle ( pi.hProcess );
- }  // end else
-
-}  // end OSD_Process :: Spawn
+// =======================================================================
+// function : OSD_Process
+// purpose  :
+// =======================================================================
+OSD_Process::OSD_Process()
+{
+  //
+}
 
 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
 
@@ -272,52 +216,29 @@ Quantity_Date OSD_Process :: SystemDate () {
 
 }  // end OSD_Process :: SystemDate
 
-Standard_Integer OSD_Process :: UserId () {
-
- PSID         retVal        = NULL;
- HANDLE       hProcessToken = INVALID_HANDLE_VALUE;
- PTOKEN_OWNER pTKowner      = NULL;
-
- if (  !OpenProcessToken (
-         GetCurrentProcess (),
-         TOKEN_QUERY, &hProcessToken
-        ) ||
-        (  pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx (
-                                       hProcessToken, TokenOwner
-                                      )
-        ) == NULL ||
-        (  retVal   = CopySidEx ( pTKowner -> Owner )  ) == NULL
- )
-
-  _osd_wnt_set_error ( myError, OSD_WProcess );
-
- if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
- if ( pTKowner      != NULL                 ) FreeTokenInformation ( pTKowner );
-
- return ( Standard_Integer )retVal;
-
-}  // end OSD_Process :: UserId
-
-TCollection_AsciiString OSD_Process :: UserName () 
+// =======================================================================
+// function : UserName
+// purpose  :
+// =======================================================================
+TCollection_AsciiString OSD_Process::UserName()
 {
-       Standard_PCharacter pBuff = new char[UNLEN + 1];
-       DWORD                   dwSize = UNLEN + 1;
-       TCollection_AsciiString retVal;
-       if (  !GetUserName ( pBuff, &dwSize )  )
-       {
-               _osd_wnt_set_error ( myError, OSD_WProcess );
-       }
-       else
-       {
-               TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
-               retVal = theTmpUserName;
-       }
-       delete [] pBuff;
-       return retVal;
-}  // end OSD_Process :: UserName
+#ifndef OCCT_UWP
+  wchar_t aUserName[UNLEN + 1];
+  DWORD aNameSize = UNLEN + 1;
+  TCollection_AsciiString retVal;
+  if (!GetUserNameW (aUserName, &aNameSize))
+  {
+    _osd_wnt_set_error(myError, OSD_WProcess);
+    return TCollection_AsciiString();
+  }
+  return TCollection_AsciiString (aUserName);
+#else
+  return TCollection_AsciiString();
+#endif
+}
 
 Standard_Boolean OSD_Process :: IsSuperUser () {
-
+#ifndef OCCT_UWP
  Standard_Boolean retVal = FALSE;
  PSID             pSIDadmin;
  HANDLE           hProcessToken = INVALID_HANDLE_VALUE;
@@ -354,34 +275,46 @@ Standard_Boolean OSD_Process :: IsSuperUser () {
  if ( pTKgroups     != NULL                 ) FreeTokenInformation ( pTKgroups );
 
  return retVal;
-
+#else
+ return FALSE;
+#endif
 }  // end OSD_Process :: IsSuperUser
 
-Standard_Integer OSD_Process :: ProcessId () {
-
- return ( Standard_Integer )GetCurrentProcessId ();
-
-}  // end OSD_Process :: ProcessId
-
-OSD_Path OSD_Process :: CurrentDirectory () {
+// =======================================================================
+// function : ProcessId
+// purpose  :
+// =======================================================================
+Standard_Integer OSD_Process::ProcessId()
+{
+  return (Standard_Integer )GetCurrentProcessId();
+}
 
+// =======================================================================
+// function : CurrentDirectory
+// purpose  :
+// =======================================================================
+OSD_Path OSD_Process::CurrentDirectory()
+{
   OSD_Path anCurrentDirectory;
-
-  DWORD dwSize = PATHLEN + 1;
-  Standard_WideChar* pBuff = new wchar_t[dwSize];
-
-  if ( GetCurrentDirectoryW(dwSize, (wchar_t*)pBuff) > 0 )
+#ifndef OCCT_UWP
+  const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL);
+  if (aBuffLen > 0)
   {
-    // conversion to UTF-8 is performed inside
-    TCollection_AsciiString aPath(TCollection_ExtendedString((Standard_ExtString)pBuff));
-    anCurrentDirectory = OSD_Path ( aPath );
+    wchar_t* aBuff = new wchar_t[aBuffLen + 1];
+    GetCurrentDirectoryW (aBuffLen, aBuff);
+    aBuff[aBuffLen] = L'\0';
+    const TCollection_AsciiString aPath (aBuff);
+    delete[] aBuff;
+
+    anCurrentDirectory = OSD_Path (aPath);
   }
   else
-    _osd_wnt_set_error ( myError, OSD_WProcess );
-  delete[] pBuff;
+  {
+    _osd_wnt_set_error (myError, OSD_WProcess);
+  }
+#endif
   return anCurrentDirectory;
-}  // end OSD_Process :: CurrentDirectory
+}
 
 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
 
@@ -390,7 +323,7 @@ void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
  where.SystemName ( path );
  TCollection_ExtendedString pathW(path);
 
- if (   !::SetCurrentDirectoryW ( (const wchar_t*) pathW.ToExtString ()  )   )
+ if (!::SetCurrentDirectoryW (pathW.ToWideString()))
 
   _osd_wnt_set_error ( myError, OSD_WProcess );