Fix own path detection on FreeBSD

This commit is contained in:
Dennis 2020-06-01 16:23:21 +02:00
parent 38bd9b3326
commit fe554ee4aa
1 changed files with 11 additions and 2 deletions

View File

@ -9,6 +9,8 @@
#include <unistd.h> #include <unistd.h>
#elif __APPLE__ #elif __APPLE__
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#elif __FreeBSD__
#include <stdlib.h>
#endif #endif
@ -26,7 +28,7 @@ static double get_scale(void) {
} }
static void get_exe_filename(char *buf, int sz) { static void get_exe_filename(const char *self, char *buf, int sz) {
#if _WIN32 #if _WIN32
int len = GetModuleFileName(NULL, buf, sz - 1); int len = GetModuleFileName(NULL, buf, sz - 1);
buf[len] = '\0'; buf[len] = '\0';
@ -38,6 +40,8 @@ static void get_exe_filename(char *buf, int sz) {
#elif __APPLE__ #elif __APPLE__
unsigned size = sz; unsigned size = sz;
_NSGetExecutablePath(buf, &size); _NSGetExecutablePath(buf, &size);
#elif __FreeBSD__
realpath(self, buf);
#else #else
strcpy(buf, "./lite"); strcpy(buf, "./lite");
#endif #endif
@ -111,8 +115,13 @@ int main(int argc, char **argv) {
lua_pushnumber(L, get_scale()); lua_pushnumber(L, get_scale());
lua_setglobal(L, "SCALE"); lua_setglobal(L, "SCALE");
#ifdef __FreeBSD__
char exename[PATH_MAX];
#else
char exename[2048]; char exename[2048];
get_exe_filename(exename, sizeof(exename)); #endif
get_exe_filename(argv[0], exename, sizeof(exename));
lua_pushstring(L, exename); lua_pushstring(L, exename);
lua_setglobal(L, "EXEFILE"); lua_setglobal(L, "EXEFILE");