0020716: Eliminate usage of "config.h" header file
[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
7fd59977 15#ifndef WNT
16
17#include <OSD_Process.ixx>
18#include <OSD_WhoAmI.hxx>
19#include <OSD_Environment.hxx>
20
21const OSD_WhoAmI Iam = OSD_WProcess;
22
23#include <errno.h>
7fd59977 24#include <stdlib.h>
03155c18 25#include <sys/param.h>
26#include <sys/time.h>
27#include <pwd.h> // For command getpwuid
28#include <unistd.h>
7fd59977 29
30OSD_Process::OSD_Process(){
31}
32
33
34void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
35 const Standard_Boolean /*ShowWindow*/)
36{
37 system(cmd.ToCString());
38}
39
40
41void OSD_Process::TerminalType(TCollection_AsciiString& Name){
42TCollection_AsciiString which="TERM";
43OSD_Environment term (which,"");
44
45 term.Value();
46 which = term.Value();
47 Name = term.Name();
48}
49
50
51// Get date of system date
52
53Quantity_Date OSD_Process::SystemDate(){
54Quantity_Date result;
55Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
56struct tm transfert;
57struct timeval tval;
58struct timezone tzone;
59int status;
60
61 status = gettimeofday( &tval, &tzone );
62 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
63 else {
64 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
65tm));
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 ;
72}
73
74 result.SetValues ( month, day, year+1900, hh, mn, ss);
75 return (result);
76}
77
78
79Standard_Integer OSD_Process::ProcessId(){
80 return (getpid());
81}
82
83
84Standard_Integer OSD_Process::UserId(){
85 return (getuid());
86}
87
88
89TCollection_AsciiString OSD_Process::UserName(){
90 struct passwd *infos;
91 infos = getpwuid(getuid());
92 TCollection_AsciiString result=infos->pw_name;
93
94 return(result);
95}
96
97Standard_Boolean OSD_Process::IsSuperUser (){
98 if (getuid()) {
99 return Standard_False;
100 }
101 else {
102 return Standard_True;
103 }
104}
105
106
107OSD_Path OSD_Process::CurrentDirectory(){
108char cwd[MAXPATHLEN+1] ;
109OSD_Path result;
110TCollection_AsciiString Name;
111
112 if (!getcwd(cwd,MAXPATHLEN+1))
113 myError.SetValue (errno, Iam, "Where");
114 else {
115 Name = cwd;
116
117// JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
118// position = Name.SearchFromEnd(".");
119// if (position != -1){
120// Ext = Name;
121// Ext.Remove(1,position);
122// Name.Remove( position,Ext.Length()+1);
123// }
124// result.SetValues("","","","","",Name,Ext);
125// End
126
127#if defined(vax) || defined(__vms)
128 Standard_Integer iDisk = Name.Search(":");
129 if (iDisk){
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,"","");
135 }
136#else
137 Name += TCollection_AsciiString("/");
138 result = OSD_Path(Name);
139 // result.SetValues("","","","",Name,"","");
140#endif
141
142 }
143return (result);
144}
145
146
147void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
148TCollection_AsciiString Name;
149int status;
150
151 where.SystemName(Name);
152
153 status = chdir (Name.ToCString());
154 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
155}
156
157
158void OSD_Process::Reset(){
159 myError.Reset();
160}
161
162Standard_Boolean OSD_Process::Failed()const{
163 return( myError.Failed());
164}
165
166void OSD_Process::Perror() {
167 myError.Perror();
168}
169
170
171Standard_Integer OSD_Process::Error()const{
172 return( myError.Error());
173}
174
175#else
176
177//------------------------------------------------------------------------
178//------------------- WNT Sources of OSD_Path ---------------------------
179//------------------------------------------------------------------------
180
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
183#ifdef NOUSER
184#undef NOUSER /* we need SW_HIDE from windows.h */
185#endif
186#include <windows.h>
187
188#ifdef SetCurrentDirectory
189# undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
190#endif
191#include <OSD_Process.hxx>
192
193#include <OSD_Path.hxx>
194#include <Quantity_Date.hxx>
195
196#include <OSD_WNT_1.hxx>
197#include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() )
198
199
200#pragma warning( disable : 4700 )
201
202void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
203
204OSD_Process :: OSD_Process () {
205
206} // end constructor
207
208void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
209 const Standard_Boolean ShowWindow /* = Standard_True */) {
210
211 STARTUPINFO si;
212 PROCESS_INFORMATION pi;
213
214 ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
215
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
220 if ( !ShowWindow )
221 {
222 si.dwFlags = STARTF_USESHOWWINDOW;
223 si.wShowWindow = SW_HIDE;
224 }
225 //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
226 //============================================
227
228 if (!CreateProcess (
229 NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
230 )
231 )
232
233 _osd_wnt_set_error ( myError, OSD_WProcess );
234
235 else {
236
237 CloseHandle ( pi.hThread );
238
239 WaitForSingleObject ( pi.hProcess, INFINITE );
240
241 CloseHandle ( pi.hProcess );
242
243 } // end else
244
245} // end OSD_Process :: Spawn
246
247void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
248
249 Name = TEXT( "WIN32 console" );
250
251} // end OSD_Process :: TerminalType
252
253Quantity_Date OSD_Process :: SystemDate () {
254
255 Quantity_Date retVal;
256 SYSTEMTIME st;
257
258 GetLocalTime ( &st );
259
260 retVal.SetValues (
261 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
262 );
263
264 return retVal;
265
266} // end OSD_Process :: SystemDate
267
268Standard_Integer OSD_Process :: UserId () {
269
270 PSID retVal = NULL;
271 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
272 PTOKEN_OWNER pTKowner = NULL;
273
274 if ( !OpenProcessToken (
275 GetCurrentProcess (),
276 TOKEN_QUERY, &hProcessToken
277 ) ||
278 ( pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx (
279 hProcessToken, TokenOwner
280 )
281 ) == NULL ||
282 ( retVal = CopySidEx ( pTKowner -> Owner ) ) == NULL
283 )
284
285 _osd_wnt_set_error ( myError, OSD_WProcess );
286
287 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
288 if ( pTKowner != NULL ) FreeTokenInformation ( pTKowner );
289
290 return ( Standard_Integer )retVal;
291
292} // end OSD_Process :: UserId
293
294TCollection_AsciiString OSD_Process :: UserName ()
295{
296 Standard_PCharacter pBuff = new char[UNLEN + 1];
297 DWORD dwSize = UNLEN + 1;
298 TCollection_AsciiString retVal;
299 if ( !GetUserName ( pBuff, &dwSize ) )
300 {
301 _osd_wnt_set_error ( myError, OSD_WProcess );
302 }
303 else
304 {
305 TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
306 retVal = theTmpUserName;
307 }
308 delete [] pBuff;
309 return retVal;
310} // end OSD_Process :: UserName
311
312Standard_Boolean OSD_Process :: IsSuperUser () {
313
314 Standard_Boolean retVal = FALSE;
315 PSID pSIDadmin;
316 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
302f96fb 317 PTOKEN_GROUPS pTKgroups = NULL;
7fd59977 318
319 if ( !OpenProcessToken (
320 GetCurrentProcess (),
321 TOKEN_QUERY, &hProcessToken
322 ) ||
323 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
324 hProcessToken, TokenGroups
325 )
326 ) == NULL
327 )
328
329 _osd_wnt_set_error ( myError, OSD_WProcess );
330
331 else {
332
333 pSIDadmin = AdminSid ();
334
335 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
336
337 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
338
339 retVal = TRUE;
340 break;
341
342 } // end if
343
344 } // end else
345
346 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
347 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
348
349 return retVal;
350
351} // end OSD_Process :: IsSuperUser
352
353Standard_Integer OSD_Process :: ProcessId () {
354
355 return ( Standard_Integer )GetCurrentProcessId ();
356
357} // end OSD_Process :: ProcessId
358
359OSD_Path OSD_Process :: CurrentDirectory () {
360
a8195d65 361 OSD_Path anCurrentDirectory;
7fd59977 362
a8195d65 363 DWORD dwSize = PATHLEN + 1;
364 Standard_PCharacter pBuff = new char[dwSize];
7fd59977 365
a8195d65 366 if ( GetCurrentDirectory(dwSize, pBuff) > 0 )
367 anCurrentDirectory = OSD_Path ( pBuff );
368 else
369 _osd_wnt_set_error ( myError, OSD_WProcess );
7fd59977 370
a8195d65 371 delete[] pBuff;
372 return anCurrentDirectory;
7fd59977 373} // end OSD_Process :: CurrentDirectory
374
375void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
376
377#ifdef UNICODE
378# define SetCurrentDirectory SetCurrentDirectoryW
379#else
380# define SetCurrentDirectory SetCurrentDirectoryA
381#endif // UNICODE
382
383 TCollection_AsciiString path;
384
385 where.SystemName ( path );
386
387 if ( !::SetCurrentDirectory ( path.ToCString () ) )
388
389 _osd_wnt_set_error ( myError, OSD_WProcess );
390
391} // end OSD_Process :: SetCurrentDirectory
392
393Standard_Boolean OSD_Process :: Failed () const {
394
395 return myError.Failed ();
396
397} // end OSD_Process :: Failed
398
399void OSD_Process :: Reset () {
400
401 myError.Reset ();
402
403} // end OSD_Process :: Reset
404
405void OSD_Process :: Perror () {
406
407 myError.Perror ();
408
409} // end OSD_Process :: Perror
410
411Standard_Integer OSD_Process :: Error () const {
412
413 return myError.Error ();
414
415} // end OSD_Process :: Error
416
417#endif