1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <OSD_Process.ixx>
18 #include <OSD_WhoAmI.hxx>
19 #include <OSD_Environment.hxx>
21 const OSD_WhoAmI Iam = OSD_WProcess;
25 #include <sys/param.h>
27 #include <pwd.h> // For command getpwuid
30 OSD_Process::OSD_Process(){
34 void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
35 const Standard_Boolean /*ShowWindow*/)
37 system(cmd.ToCString());
41 void OSD_Process::TerminalType(TCollection_AsciiString& Name){
42 TCollection_AsciiString which="TERM";
43 OSD_Environment term (which,"");
51 // Get date of system date
53 Quantity_Date OSD_Process::SystemDate(){
55 Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
58 struct timezone tzone;
61 status = gettimeofday( &tval, &tzone );
62 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
64 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
66 month = transfert.tm_mon + 1; // Add to January (month #1)
67 day = transfert.tm_mday;
68 year = transfert.tm_year;
69 hh = transfert.tm_hour;
70 mn = transfert.tm_min ;
71 ss = transfert.tm_sec ;
74 result.SetValues ( month, day, year+1900, hh, mn, ss);
79 Standard_Integer OSD_Process::ProcessId(){
84 Standard_Integer OSD_Process::UserId(){
89 TCollection_AsciiString OSD_Process::UserName(){
91 infos = getpwuid(getuid());
92 TCollection_AsciiString result=infos->pw_name;
97 Standard_Boolean OSD_Process::IsSuperUser (){
99 return Standard_False;
102 return Standard_True;
107 OSD_Path OSD_Process::CurrentDirectory(){
108 char cwd[MAXPATHLEN+1] ;
110 TCollection_AsciiString Name;
112 if (!getcwd(cwd,MAXPATHLEN+1))
113 myError.SetValue (errno, Iam, "Where");
117 // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
118 // position = Name.SearchFromEnd(".");
119 // if (position != -1){
121 // Ext.Remove(1,position);
122 // Name.Remove( position,Ext.Length()+1);
124 // result.SetValues("","","","","",Name,Ext);
127 #if defined(vax) || defined(__vms)
128 Standard_Integer iDisk = Name.Search(":");
130 TCollection_AsciiString Disk;
131 TCollection_AsciiString Directory;
132 Disk = Name.SubString(1,iDisk-1);
133 Directory = Name.SubString(iDisk+1,Name.Length());
134 result.SetValues("","","",Disk,Directory,"","");
137 Name += TCollection_AsciiString("/");
138 result = OSD_Path(Name);
139 // result.SetValues("","","","",Name,"","");
147 void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
148 TCollection_AsciiString Name;
151 where.SystemName(Name);
153 status = chdir (Name.ToCString());
154 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
158 void OSD_Process::Reset(){
162 Standard_Boolean OSD_Process::Failed()const{
163 return( myError.Failed());
166 void OSD_Process::Perror() {
171 Standard_Integer OSD_Process::Error()const{
172 return( myError.Error());
177 //------------------------------------------------------------------------
178 //------------------- WNT Sources of OSD_Path ---------------------------
179 //------------------------------------------------------------------------
181 //it is important to undefine NOUSER and enforce including <windows.h> before
182 //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
184 #undef NOUSER /* we need SW_HIDE from windows.h */
188 #ifdef SetCurrentDirectory
189 # undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
191 #include <OSD_Process.hxx>
193 #include <OSD_Path.hxx>
194 #include <Quantity_Date.hxx>
196 #include <OSD_WNT_1.hxx>
197 #include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() )
200 #pragma warning( disable : 4700 )
202 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
204 OSD_Process :: OSD_Process () {
208 void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
209 const Standard_Boolean ShowWindow /* = Standard_True */) {
212 PROCESS_INFORMATION pi;
214 ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
216 si.cb = sizeof ( STARTUPINFO );
217 //============================================
218 //---> Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
219 //---> Reason : to allow to hide the window
222 si.dwFlags = STARTF_USESHOWWINDOW;
223 si.wShowWindow = SW_HIDE;
225 //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
226 //============================================
229 NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
233 _osd_wnt_set_error ( myError, OSD_WProcess );
237 CloseHandle ( pi.hThread );
239 WaitForSingleObject ( pi.hProcess, INFINITE );
241 CloseHandle ( pi.hProcess );
245 } // end OSD_Process :: Spawn
247 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
249 Name = TEXT( "WIN32 console" );
251 } // end OSD_Process :: TerminalType
253 Quantity_Date OSD_Process :: SystemDate () {
255 Quantity_Date retVal;
258 GetLocalTime ( &st );
261 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
266 } // end OSD_Process :: SystemDate
268 Standard_Integer OSD_Process :: UserId () {
271 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
272 PTOKEN_OWNER pTKowner = NULL;
274 if ( !OpenProcessToken (
275 GetCurrentProcess (),
276 TOKEN_QUERY, &hProcessToken
278 ( pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx (
279 hProcessToken, TokenOwner
282 ( retVal = CopySidEx ( pTKowner -> Owner ) ) == NULL
285 _osd_wnt_set_error ( myError, OSD_WProcess );
287 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
288 if ( pTKowner != NULL ) FreeTokenInformation ( pTKowner );
290 return ( Standard_Integer )retVal;
292 } // end OSD_Process :: UserId
294 TCollection_AsciiString OSD_Process :: UserName ()
296 Standard_PCharacter pBuff = new char[UNLEN + 1];
297 DWORD dwSize = UNLEN + 1;
298 TCollection_AsciiString retVal;
299 if ( !GetUserName ( pBuff, &dwSize ) )
301 _osd_wnt_set_error ( myError, OSD_WProcess );
305 TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
306 retVal = theTmpUserName;
310 } // end OSD_Process :: UserName
312 Standard_Boolean OSD_Process :: IsSuperUser () {
314 Standard_Boolean retVal = FALSE;
316 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
317 PTOKEN_GROUPS pTKgroups = NULL;
319 if ( !OpenProcessToken (
320 GetCurrentProcess (),
321 TOKEN_QUERY, &hProcessToken
323 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
324 hProcessToken, TokenGroups
329 _osd_wnt_set_error ( myError, OSD_WProcess );
333 pSIDadmin = AdminSid ();
335 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
337 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
346 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
347 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
351 } // end OSD_Process :: IsSuperUser
353 Standard_Integer OSD_Process :: ProcessId () {
355 return ( Standard_Integer )GetCurrentProcessId ();
357 } // end OSD_Process :: ProcessId
359 OSD_Path OSD_Process :: CurrentDirectory () {
361 OSD_Path anCurrentDirectory;
363 DWORD dwSize = PATHLEN + 1;
364 Standard_PCharacter pBuff = new char[dwSize];
366 if ( GetCurrentDirectory(dwSize, pBuff) > 0 )
367 anCurrentDirectory = OSD_Path ( pBuff );
369 _osd_wnt_set_error ( myError, OSD_WProcess );
372 return anCurrentDirectory;
373 } // end OSD_Process :: CurrentDirectory
375 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
378 # define SetCurrentDirectory SetCurrentDirectoryW
380 # define SetCurrentDirectory SetCurrentDirectoryA
383 TCollection_AsciiString path;
385 where.SystemName ( path );
387 if ( !::SetCurrentDirectory ( path.ToCString () ) )
389 _osd_wnt_set_error ( myError, OSD_WProcess );
391 } // end OSD_Process :: SetCurrentDirectory
393 Standard_Boolean OSD_Process :: Failed () const {
395 return myError.Failed ();
397 } // end OSD_Process :: Failed
399 void OSD_Process :: Reset () {
403 } // end OSD_Process :: Reset
405 void OSD_Process :: Perror () {
409 } // end OSD_Process :: Perror
411 Standard_Integer OSD_Process :: Error () const {
413 return myError.Error ();
415 } // end OSD_Process :: Error