14 #define SYSTEM_SYSTEM_CPP
26 #define strcasecmp _stricmp
27 #define strncasecmp _strnicmp
28 #define getpid _getpid
36 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
40 #include "sys/times.h"
45 #if defined(linux) || defined(__APPLE__)
47 #include <sys/utsname.h>
62 # if __GNUC__ < 3 || \
63 (__GNUC__ == 3 && (__GNUC_MINOR__ < 4 ))
65 # define __attribute__(x)
69 # define __attribute__(x)
77 void* mh = ::LoadLibrary( name.length() == 0 ?
System::exeName().c_str() : name.c_str());
80 const char*
path = name.c_str();
81 #if defined(linux) || defined(__APPLE__)
82 void *mh = ::dlopen(name.length() == 0 ? 0 :
path, RTLD_LAZY | RTLD_GLOBAL);
85 shl_t mh = ::shl_load(name.length() == 0 ? 0 :
path, BIND_IMMEDIATE | BIND_VERBOSE, 0);
86 HMODULE*
mod =
new HMODULE;
88 if ( 0 != ::shl_gethandle_r(mh, &mod->dsc) ) {
92 typedef void* (*___all)();
93 ___all _alloc = (___all)malloc;
94 mod->numSym = ::shl_getsymbols(mod->dsc.handle, TYPE_PROCEDURE, EXPORT_SYMBOLS, malloc, &mod->sym);
100 if ( 0 == *handle ) {
113 if ((dllName.length() != 0) &&
114 ::strncasecmp(dllName.data()+dllName.length()-len,
SHLIB_SUFFIX, len) != 0) {
119 return doLoad(dllName, handle);
126 if (name.length() == 0) {
132 if (
getEnv(name, imgName) ) {
137 #if defined(linux) || defined(__APPLE__)
138 dllName =
"lib" + dllName;
145 #if defined(linux) || defined(__APPLE__)
157 if ( !::FreeLibrary((HINSTANCE)handle) ) {
158 #elif defined(linux) || defined(__APPLE__)
167 HMODULE*
mod = (HMODULE*)handle;
168 if ( 0 == ::shl_unload( mod->dsc.handle ) ) {
183 *pFunction = (
EntryPoint)::GetProcAddress((HINSTANCE)handle, name.data());
184 if ( 0 == *pFunction ) {
190 *pFunction = (
EntryPoint)::dlsym(handle, name.c_str());
192 *pFunction = FuncPtrCast<EntryPoint>(::dlsym(handle, name.c_str()));
194 if ( 0 == *pFunction ) {
200 #elif defined(__APPLE__)
201 *pFunction = (
EntryPoint)::dlsym(handle, name.c_str());
205 *pFunction = (
EntryPoint)::dlsym(handle, sname.c_str());
207 if ( 0 == *pFunction ) {
215 HMODULE* mod = (HMODULE*)handle;
217 long ll1 = name.length();
218 for (
int i = 0;
i < mod->numSym;
i++ ) {
219 long ll2 = strlen(mod->sym[
i].name);
220 if ( 0 != ::strncmp(mod->sym[
i].name, name.c_str(), (ll1>ll2) ? ll1 : ll2)==0 ) {
238 return ::GetLastError();
241 return static_cast<unsigned long>(
static_cast<unsigned int>(
errno));
255 LPVOID lpMessageBuffer;
257 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
260 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
261 (LPTSTR) &lpMessageBuffer,
264 errString = (
const char*)lpMessageBuffer;
266 ::LocalFree( lpMessageBuffer );
270 if ( error == 0xAFFEDEAD ) {
271 cerrString = (
char*)::dlerror();
272 if ( 0 == cerrString ) {
273 cerrString = ::strerror(error);
275 if ( 0 == cerrString ) {
276 cerrString = (
char *)
"Unknown error. No information found in strerror()!";
284 cerrString = ::strerror(error);
299 if ( ::strncmp(class_name,
"class ", 6) == 0 ) {
303 if ( ::strncmp(class_name,
"struct ", 7) == 0 ) {
310 while( (loc = tmp.find(
"class ")) > 0 ) {
314 while( (loc = tmp.find(
"struct ")) > 0 ) {
323 while ( (off=result.find(
" *")) != std::string::npos ) {
324 result.replace(off, 2,
"*");
327 while ( (off=result.find(
" &")) != std::string::npos ) {
328 result.replace(off, 2,
"&");
331 #elif defined(__linux) || defined(__APPLE__)
332 if ( ::strlen(class_name) == 1 ) {
335 switch(class_name[0]) {
349 result =
"signed char";
352 result =
"unsigned char";
358 result =
"unsigned short";
364 result =
"unsigned int";
370 result =
"unsigned long";
373 result =
"long long";
376 result =
"unsigned long long";
382 result =
"unsigned __int128";
391 result =
"long double";
394 result =
"__float128";
405 if (realname == 0)
return class_name;
410 while( std::string::npos != pos ) {
411 result.replace( pos , 2 ,
"," ) ;
412 pos = result.find(
", ");
424 memset(buffer,0,
sizeof(buffer));
426 unsigned long len =
sizeof(buffer);
427 ::GetComputerName(buffer, &len);
429 ::gethostname(buffer,
sizeof(buffer));
443 if (uname(&ut) == 0) {
458 ut.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
461 ver << ut.dwMajorVersion <<
'.' << ut.dwMinorVersion;
465 if (uname(&ut) == 0) {
479 ::GetSystemInfo(&ut);
481 arch << ut.wProcessorArchitecture;
485 if (uname(&ut) == 0) {
497 if ( account ==
"" ) {
500 unsigned long buflen =
sizeof(buffer);
501 ::GetUserName(buffer, &buflen);
504 const char* acct = ::getlogin();
505 if ( 0 == acct ) acct = ::getenv(
"LOGNAME");
506 if ( 0 == acct ) acct = ::getenv(
"USER");
507 account = (acct) ? acct :
"Unknown";
525 if ( s_argvChars.
size() == 0 ) {
530 #pragma warning(push)
531 #pragma warning(disable:4996)
536 char *next, *tmp1, *tmp2;
537 for(LPTSTR cmd = ::GetCommandLine(); *cmd; cmd=next) {
538 memset(exe,0,
sizeof(exe));
539 while ( *cmd ==
' ' ) cmd++;
540 next=::strchr(cmd,
' ');
541 if ( !next ) next = cmd + strlen(cmd);
542 if ( (tmp1=::
strchr(cmd,
'\"')) > 0 && tmp1 < next ) {
543 tmp2 = ::strchr(++tmp1,
'\"');
546 if ( cmd < tmp1 ) strncpy(exe, cmd, tmp1-cmd-1);
547 strncpy(&exe[strlen(exe)], tmp1, tmp2-tmp1-1);
552 s_argvStrings.
erase(s_argvStrings.
begin(), s_argvStrings.
end());
557 strncpy(exe, cmd, next-cmd);
563 #elif defined(linux) || defined(__APPLE__)
564 sprintf(exe,
"/proc/%d/cmdline", ::getpid());
565 FILE *cmdLine = ::fopen(exe,
"r");
568 long len = fread(cmd,
sizeof(
char),
sizeof(cmd), cmdLine);
571 for (
char* token = cmd; token-cmd < len; token += strlen(token)+1 ) {
576 s_argvChars[0] = s_argvStrings[0].c_str();
591 return (
char**)&s_argvChars[0];
597 #pragma warning(disable:4996)
603 if ( (env = getenv(var)) != 0 ) {
613 if ( (env = getenv(var)) != 0 ) {
622 return getenv(var) != 0;
626 #if defined(__APPLE__)
628 #include "crt_externs.h"
632 # define environ _environ
633 #elif defined(__APPLE__)
634 static char **environ = *_NSGetEnviron();
637 for (
int i=0; environ[
i] != 0; ++
i) {
647 #include <execinfo.h>
656 int count = backtrace( addresses, depth );
663 #else // windows and osx parts not implemented
672 const int totalOffset = offset + 2;
673 const int totalDepth = depth + totalOffset;
677 void** addresses = (
void**) malloc(totalDepth*
sizeof(
void *));
678 if ( addresses != 0 ){
680 for (
int i = totalOffset;
i < count; ++
i) {
710 if ( dladdr( addresses, &info ) && info.dli_fname
711 && info.dli_fname[0] !=
'\0' ) {
712 const char* symbol = info.dli_sname
713 && info.dli_sname[0] !=
'\0' ? info.dli_sname : 0;
715 lib = info.dli_fname;
716 addr = info.dli_saddr;
722 fnc = (stat == 0) ? dmg : symbol;
732 #else // not implemented for windows and osx
743 return value.empty() ?
745 ::unsetenv(name.c_str()) , 0 :
747 ::setenv(name.c_str(),value.c_str(), overwrite);
750 if ( value.empty() ) {
752 return ::_putenv((name+
"=").c_str());
755 if ( !getenv(name.c_str()) || overwrite ) {
757 return ::_putenv((name+
"="+value).c_str());