0026585: Eliminate compile warnings obtained by building occt with vc14: 'type cast...
[occt.git] / src / OSD / OSD_Process.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 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
973c2be1 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.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
57c28b61 15#ifndef _WIN32
7fd59977 16
42cf5bc1 17
7fd59977 18#include <OSD_Environment.hxx>
42cf5bc1 19#include <OSD_OSDError.hxx>
20#include <OSD_Path.hxx>
21#include <OSD_Process.hxx>
22#include <OSD_WhoAmI.hxx>
23#include <Quantity_Date.hxx>
24#include <TCollection_AsciiString.hxx>
7fd59977 25
26const OSD_WhoAmI Iam = OSD_WProcess;
27
28#include <errno.h>
7fd59977 29#include <stdlib.h>
03155c18 30#include <sys/param.h>
31#include <sys/time.h>
32#include <pwd.h> // For command getpwuid
33#include <unistd.h>
7fd59977 34
35OSD_Process::OSD_Process(){
36}
37
38
39void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
40 const Standard_Boolean /*ShowWindow*/)
41{
42 system(cmd.ToCString());
43}
44
45
46void OSD_Process::TerminalType(TCollection_AsciiString& Name){
47TCollection_AsciiString which="TERM";
48OSD_Environment term (which,"");
49
50 term.Value();
51 which = term.Value();
52 Name = term.Name();
53}
54
55
56// Get date of system date
57
58Quantity_Date OSD_Process::SystemDate(){
59Quantity_Date result;
60Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
61struct tm transfert;
62struct timeval tval;
63struct timezone tzone;
64int status;
65
66 status = gettimeofday( &tval, &tzone );
67 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
68 else {
69 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
70tm));
71 month = transfert.tm_mon + 1; // Add to January (month #1)
72 day = transfert.tm_mday;
73 year = transfert.tm_year;
74 hh = transfert.tm_hour;
75 mn = transfert.tm_min ;
76 ss = transfert.tm_sec ;
77}
78
79 result.SetValues ( month, day, year+1900, hh, mn, ss);
80 return (result);
81}
82
83
84Standard_Integer OSD_Process::ProcessId(){
85 return (getpid());
86}
87
7fd59977 88TCollection_AsciiString OSD_Process::UserName(){
89 struct passwd *infos;
90 infos = getpwuid(getuid());
91 TCollection_AsciiString result=infos->pw_name;
92
93 return(result);
94}
95
96Standard_Boolean OSD_Process::IsSuperUser (){
97 if (getuid()) {
98 return Standard_False;
99 }
100 else {
101 return Standard_True;
102 }
103}
104
105
106OSD_Path OSD_Process::CurrentDirectory(){
107char cwd[MAXPATHLEN+1] ;
108OSD_Path result;
109TCollection_AsciiString Name;
110
111 if (!getcwd(cwd,MAXPATHLEN+1))
112 myError.SetValue (errno, Iam, "Where");
113 else {
114 Name = cwd;
115
116// JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
117// position = Name.SearchFromEnd(".");
118// if (position != -1){
119// Ext = Name;
120// Ext.Remove(1,position);
121// Name.Remove( position,Ext.Length()+1);
122// }
123// result.SetValues("","","","","",Name,Ext);
124// End
125
126#if defined(vax) || defined(__vms)
127 Standard_Integer iDisk = Name.Search(":");
128 if (iDisk){
129 TCollection_AsciiString Disk;
130 TCollection_AsciiString Directory;
131 Disk = Name.SubString(1,iDisk-1);
132 Directory = Name.SubString(iDisk+1,Name.Length());
133 result.SetValues("","","",Disk,Directory,"","");
134 }
135#else
136 Name += TCollection_AsciiString("/");
137 result = OSD_Path(Name);
138 // result.SetValues("","","","",Name,"","");
139#endif
140
141 }
142return (result);
143}
144
145
146void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
147TCollection_AsciiString Name;
148int status;
149
150 where.SystemName(Name);
151
152 status = chdir (Name.ToCString());
153 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
154}
155
156
157void OSD_Process::Reset(){
158 myError.Reset();
159}
160
161Standard_Boolean OSD_Process::Failed()const{
162 return( myError.Failed());
163}
164
165void OSD_Process::Perror() {
166 myError.Perror();
167}
168
169
170Standard_Integer OSD_Process::Error()const{
171 return( myError.Error());
172}
173
174#else
175
176//------------------------------------------------------------------------
177//------------------- WNT Sources of OSD_Path ---------------------------
178//------------------------------------------------------------------------
179
180//it is important to undefine NOUSER and enforce including <windows.h> before
181//Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
182#ifdef NOUSER
183#undef NOUSER /* we need SW_HIDE from windows.h */
184#endif
185#include <windows.h>
186
187#ifdef SetCurrentDirectory
188# undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
189#endif
190#include <OSD_Process.hxx>
191
192#include <OSD_Path.hxx>
193#include <Quantity_Date.hxx>
d9ff84e8 194#include <Standard_PExtCharacter.hxx>
195#include <TCollection_ExtendedString.hxx>
7fd59977 196
197#include <OSD_WNT_1.hxx>
198#include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() )
199
200
201#pragma warning( disable : 4700 )
202
203void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
204
205OSD_Process :: OSD_Process () {
206
207} // end constructor
208
209void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
210 const Standard_Boolean ShowWindow /* = Standard_True */) {
211
212 STARTUPINFO si;
213 PROCESS_INFORMATION pi;
214
215 ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
216
217 si.cb = sizeof ( STARTUPINFO );
218 //============================================
219 //---> Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
220 //---> Reason : to allow to hide the window
221 if ( !ShowWindow )
222 {
223 si.dwFlags = STARTF_USESHOWWINDOW;
224 si.wShowWindow = SW_HIDE;
225 }
226 //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
227 //============================================
228
229 if (!CreateProcess (
230 NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
231 )
232 )
233
234 _osd_wnt_set_error ( myError, OSD_WProcess );
235
236 else {
237
238 CloseHandle ( pi.hThread );
239
240 WaitForSingleObject ( pi.hProcess, INFINITE );
241
242 CloseHandle ( pi.hProcess );
243
244 } // end else
245
246} // end OSD_Process :: Spawn
247
248void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
249
d9ff84e8 250 Name = "WIN32 console";
7fd59977 251
252} // end OSD_Process :: TerminalType
253
254Quantity_Date OSD_Process :: SystemDate () {
255
256 Quantity_Date retVal;
257 SYSTEMTIME st;
258
259 GetLocalTime ( &st );
260
261 retVal.SetValues (
262 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
263 );
264
265 return retVal;
266
267} // end OSD_Process :: SystemDate
268
7fd59977 269TCollection_AsciiString OSD_Process :: UserName ()
270{
271 Standard_PCharacter pBuff = new char[UNLEN + 1];
272 DWORD dwSize = UNLEN + 1;
273 TCollection_AsciiString retVal;
274 if ( !GetUserName ( pBuff, &dwSize ) )
275 {
276 _osd_wnt_set_error ( myError, OSD_WProcess );
277 }
278 else
279 {
280 TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
281 retVal = theTmpUserName;
282 }
283 delete [] pBuff;
284 return retVal;
285} // end OSD_Process :: UserName
286
287Standard_Boolean OSD_Process :: IsSuperUser () {
288
289 Standard_Boolean retVal = FALSE;
290 PSID pSIDadmin;
291 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
302f96fb 292 PTOKEN_GROUPS pTKgroups = NULL;
7fd59977 293
294 if ( !OpenProcessToken (
295 GetCurrentProcess (),
296 TOKEN_QUERY, &hProcessToken
297 ) ||
298 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
299 hProcessToken, TokenGroups
300 )
301 ) == NULL
302 )
303
304 _osd_wnt_set_error ( myError, OSD_WProcess );
305
306 else {
307
308 pSIDadmin = AdminSid ();
309
310 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
311
312 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
313
314 retVal = TRUE;
315 break;
316
317 } // end if
318
319 } // end else
320
321 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
322 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
323
324 return retVal;
325
326} // end OSD_Process :: IsSuperUser
327
328Standard_Integer OSD_Process :: ProcessId () {
329
330 return ( Standard_Integer )GetCurrentProcessId ();
331
332} // end OSD_Process :: ProcessId
333
334OSD_Path OSD_Process :: CurrentDirectory () {
335
a8195d65 336 OSD_Path anCurrentDirectory;
7fd59977 337
a8195d65 338 DWORD dwSize = PATHLEN + 1;
d9ff84e8 339 Standard_WideChar* pBuff = new wchar_t[dwSize];
7fd59977 340
d9ff84e8 341 if ( GetCurrentDirectoryW(dwSize, (wchar_t*)pBuff) > 0 )
342 {
343 // conversion to UTF-8 is performed inside
344 TCollection_AsciiString aPath(TCollection_ExtendedString((Standard_ExtString)pBuff));
345 anCurrentDirectory = OSD_Path ( aPath );
346 }
a8195d65 347 else
348 _osd_wnt_set_error ( myError, OSD_WProcess );
7fd59977 349
a8195d65 350 delete[] pBuff;
351 return anCurrentDirectory;
7fd59977 352} // end OSD_Process :: CurrentDirectory
353
354void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
355
7fd59977 356 TCollection_AsciiString path;
357
358 where.SystemName ( path );
d9ff84e8 359 TCollection_ExtendedString pathW(path);
7fd59977 360
d9ff84e8 361 if ( !::SetCurrentDirectoryW ( (const wchar_t*) pathW.ToExtString () ) )
7fd59977 362
363 _osd_wnt_set_error ( myError, OSD_WProcess );
364
365} // end OSD_Process :: SetCurrentDirectory
366
367Standard_Boolean OSD_Process :: Failed () const {
368
369 return myError.Failed ();
370
371} // end OSD_Process :: Failed
372
373void OSD_Process :: Reset () {
374
375 myError.Reset ();
376
377} // end OSD_Process :: Reset
378
379void OSD_Process :: Perror () {
380
381 myError.Perror ();
382
383} // end OSD_Process :: Perror
384
385Standard_Integer OSD_Process :: Error () const {
386
387 return myError.Error ();
388
389} // end OSD_Process :: Error
390
391#endif