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_Environment.hxx>
18 #include <OSD_OSDError.hxx>
19 #include <OSD_Path.hxx>
20 #include <OSD_Process.hxx>
21 #include <OSD_WhoAmI.hxx>
22 #include <Quantity_Date.hxx>
23 #include <TCollection_AsciiString.hxx>
25 const OSD_WhoAmI Iam = OSD_WProcess;
29 #include <sys/param.h>
31 #include <pwd.h> // For command getpwuid
34 OSD_Process::OSD_Process(){
38 Standard_Integer OSD_Process::Spawn (const TCollection_AsciiString& cmd,
39 const Standard_Boolean /*ShowWindow*/)
41 return system(cmd.ToCString());
45 void OSD_Process::TerminalType(TCollection_AsciiString& Name){
46 TCollection_AsciiString which="TERM";
47 OSD_Environment term (which,"");
55 // Get date of system date
57 Quantity_Date OSD_Process::SystemDate(){
59 Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
62 struct timezone tzone;
65 status = gettimeofday( &tval, &tzone );
66 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
68 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
70 month = transfert.tm_mon + 1; // Add to January (month #1)
71 day = transfert.tm_mday;
72 year = transfert.tm_year;
73 hh = transfert.tm_hour;
74 mn = transfert.tm_min ;
75 ss = transfert.tm_sec ;
78 result.SetValues ( month, day, year+1900, hh, mn, ss);
83 Standard_Integer OSD_Process::ProcessId(){
87 TCollection_AsciiString OSD_Process::UserName()
89 struct passwd *anInfos = getpwuid (getuid());
90 return TCollection_AsciiString (anInfos ? anInfos->pw_name : "");
93 Standard_Boolean OSD_Process::IsSuperUser (){
95 return Standard_False;
103 OSD_Path OSD_Process::CurrentDirectory(){
104 char cwd[MAXPATHLEN+1] ;
106 TCollection_AsciiString Name;
108 if (!getcwd(cwd,MAXPATHLEN+1))
109 myError.SetValue (errno, Iam, "Where");
113 // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
114 // position = Name.SearchFromEnd(".");
115 // if (position != -1){
117 // Ext.Remove(1,position);
118 // Name.Remove( position,Ext.Length()+1);
120 // result.SetValues("","","","","",Name,Ext);
123 #if defined(vax) || defined(__vms)
124 Standard_Integer iDisk = Name.Search(":");
126 TCollection_AsciiString Disk;
127 TCollection_AsciiString Directory;
128 Disk = Name.SubString(1,iDisk-1);
129 Directory = Name.SubString(iDisk+1,Name.Length());
130 result.SetValues("","","",Disk,Directory,"","");
133 Name += TCollection_AsciiString("/");
134 result = OSD_Path(Name);
135 // result.SetValues("","","","",Name,"","");
143 void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
144 TCollection_AsciiString Name;
147 where.SystemName(Name);
149 status = chdir (Name.ToCString());
150 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
154 void OSD_Process::Reset(){
158 Standard_Boolean OSD_Process::Failed()const{
159 return( myError.Failed());
162 void OSD_Process::Perror() {
167 Standard_Integer OSD_Process::Error()const{
168 return( myError.Error());
173 //------------------------------------------------------------------------
174 //------------------- WNT Sources of OSD_Path ---------------------------
175 //------------------------------------------------------------------------
177 //it is important to undefine NOUSER and enforce including <windows.h> before
178 //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
180 #undef NOUSER /* we need SW_HIDE from windows.h */
184 #include <OSD_Process.hxx>
186 #include <OSD_Path.hxx>
187 #include <Quantity_Date.hxx>
188 #include <Standard_PExtCharacter.hxx>
189 #include <TCollection_ExtendedString.hxx>
191 #include <OSD_WNT_1.hxx>
192 #include <LMCONS.H> // for UNLEN - maximum user name length GetUserName()
194 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
196 // =======================================================================
197 // function : OSD_Process
199 // =======================================================================
200 OSD_Process::OSD_Process()
205 // =======================================================================
208 // =======================================================================
209 Standard_Integer OSD_Process::Spawn (const TCollection_AsciiString& theCmd,
210 const Standard_Boolean theToShowWindow)
213 STARTUPINFOW aStartupInfo;
214 ZeroMemory (&aStartupInfo, sizeof(STARTUPINFO));
215 aStartupInfo.cb = sizeof(STARTUPINFO);
216 if (!theToShowWindow)
218 aStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
219 aStartupInfo.wShowWindow = SW_HIDE;
223 TCollection_ExtendedString aCmdWide (theCmd);
224 wchar_t* aCmdWidePtr = const_cast<wchar_t*>(aCmdWide.ToWideString()); // CreateProcessW() can modify content of this string
225 PROCESS_INFORMATION aProcessInfo;
226 if (!CreateProcessW (NULL, aCmdWidePtr, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &aStartupInfo, &aProcessInfo))
228 _osd_wnt_set_error (myError, OSD_WProcess);
229 aRes = myError.Error();
233 CloseHandle (aProcessInfo.hThread);
234 WaitForSingleObject (aProcessInfo.hProcess, INFINITE);
235 GetExitCodeProcess (aProcessInfo.hProcess, &aRes);
236 CloseHandle (aProcessInfo.hProcess);
242 (void )theToShowWindow;
247 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
249 Name = "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 // =======================================================================
269 // function : UserName
271 // =======================================================================
272 TCollection_AsciiString OSD_Process::UserName()
275 wchar_t aUserName[UNLEN + 1];
276 DWORD aNameSize = UNLEN + 1;
277 TCollection_AsciiString retVal;
278 if (!GetUserNameW (aUserName, &aNameSize))
280 _osd_wnt_set_error(myError, OSD_WProcess);
281 return TCollection_AsciiString();
283 return TCollection_AsciiString (aUserName);
285 return TCollection_AsciiString();
289 Standard_Boolean OSD_Process :: IsSuperUser () {
291 Standard_Boolean retVal = FALSE;
293 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
294 PTOKEN_GROUPS pTKgroups = NULL;
296 if ( !OpenProcessToken (
297 GetCurrentProcess (),
298 TOKEN_QUERY, &hProcessToken
300 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
301 hProcessToken, TokenGroups
306 _osd_wnt_set_error ( myError, OSD_WProcess );
310 pSIDadmin = AdminSid ();
312 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
314 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
323 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
324 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
330 } // end OSD_Process :: IsSuperUser
332 // =======================================================================
333 // function : ProcessId
335 // =======================================================================
336 Standard_Integer OSD_Process::ProcessId()
338 return (Standard_Integer )GetCurrentProcessId();
341 // =======================================================================
342 // function : CurrentDirectory
344 // =======================================================================
345 OSD_Path OSD_Process::CurrentDirectory()
347 OSD_Path anCurrentDirectory;
349 const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL);
352 wchar_t* aBuff = new wchar_t[aBuffLen + 1];
353 GetCurrentDirectoryW (aBuffLen, aBuff);
354 aBuff[aBuffLen] = L'\0';
355 const TCollection_AsciiString aPath (aBuff);
358 anCurrentDirectory = OSD_Path (aPath);
362 _osd_wnt_set_error (myError, OSD_WProcess);
365 return anCurrentDirectory;
368 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
370 TCollection_AsciiString path;
372 where.SystemName ( path );
373 TCollection_ExtendedString pathW(path);
375 if (!::SetCurrentDirectoryW (pathW.ToWideString()))
377 _osd_wnt_set_error ( myError, OSD_WProcess );
379 } // end OSD_Process :: SetCurrentDirectory
381 Standard_Boolean OSD_Process :: Failed () const {
383 return myError.Failed ();
385 } // end OSD_Process :: Failed
387 void OSD_Process :: Reset () {
391 } // end OSD_Process :: Reset
393 void OSD_Process :: Perror () {
397 } // end OSD_Process :: Perror
399 Standard_Integer OSD_Process :: Error () const {
401 return myError.Error ();
403 } // end OSD_Process :: Error