23 #define SYSTEM_SYSTEM_CPP
41 #elif defined( __APPLE__ )
43 #elif defined( _WIN32 )
48 # define VCL_NAMESPACE Gaudi
54 # define strcasecmp _stricmp
55 # define strncasecmp _strnicmp
56 # define getpid _getpid
64 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
66 # include "sys/times.h"
72 # if defined( __linux ) || defined( __APPLE__ )
74 # include <sys/utsname.h>
83 # endif // HPUX or not...
91 #endif // Windows or Unix...
99 # if defined( __linux ) || defined( __APPLE__ )
100 void* mh = ::dlopen(
name.length() == 0 ?
nullptr :
path, RTLD_LAZY | RTLD_GLOBAL );
103 shl_t mh = ::shl_load(
name.length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0 );
104 HMODULE*
mod =
new HMODULE;
106 if ( 0 != ::shl_gethandle_r( mh, &
mod->dsc ) ) {
109 typedef void* ( *___all )();
110 ___all _alloc = (___all)malloc;
111 mod->numSym = ::shl_getsymbols(
mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &
mod->sym );
124 if (
name.length() == 0 ) {
return doLoad(
name, handle ); }
129 bool hasShlibSuffix =
false;
130 for (
const char* suffix : SHLIB_SUFFIXES ) {
131 const size_t len =
strlen( suffix );
132 if ( dllName.
compare( dllName.
length() - len, len, suffix ) == 0 ) {
133 hasShlibSuffix =
true;
140 if ( !hasShlibSuffix ) { dllName += SHLIB_SUFFIXES[0]; }
143 return doLoad( dllName, handle );
148 unsigned long res = 0;
150 if (
name.length() == 0 ) {
151 res = loadWithoutEnvironment(
name, handle );
157 res = loadWithoutEnvironment( imgName, handle );
163 #if defined( __linux ) || defined( __APPLE__ )
164 if ( ( dllName.
find(
'/' ) == std::string::npos ) && ( dllName.
compare( 0, 3,
"lib" ) != 0 ) ) {
165 dllName =
"lib" + dllName;
170 for (
const char* suffix : SHLIB_SUFFIXES ) {
173 const size_t len = strlen( suffix );
174 if ( dllName.
compare( dllName.
length() - len, len, suffix ) != 0 ) { libName += suffix; }
176 res = loadWithoutEnvironment( libName, handle );
178 if ( res == 1 ) {
break; }
182 #if defined( __linux ) || defined( __APPLE__ )
194 if ( !::FreeLibrary( (HINSTANCE)handle ) ) {
195 #elif defined( __linux ) || defined( __APPLE__ )
202 HMODULE*
mod = (HMODULE*)handle;
203 if ( 0 == ::shl_unload(
mod->dsc.handle ) ) {
217 *pFunction = (
EntryPoint )::GetProcAddress( (HINSTANCE)handle,
name.data() );
220 #elif defined( __linux )
221 *pFunction =
reinterpret_cast<EntryPoint>( ::dlsym( handle,
name.c_str() ) );
228 #elif defined( __APPLE__ )
230 if ( !( *pFunction ) ) {
235 if ( 0 == *pFunction ) {
243 HMODULE*
mod = (HMODULE*)handle;
245 long ll1 =
name.length();
246 for (
int i = 0; i <
mod->numSym; i++ ) {
247 long ll2 = strlen(
mod->sym[i].name );
248 if ( 0 != ::strncmp(
mod->sym[i].name,
name.c_str(), ( ll1 > ll2 ) ? ll1 : ll2 ) == 0 ) {
266 return ::GetLastError();
269 return static_cast<unsigned long>(
static_cast<unsigned int>( errno ) );
283 LPVOID lpMessageBuffer;
284 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
285 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
286 (LPTSTR)&lpMessageBuffer, 0, NULL );
287 errString = (
const char*)lpMessageBuffer;
289 ::LocalFree( lpMessageBuffer );
291 char* cerrString(
nullptr );
293 if ( error == 0xAFFEDEAD ) {
294 cerrString = ::dlerror();
295 if ( !cerrString ) cerrString = ::strerror( error );
297 cerrString = (
char*)
"Unknown error. No information found in strerror()!";
303 cerrString = ::strerror( error );
340 using namespace Gaudi;
375 return (
char**)&(
args[0] );
381 # pragma warning( disable : 4996 )
387 if ( ( env = getenv( var ) ) !=
nullptr ) {
397 if ( ( env = getenv( var ) ) !=
nullptr ) {
408 #if defined( __APPLE__ )
410 # include "crt_externs.h"
413 #if defined( _WIN32 )
414 # define environ _environ
415 #elif defined( __APPLE__ )
416 static char** environ = *_NSGetEnviron();
419 for (
int i = 0; environ[i] !=
nullptr; ++i ) { vars.
push_back( environ[i] ); }
427 # include <execinfo.h>
430 int System::backTrace( [[maybe_unused]]
void** addresses, [[maybe_unused]]
const int depth ) {
434 int count = backtrace( addresses, depth );
435 return count > 0 ? count : 0;
437 #else // windows and osx parts not implemented
445 const int totalOffset = offset + 2;
446 const int totalDepth = depth + totalOffset;
452 for (
int i = totalOffset; i < count; ++i ) {
453 void* addr =
nullptr;
473 if ( dladdr( addresses, &info ) && info.dli_fname && info.dli_fname[0] !=
'\0' ) {
474 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
476 lib = info.dli_fname;
477 addr = info.dli_saddr;
483 fnc = ( stat == 0 ) ? dmg.get() : symbol;
492 #else // not implemented for windows and osx
501 return value.
empty() ?
503 ::unsetenv(
name.c_str() ),
506 ::setenv(
name.c_str(), value.
c_str(), overwrite );
509 if ( value.
empty() ) {
511 return ::_putenv( (
name +
"=" ).c_str() );
513 if ( !getenv(
name.c_str() ) || overwrite ) {
515 return ::_putenv( (
name +
"=" + value ).c_str() );