[winpr,library] implement GetModuleFileNameA

This commit is contained in:
akallabeth 2024-09-17 17:43:21 +02:00
parent accb4eae75
commit 347ac5a941
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5

View File

@ -77,6 +77,10 @@
#include <mach-o/dyld.h>
#endif
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
#endif
DLL_DIRECTORY_COOKIE AddDllDirectory(PCWSTR NewDirectory)
@ -265,18 +269,11 @@ DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
return status;
}
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
#if defined(__linux__) || defined(__NetBSD__) || defined(__DragonFly__)
static DWORD module_from_proc(const char* proc, LPSTR lpFilename, DWORD nSize)
{
#if defined(__linux__)
SSIZE_T status = 0;
size_t length = 0;
char path[64];
if (!hModule)
{
char buffer[4096] = { 0 };
(void)sprintf_s(path, ARRAYSIZE(path), "/proc/%d/exe", getpid());
status = readlink(path, buffer, ARRAYSIZE(buffer) - 1);
char buffer[8192] = { 0 };
ssize_t status = readlink(proc, buffer, ARRAYSIZE(buffer) - 1);
if ((status < 0) || ((size_t)status >= ARRAYSIZE(buffer)))
{
@ -284,7 +281,7 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
return 0;
}
length = strnlen(buffer, ARRAYSIZE(buffer));
const size_t length = strnlen(buffer, ARRAYSIZE(buffer));
if (length < nSize)
{
@ -297,8 +294,32 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
lpFilename[nSize - 1] = '\0';
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return nSize;
}
#endif
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
{
#if defined(__linux__)
if (!hModule)
return module_from_proc("/proc/self/exe", lpFilename, nSize);
#elif defined(__FreeBSD__)
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t cb = nSize;
const int rc = sysctl(mib, ARRAYSIZE(mib), lpFilename, &cb, NULL, 0);
if ((rc != 0) || (cb > nSize))
{
SetLastError(ERROR_INTERNAL_ERROR);
return 0;
}
return (DWORD)cb;
#elif defined(__NetBSD__)
if (!hModule)
return module_from_proc("/proc/curproc/exe", lpFilename, nSize);
#elif defined(__DragonFly__)
if (!hModule)
return module_from_proc("/proc/curproc/file", lpFilename, nSize);
#elif defined(__MACOSX__)
int status;
size_t length;