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
70 # 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 ) {
271 return ::GetLastError();
274 return static_cast<unsigned long>(
static_cast<unsigned int>( errno ) );
288 LPVOID lpMessageBuffer;
289 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
290 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
291 (LPTSTR)&lpMessageBuffer, 0, NULL );
292 errString = (
const char*)lpMessageBuffer;
294 ::LocalFree( lpMessageBuffer );
296 char* cerrString(
nullptr );
298 if ( error == 0xAFFEDEAD ) {
299 cerrString = ::dlerror();
300 if ( !cerrString ) cerrString = ::strerror( error );
302 cerrString = (
char*)
"Unknown error. No information found in strerror()!";
308 cerrString = ::strerror( error );
345 using namespace Gaudi;
380 return (
char**)&(
args[0] );
386 # pragma warning( disable : 4996 )
392 if ( ( env = getenv( var ) ) !=
nullptr ) {
402 if ( ( env = getenv( var ) ) !=
nullptr ) {
413 #if defined( __APPLE__ )
415 # include <crt_externs.h>
418 #if defined( _WIN32 )
419 # define environ _environ
420 #elif defined( __APPLE__ )
421 static char** environ = *_NSGetEnviron();
424 for (
int i = 0; environ[i] !=
nullptr; ++i ) { vars.
push_back( environ[i] ); }
432 # include <execinfo.h>
435 int System::backTrace( [[maybe_unused]]
void** addresses, [[maybe_unused]]
const int depth ) {
439 int count = backtrace( addresses, depth );
440 return count > 0 ? count : 0;
442 #else // windows and osx parts not implemented
450 const size_t totalOffset = offset + 2;
451 const size_t totalDepth = depth + totalOffset;
457 for (
size_t i = totalOffset; i < count; ++i ) {
458 void* addr =
nullptr;
478 if ( dladdr( addresses, &info ) && info.dli_fname && info.dli_fname[0] !=
'\0' ) {
479 const char* symbol = info.dli_sname && info.dli_sname[0] !=
'\0' ? info.dli_sname :
nullptr;
481 lib = info.dli_fname;
482 addr = info.dli_saddr;
488 fnc = ( stat == 0 ) ? dmg.get() : symbol;
497 #else // not implemented for windows and osx
506 return value.
empty() ?
508 ::unsetenv(
name.c_str() ),
511 ::setenv(
name.c_str(), value.
c_str(), overwrite );
514 if ( value.
empty() ) {
516 return ::_putenv( (
name +
"=" ).c_str() );
518 if ( !getenv(
name.c_str() ) || overwrite ) {
520 return ::_putenv( (
name +
"=" + value ).c_str() );