1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
26 #include <OSD_Process.ixx>
27 #include <OSD_WhoAmI.hxx>
28 #include <OSD_Environment.hxx>
30 const OSD_WhoAmI Iam = OSD_WProcess;
40 #ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
44 #if defined(HAVE_TIME_H) || defined(WNT)
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
53 # include <pwd.h> // For command getpwuid
56 OSD_Process::OSD_Process(){
60 void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
61 const Standard_Boolean /*ShowWindow*/)
63 system(cmd.ToCString());
67 void OSD_Process::TerminalType(TCollection_AsciiString& Name){
68 TCollection_AsciiString which="TERM";
69 OSD_Environment term (which,"");
77 // Get date of system date
79 Quantity_Date OSD_Process::SystemDate(){
81 Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
84 struct timezone tzone;
87 status = gettimeofday( &tval, &tzone );
88 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
90 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
92 month = transfert.tm_mon + 1; // Add to January (month #1)
93 day = transfert.tm_mday;
94 year = transfert.tm_year;
95 hh = transfert.tm_hour;
96 mn = transfert.tm_min ;
97 ss = transfert.tm_sec ;
100 result.SetValues ( month, day, year+1900, hh, mn, ss);
105 Standard_Integer OSD_Process::ProcessId(){
110 Standard_Integer OSD_Process::UserId(){
115 TCollection_AsciiString OSD_Process::UserName(){
116 struct passwd *infos;
117 infos = getpwuid(getuid());
118 TCollection_AsciiString result=infos->pw_name;
123 Standard_Boolean OSD_Process::IsSuperUser (){
125 return Standard_False;
128 return Standard_True;
133 OSD_Path OSD_Process::CurrentDirectory(){
134 char cwd[MAXPATHLEN+1] ;
136 TCollection_AsciiString Name;
138 if (!getcwd(cwd,MAXPATHLEN+1))
139 myError.SetValue (errno, Iam, "Where");
143 // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
144 // position = Name.SearchFromEnd(".");
145 // if (position != -1){
147 // Ext.Remove(1,position);
148 // Name.Remove( position,Ext.Length()+1);
150 // result.SetValues("","","","","",Name,Ext);
153 #if defined(vax) || defined(__vms)
154 Standard_Integer iDisk = Name.Search(":");
156 TCollection_AsciiString Disk;
157 TCollection_AsciiString Directory;
158 Disk = Name.SubString(1,iDisk-1);
159 Directory = Name.SubString(iDisk+1,Name.Length());
160 result.SetValues("","","",Disk,Directory,"","");
163 Name += TCollection_AsciiString("/");
164 result = OSD_Path(Name);
165 // result.SetValues("","","","",Name,"","");
173 void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
174 TCollection_AsciiString Name;
177 where.SystemName(Name);
179 status = chdir (Name.ToCString());
180 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
184 void OSD_Process::Reset(){
188 Standard_Boolean OSD_Process::Failed()const{
189 return( myError.Failed());
192 void OSD_Process::Perror() {
197 Standard_Integer OSD_Process::Error()const{
198 return( myError.Error());
203 //------------------------------------------------------------------------
204 //------------------- WNT Sources of OSD_Path ---------------------------
205 //------------------------------------------------------------------------
207 //it is important to undefine NOUSER and enforce including <windows.h> before
208 //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
210 #undef NOUSER /* we need SW_HIDE from windows.h */
214 #ifdef SetCurrentDirectory
215 # undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
217 #include <OSD_Process.hxx>
219 #include <OSD_Path.hxx>
220 #include <Quantity_Date.hxx>
222 #include <OSD_WNT_1.hxx>
223 #include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() )
226 #pragma warning( disable : 4700 )
228 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
230 OSD_Process :: OSD_Process () {
234 void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
235 const Standard_Boolean ShowWindow /* = Standard_True */) {
238 PROCESS_INFORMATION pi;
240 ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
242 si.cb = sizeof ( STARTUPINFO );
243 //============================================
244 //---> Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
245 //---> Reason : to allow to hide the window
248 si.dwFlags = STARTF_USESHOWWINDOW;
249 si.wShowWindow = SW_HIDE;
251 //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
252 //============================================
255 NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
259 _osd_wnt_set_error ( myError, OSD_WProcess );
263 CloseHandle ( pi.hThread );
265 WaitForSingleObject ( pi.hProcess, INFINITE );
267 CloseHandle ( pi.hProcess );
271 } // end OSD_Process :: Spawn
273 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
275 Name = TEXT( "WIN32 console" );
277 } // end OSD_Process :: TerminalType
279 Quantity_Date OSD_Process :: SystemDate () {
281 Quantity_Date retVal;
284 GetLocalTime ( &st );
287 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
292 } // end OSD_Process :: SystemDate
294 Standard_Integer OSD_Process :: UserId () {
297 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
298 PTOKEN_OWNER pTKowner = NULL;
300 if ( !OpenProcessToken (
301 GetCurrentProcess (),
302 TOKEN_QUERY, &hProcessToken
304 ( pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx (
305 hProcessToken, TokenOwner
308 ( retVal = CopySidEx ( pTKowner -> Owner ) ) == NULL
311 _osd_wnt_set_error ( myError, OSD_WProcess );
313 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
314 if ( pTKowner != NULL ) FreeTokenInformation ( pTKowner );
316 return ( Standard_Integer )retVal;
318 } // end OSD_Process :: UserId
320 TCollection_AsciiString OSD_Process :: UserName ()
322 Standard_PCharacter pBuff = new char[UNLEN + 1];
323 DWORD dwSize = UNLEN + 1;
324 TCollection_AsciiString retVal;
325 if ( !GetUserName ( pBuff, &dwSize ) )
327 _osd_wnt_set_error ( myError, OSD_WProcess );
331 TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
332 retVal = theTmpUserName;
336 } // end OSD_Process :: UserName
338 Standard_Boolean OSD_Process :: IsSuperUser () {
340 Standard_Boolean retVal = FALSE;
342 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
343 PTOKEN_GROUPS pTKgroups;
345 if ( !OpenProcessToken (
346 GetCurrentProcess (),
347 TOKEN_QUERY, &hProcessToken
349 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
350 hProcessToken, TokenGroups
355 _osd_wnt_set_error ( myError, OSD_WProcess );
359 pSIDadmin = AdminSid ();
361 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
363 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
372 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
373 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
377 } // end OSD_Process :: IsSuperUser
379 Standard_Integer OSD_Process :: ProcessId () {
381 return ( Standard_Integer )GetCurrentProcessId ();
383 } // end OSD_Process :: ProcessId
385 OSD_Path OSD_Process :: CurrentDirectory () {
387 Standard_PCharacter pBuff;
391 dwSize = GetCurrentDirectory ( dwSize, pBuff );
392 pBuff = new Standard_Character[ dwSize ];
394 if ( ( dwSize = GetCurrentDirectory ( dwSize, pBuff ) ) == NULL )
396 _osd_wnt_set_error ( myError, OSD_WProcess );
400 retVal = OSD_Path ( pBuff );
406 } // end OSD_Process :: CurrentDirectory
408 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
411 # define SetCurrentDirectory SetCurrentDirectoryW
413 # define SetCurrentDirectory SetCurrentDirectoryA
416 TCollection_AsciiString path;
418 where.SystemName ( path );
420 if ( !::SetCurrentDirectory ( path.ToCString () ) )
422 _osd_wnt_set_error ( myError, OSD_WProcess );
424 } // end OSD_Process :: SetCurrentDirectory
426 Standard_Boolean OSD_Process :: Failed () const {
428 return myError.Failed ();
430 } // end OSD_Process :: Failed
432 void OSD_Process :: Reset () {
436 } // end OSD_Process :: Reset
438 void OSD_Process :: Perror () {
442 } // end OSD_Process :: Perror
444 Standard_Integer OSD_Process :: Error () const {
446 return myError.Error ();
448 } // end OSD_Process :: Error