core-legacy/oslink.cpp
changeset 545 07e5df11f9fc
parent 541 35a8cda28485
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core-legacy/oslink.cpp	Fri Nov 14 05:02:00 2008 -0600
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* ======== SourceMM ========
     1.5 + * Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 + * No warranties of any kind
     1.7 + *
     1.8 + * License: zlib/libpng
     1.9 + *
    1.10 + * Author(s): David "BAILOPAN" Anderson
    1.11 + * ============================
    1.12 + */
    1.13 +
    1.14 +/**
    1.15 + * @brief Implements OS-dependant functions from oslink.h
    1.16 + * @file oslink.cpp
    1.17 + */
    1.18 +
    1.19 +#include <stdio.h>
    1.20 +#include "oslink.h"
    1.21 +#ifdef __linux
    1.22 +#include <errno.h>
    1.23 +#endif
    1.24 +
    1.25 +#if defined __WIN32__ || defined _WIN32 || defined WIN32
    1.26 +const char *dlerror()
    1.27 +{
    1.28 +	static char buf[1024];
    1.29 +	DWORD num;
    1.30 +
    1.31 +	num = GetLastError();
    1.32 +
    1.33 +	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    1.34 +						NULL,
    1.35 +						num,
    1.36 +						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    1.37 +						buf,
    1.38 +						sizeof(buf),
    1.39 +						NULL)
    1.40 +		== 0)
    1.41 +	{
    1.42 +		_snprintf(buf, sizeof(buf), "unknown error %x", num);
    1.43 +	}
    1.44 +
    1.45 +	return buf;
    1.46 +}
    1.47 +#endif
    1.48 +
    1.49 +#if defined __linux__
    1.50 +int GetLastError()
    1.51 +{
    1.52 +	return errno;
    1.53 +}
    1.54 +
    1.55 +extern "C" void __cxa_guard_acquire(void)
    1.56 +{
    1.57 +}
    1.58 +
    1.59 +extern "C" void __cxa_guard_release(void)
    1.60 +{
    1.61 +}
    1.62 +#endif
    1.63 +
    1.64 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    1.65 +{
    1.66 +#if defined WIN32 || defined _WIN32
    1.67 +	MEMORY_BASIC_INFORMATION mem;
    1.68 +	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    1.69 +		return false;
    1.70 +	if (mem.AllocationBase == NULL)
    1.71 +		return false;
    1.72 +	HMODULE dll = (HMODULE)mem.AllocationBase;
    1.73 +	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    1.74 +#elif defined __linux__
    1.75 +	Dl_info info;
    1.76 +	if (!dladdr(pAddr, &info))
    1.77 +		return false;
    1.78 +	if (!info.dli_fbase || !info.dli_fname)
    1.79 +		return false;
    1.80 +	const char *dllpath = info.dli_fname;
    1.81 +	snprintf(buffer, maxlength, "%s", dllpath);
    1.82 +#endif
    1.83 +	return true;
    1.84 +}