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 infos = getpwuid(getuid());
90 TCollection_AsciiString result=infos->pw_name;
95 Standard_Boolean OSD_Process::IsSuperUser (){
97 return Standard_False;
100 return Standard_True;
105 OSD_Path OSD_Process::CurrentDirectory(){
106 char cwd[MAXPATHLEN+1] ;
108 TCollection_AsciiString Name;
110 if (!getcwd(cwd,MAXPATHLEN+1))
111 myError.SetValue (errno, Iam, "Where");
115 // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
116 // position = Name.SearchFromEnd(".");
117 // if (position != -1){
119 // Ext.Remove(1,position);
120 // Name.Remove( position,Ext.Length()+1);
122 // result.SetValues("","","","","",Name,Ext);
125 #if defined(vax) || defined(__vms)
126 Standard_Integer iDisk = Name.Search(":");
128 TCollection_AsciiString Disk;
129 TCollection_AsciiString Directory;
130 Disk = Name.SubString(1,iDisk-1);
131 Directory = Name.SubString(iDisk+1,Name.Length());
132 result.SetValues("","","",Disk,Directory,"","");
135 Name += TCollection_AsciiString("/");
136 result = OSD_Path(Name);
137 // result.SetValues("","","","",Name,"","");
145 void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
146 TCollection_AsciiString Name;
149 where.SystemName(Name);
151 status = chdir (Name.ToCString());
152 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
156 void OSD_Process::Reset(){
160 Standard_Boolean OSD_Process::Failed()const{
161 return( myError.Failed());
164 void OSD_Process::Perror() {
169 Standard_Integer OSD_Process::Error()const{
170 return( myError.Error());
175 //------------------------------------------------------------------------
176 //------------------- WNT Sources of OSD_Path ---------------------------
177 //------------------------------------------------------------------------
179 //it is important to undefine NOUSER and enforce including <windows.h> before
180 //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
182 #undef NOUSER /* we need SW_HIDE from windows.h */
186 #ifdef SetCurrentDirectory
187 # undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
189 #include <OSD_Process.hxx>
191 #include <OSD_Path.hxx>
192 #include <Quantity_Date.hxx>
193 #include <Standard_PExtCharacter.hxx>
194 #include <TCollection_ExtendedString.hxx>
196 #include <OSD_WNT_1.hxx>
197 #include <LMCONS.H> // for UNLEN - maximum user name length GetUserName()
199 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
201 // =======================================================================
202 // function : OSD_Process
204 // =======================================================================
205 OSD_Process::OSD_Process()
210 // =======================================================================
213 // =======================================================================
214 Standard_Integer OSD_Process::Spawn (const TCollection_AsciiString& theCmd,
215 const Standard_Boolean theToShowWindow)
218 STARTUPINFOW aStartupInfo;
219 ZeroMemory (&aStartupInfo, sizeof(STARTUPINFO));
220 aStartupInfo.cb = sizeof(STARTUPINFO);
221 if (!theToShowWindow)
223 aStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
224 aStartupInfo.wShowWindow = SW_HIDE;
228 TCollection_ExtendedString aCmdWide (theCmd);
229 wchar_t* aCmdWidePtr = const_cast<wchar_t*>(aCmdWide.ToWideString()); // CreateProcessW() can modify content of this string
230 PROCESS_INFORMATION aProcessInfo;
231 if (!CreateProcessW (NULL, aCmdWidePtr, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &aStartupInfo, &aProcessInfo))
233 _osd_wnt_set_error (myError, OSD_WProcess);
234 aRes = myError.Error();
238 CloseHandle (aProcessInfo.hThread);
239 WaitForSingleObject (aProcessInfo.hProcess, INFINITE);
240 GetExitCodeProcess (aProcessInfo.hProcess, &aRes);
241 CloseHandle (aProcessInfo.hProcess);
247 (void )theToShowWindow;
252 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
254 Name = "WIN32 console";
256 } // end OSD_Process :: TerminalType
258 Quantity_Date OSD_Process :: SystemDate () {
260 Quantity_Date retVal;
263 GetLocalTime ( &st );
266 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
271 } // end OSD_Process :: SystemDate
273 // =======================================================================
274 // function : UserName
276 // =======================================================================
277 TCollection_AsciiString OSD_Process::UserName()
280 wchar_t aUserName[UNLEN + 1];
281 DWORD aNameSize = UNLEN + 1;
282 TCollection_AsciiString retVal;
283 if (!GetUserNameW (aUserName, &aNameSize))
285 _osd_wnt_set_error(myError, OSD_WProcess);
286 return TCollection_AsciiString();
288 return TCollection_AsciiString (aUserName);
290 return TCollection_AsciiString();
294 Standard_Boolean OSD_Process :: IsSuperUser () {
296 Standard_Boolean retVal = FALSE;
298 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
299 PTOKEN_GROUPS pTKgroups = NULL;
301 if ( !OpenProcessToken (
302 GetCurrentProcess (),
303 TOKEN_QUERY, &hProcessToken
305 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
306 hProcessToken, TokenGroups
311 _osd_wnt_set_error ( myError, OSD_WProcess );
315 pSIDadmin = AdminSid ();
317 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
319 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
328 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
329 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
335 } // end OSD_Process :: IsSuperUser
337 // =======================================================================
338 // function : ProcessId
340 // =======================================================================
341 Standard_Integer OSD_Process::ProcessId()
343 return (Standard_Integer )GetCurrentProcessId();
346 // =======================================================================
347 // function : CurrentDirectory
349 // =======================================================================
350 OSD_Path OSD_Process::CurrentDirectory()
352 OSD_Path anCurrentDirectory;
354 const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL);
357 wchar_t* aBuff = new wchar_t[aBuffLen + 1];
358 GetCurrentDirectoryW (aBuffLen, aBuff);
359 aBuff[aBuffLen] = L'\0';
360 const TCollection_AsciiString aPath (aBuff);
363 anCurrentDirectory = OSD_Path (aPath);
367 _osd_wnt_set_error (myError, OSD_WProcess);
370 return anCurrentDirectory;
373 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
375 TCollection_AsciiString path;
377 where.SystemName ( path );
378 TCollection_ExtendedString pathW(path);
380 if (!::SetCurrentDirectoryW (pathW.ToWideString()))
382 _osd_wnt_set_error ( myError, OSD_WProcess );
384 } // end OSD_Process :: SetCurrentDirectory
386 Standard_Boolean OSD_Process :: Failed () const {
388 return myError.Failed ();
390 } // end OSD_Process :: Failed
392 void OSD_Process :: Reset () {
396 } // end OSD_Process :: Reset
398 void OSD_Process :: Perror () {
402 } // end OSD_Process :: Perror
404 Standard_Integer OSD_Process :: Error () const {
406 return myError.Error ();
408 } // end OSD_Process :: Error