summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeffery Myers <[email protected]>2022-01-14 11:16:25 -0800
committerGitHub <[email protected]>2022-01-14 20:16:25 +0100
commitf4dea6919a34fa8c77cad8a589e5f4c20595988c (patch)
tree5e10c4d481fb17c9b68cb058864a447ebb31303c /src
parentf57727995a0676bae6692c376081a836db539917 (diff)
downloadraylib-f4dea6919a34fa8c77cad8a589e5f4c20595988c.tar.gz
raylib-f4dea6919a34fa8c77cad8a589e5f4c20595988c.zip
[CORE] Bug and formating fixes for GetApplicatonDir (#2285)
* Fix formating problems with GetApplicationDir. Don't ever return an empty string * always return a valid path even if it's ./ * remove the need for the dll and just use the normal GetModuleFileName function
Diffstat (limited to 'src')
-rw-r--r--src/rcore.c153
1 files changed, 63 insertions, 90 deletions
diff --git a/src/rcore.c b/src/rcore.c
index d8e96b7a..8e93709b 100644
--- a/src/rcore.c
+++ b/src/rcore.c
@@ -166,29 +166,13 @@
#ifndef MAX_PATH
#define MAX_PATH 1025
#endif
- void *LoadLibraryA(void *lpLibFileName);
- void *LoadLibraryW(void *lpLibFileName);
-
- #ifdef UNICODE
- #define LoadLibrary LoadLibraryW
- #else
- #define LoadLibrary LoadLibraryA
- #endif // !UNICODE
-
- void *GetProcAddress(void *hModule, void *lpProcName);
-
- void *GetCurrentProcess(void);
- bool FreeLibrary(void *hLibModule);
-
+ unsigned int GetModuleFileNameA( void* hModule, const char* lpFilename, unsigned int nSize);
+ unsigned int GetModuleFileNameW( void* hModule, const unsigned short* lpFilename, unsigned int nSize);
int WideCharToMultiByte(unsigned int cp, unsigned long flags, const unsigned short *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
-
- const char pathDelim = '\\';
#elif defined(__linux__)
#include <unistd.h>
- const char pathDelim = '/';
#elif defined(__APPLE__)
#include <sys/syslimits.h>
- const char pathDelim = '/';
#endif // OSs
#endif // PLATFORM_DESKTOP
@@ -3003,86 +2987,75 @@ const char *GetWorkingDirectory(void)
const char *GetApplicationDirectory(void)
{
- static char appDir[MAX_FILEPATH_LENGTH] = { 0 };
- memset(appDir, 0, MAX_FILEPATH_LENGTH);
+ static char appDir[MAX_FILEPATH_LENGTH] = { 0 };
+ memset(appDir, 0, MAX_FILEPATH_LENGTH);
#if defined(_WIN32)
- typedef unsigned long(*GetModuleFileNameFunc)(void*, void*, void*, unsigned long);
-
- GetModuleFileNameFunc getModuleFileNameExWPtr = NULL;
- void *lib = LoadLibrary(L"psapi.dll");
-
- if (lib == NULL)
- {
- appDir[0] = '\\';
- }
- else
- {
+ int len = 0;
#if defined (UNICODE)
- getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExW");
+ unsigned short widePath[MAX_PATH];
+ len = GetModuleFileNameW(NULL, widePath, MAX_PATH);
+ len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL);
#else
- getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExA");
+ len = GetModuleFileNameA(NULL, appDir, MAX_PATH);
#endif
+ if (len > 0)
+ {
+ for (int i = len; i >= 0; --i)
+ {
+ if (appDir[i] == '\\')
+ {
+ appDir[i + 1] = '\0';
+ i = -1;
+ }
+ }
+ }
+ else
+ {
+ appDir[0] = '.';
+ appDir[1] = '\\';
+ }
- if (getModuleFileNameExWPtr == NULL)
- {
- appDir[0] = '\\';
- }
- else
- {
- int len = 0;
-#if defined (UNICODE)
- unsigned short widePath[MAX_PATH];
- len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, widePath, MAX_PATH);
- len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL);
-#else
- len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, appDir, MAX_PATH);
-#endif
- if (len > 0)
- {
- for (int i = len; i >= 0; --i)
- {
- if (appDir[i] == '\\')
- {
- appDir[i + 1] = '\0';
- i = -1;
- }
- }
- }
- }
-
- FreeLibrary(lib);
- }
#elif defined(__linux__)
- unsigned int size = sizeof(appDir);
- ssize_t len = readlink("/proc/self/exe", appDir, size);
-
- if (len > 0)
- {
- for (int i = len; i >= 0; --i)
- {
- if (appDir[i] == '/')
- {
- appDir[i + 1] = '\0';
- i = -1;
- }
- }
- }
+ unsigned int size = sizeof(appDir);
+ ssize_t len = readlink("/proc/self/exe", appDir, size);
+
+ if (len > 0)
+ {
+ for (int i = len; i >= 0; --i)
+ {
+ if (appDir[i] == '/')
+ {
+ appDir[i + 1] = '\0';
+ i = -1;
+ }
+ }
+ }
+ else
+ {
+ appDir[0] = '.';
+ appDir[1] = '/';
+ }
#elif defined(__APPLE__)
- uint32_t size = sizeof(appDir);
-
- if (_NSGetExecutablePath(appDir, &size) == 0)
- {
- int len = strlen(appDir);
- for (int i = len; i >= 0; --i)
- {
- if (appDir[i] == '/')
- {
- appDir[i + 1] = '\0';
- i = -1;
- }
- }
- }
+ uint32_t size = sizeof(appDir);
+
+ if (_NSGetExecutablePath(appDir, &size) == 0)
+ {
+ int len = strlen(appDir);
+ for (int i = len; i >= 0; --i)
+ {
+ if (appDir[i] == '/')
+ {
+ appDir[i + 1] = '\0';
+ i = -1;
+ }
+ }
+ }
+ else
+ {
+ appDir[0] = '.';
+ appDir[1] = '/';
+ }
#endif
return appDir;