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 )
100 void* mh = ::dlopen(
name.length() == 0 ?
nullptr :
path, RTLD_LAZY | RTLD_GLOBAL );
102 # elif defined( __APPLE__ )
103 void* mh = ::dlopen(
name.length() == 0 ?
nullptr :
path, RTLD_LAZY | RTLD_GLOBAL );
106 shl_t mh = ::shl_load(
name.length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0 );
107 HMODULE*
mod =
new HMODULE;
109 if ( 0 != ::shl_gethandle_r( mh, &
mod->dsc ) ) {
112 typedef void* ( *___all )();
113 ___all _alloc = (___all)malloc;
114 mod->numSym = ::shl_getsymbols(
mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &
mod->sym );
127 if (
name.length() == 0 ) {
return doLoad(
name, handle ); }
132 bool hasShlibSuffix =
false;
133 for (
const char* suffix : SHLIB_SUFFIXES ) {
134 const size_t len =
strlen( suffix );
135 if ( dllName.
compare( dllName.
length() - len, len, suffix ) == 0 ) {
136 hasShlibSuffix =
true;
143 if ( !hasShlibSuffix ) { dllName += SHLIB_SUFFIXES[0]; }
146 return doLoad( dllName, handle );
151 unsigned long res = 0;
153 if (
name.length() == 0 ) {
154 res = loadWithoutEnvironment(
name, handle );
160 res = loadWithoutEnvironment( imgName, handle );
166 #if defined( __linux ) || defined( __APPLE__ )
167 if ( ( dllName.
find(
'/' ) == std::string::npos ) && ( dllName.
compare( 0, 3,
"lib" ) != 0 ) ) {
168 dllName =
"lib" + dllName;
173 for (
const char* suffix : SHLIB_SUFFIXES ) {
176 const size_t len = strlen( suffix );
177 if ( dllName.
compare( dllName.
length() - len, len, suffix ) != 0 ) { libName += suffix; }
179 res = loadWithoutEnvironment( libName, handle );
181 if ( res == 1 ) {
break; }
185 #if defined( __linux ) || defined( __APPLE__ )
197 if ( !::FreeLibrary( (HINSTANCE)handle ) ) {
198 #elif defined( __linux ) || defined( __APPLE__ )
205 HMODULE*
mod = (HMODULE*)handle;
206 if ( 0 == ::shl_unload(
mod->dsc.handle ) ) {
220 *pFunction = (
EntryPoint )::GetProcAddress( (HINSTANCE)handle,
name.data() );
223 #elif defined( __linux )
224 *pFunction =
reinterpret_cast<EntryPoint>( ::dlsym( handle,
name.c_str() ) );
231 #elif defined( __APPLE__ )
233 if ( !( *pFunction ) ) {
238 if ( 0 == *pFunction ) {
246 HMODULE*
mod = (HMODULE*)handle;
248 long ll1 =
name.length();
249 for (
int i = 0; i <
mod->numSym; i++ ) {
250 long ll2 = strlen(
mod->sym[i].name );
251 if ( 0 != ::strncmp(
mod->sym[i].name,
name.c_str(), ( ll1 > ll2 ) ? ll1 : ll2 ) == 0 ) {
269 return ::GetLastError();
272 return static_cast<unsigned long>(
static_cast<unsigned int>( errno ) );
286 LPVOID lpMessageBuffer;
287 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
288 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
289 (LPTSTR)&lpMessageBuffer, 0, NULL );
290 errString = (
const char*)lpMessageBuffer;
292 ::LocalFree( lpMessageBuffer );
294 char* cerrString(
nullptr );
296 if ( error == 0xAFFEDEAD ) {
297 cerrString = ::dlerror();
298 if ( !cerrString ) cerrString = ::strerror( error );
300 cerrString = (
char*)
"Unknown error. No information found in strerror()!";
306 cerrString = ::strerror( error );
343 using namespace Gaudi;
378 return (
char**)&(
args[0] );
384 # pragma warning( disable : 4996 )
390 if ( ( env = getenv( var ) ) !=
nullptr ) {
400 if ( ( env = getenv( var ) ) !=
nullptr ) {
411 #if defined( __APPLE__ )
413 # include "crt_externs.h"
416 #if defined( _WIN32 )
417 # define environ _environ
418 #elif defined( __APPLE__ )
419 static char** environ = *_NSGetEnviron();
422 for (
int i = 0; environ[i] !=
nullptr; ++i ) { vars.
push_back( environ[i] ); }
430 # include <execinfo.h>
433 int System::backTrace( [[maybe_unused]]
void** addresses, [[maybe_unused]]
const int depth ) {
437 int count = backtrace( addresses, depth );
438 return count > 0 ? count : 0;
440 #else // windows and osx parts not implemented
448 const int totalOffset = offset + 2;
449 const int totalDepth = depth + totalOffset;
455 for (
int i = totalOffset; i < count; ++i ) {
456 void* addr =
nullptr;
476 if ( dladdr( addresses, &info ) && info.dli_fname && info.dli_fname[0] !=
'\0' ) {
477 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
479 lib = info.dli_fname;
480 addr = info.dli_saddr;
486 fnc = ( stat == 0 ) ? dmg.get() : symbol;
495 #else // not implemented for windows and osx
504 return value.
empty() ?
506 ::unsetenv(
name.c_str() ),
509 ::setenv(
name.c_str(), value.
c_str(), overwrite );
512 if ( value.
empty() ) {
514 return ::_putenv( (
name +
"=" ).c_str() );
516 if ( !getenv(
name.c_str() ) || overwrite ) {
518 return ::_putenv( (
name +
"=" + value ).c_str() );