0022651: Impossible to build OCC as static library due to using Standard_EXPORT inste...
[occt.git] / src / OSD / OSD_Process.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 8b01231..8742261
@@ -1,51 +1,40 @@
+// Copyright (c) 1998-1999 Matra Datavision
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+#ifndef _WIN32
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#ifndef WNT
-
-#include <OSD_Process.ixx>
-#include <OSD_WhoAmI.hxx>
 #include <OSD_Environment.hxx>
+#include <OSD_OSDError.hxx>
+#include <OSD_Path.hxx>
+#include <OSD_Process.hxx>
+#include <OSD_WhoAmI.hxx>
+#include <Quantity_Date.hxx>
+#include <TCollection_AsciiString.hxx>
 
 const OSD_WhoAmI Iam = OSD_WProcess;
 
 #include <errno.h>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #include <stdlib.h>
-
-#ifdef HAVE_SYS_PARAM_H
-# include <sys/param.h>
-#endif
-
-#if defined(HAVE_TIME_H) || defined(WNT)
-# include <time.h>
-#endif
-
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
-
-#ifdef HAVE_PWD_H
-# include <pwd.h>       // For command getpwuid
-#endif
+#include <sys/param.h>
+#include <sys/time.h>
+#include <pwd.h>       // For command getpwuid
+#include <unistd.h>
 
 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,"");
@@ -88,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 (){
@@ -193,68 +174,30 @@ 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>
 #include <Quantity_Date.hxx>
+#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 ) {
 
- Name = TEXT( "WIN32 console" );
+ Name = "WIN32 console";
 
 }  // end OSD_Process :: TerminalType
 
@@ -273,56 +216,33 @@ 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;
- PTOKEN_GROUPS    pTKgroups;
+ PTOKEN_GROUPS    pTKgroups = NULL;
 
  if (  !OpenProcessToken (
          GetCurrentProcess (),
@@ -355,51 +275,55 @@ 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 () {
-
- Standard_PCharacter pBuff;
- DWORD            dwSize = 0;
- OSD_Path         retVal;
-
- dwSize = GetCurrentDirectory ( dwSize, pBuff );
- pBuff  = new Standard_Character[ dwSize ];
-
- if (   (  dwSize = GetCurrentDirectory ( dwSize, pBuff )  ) == NULL   )
-
-  _osd_wnt_set_error ( myError, OSD_WProcess );
-
- else
-  retVal = OSD_Path ( pBuff );
- delete[] pBuff;
+// =======================================================================
+// function : ProcessId
+// purpose  :
+// =======================================================================
+Standard_Integer OSD_Process::ProcessId()
+{
+  return (Standard_Integer )GetCurrentProcessId();
+}
 
- return retVal;
-}  // end OSD_Process :: CurrentDirectory
+// =======================================================================
+// function : CurrentDirectory
+// purpose  :
+// =======================================================================
+OSD_Path OSD_Process::CurrentDirectory()
+{
+  OSD_Path anCurrentDirectory;
+#ifndef OCCT_UWP
+  const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL);
+  if (aBuffLen > 0)
+  {
+    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);
+  }
+#endif
+  return anCurrentDirectory;
+}
 
 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
 
-#ifdef UNICODE
-# define SetCurrentDirectory  SetCurrentDirectoryW
-#else
-# define SetCurrentDirectory  SetCurrentDirectoryA
-#endif  // UNICODE
-
  TCollection_AsciiString path;
 
  where.SystemName ( path );
+ TCollection_ExtendedString pathW(path);
 
- if (   !::SetCurrentDirectory (  path.ToCString ()  )   )
+ if (!::SetCurrentDirectoryW (pathW.ToWideString()))
 
   _osd_wnt_set_error ( myError, OSD_WProcess );