Add support for path detection when using symlinks or lite is in $PATH

It turns out using argv[0] is a bad idea. This uses sysctl() to retrieve
its own path
This commit is contained in:
Dennis 2021-05-09 14:11:01 +02:00
parent 5d800b3964
commit 4f898083d1
1 changed files with 14 additions and 4 deletions

View File

@ -10,7 +10,8 @@
#elif __APPLE__ #elif __APPLE__
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#elif __FreeBSD__ #elif __FreeBSD__
#include <stdlib.h> #include <sys/sysctl.h>
#include <unistd.h>
#endif #endif
@ -28,7 +29,7 @@ static double get_scale(void) {
} }
static void get_exe_filename(const char *self, char *buf, int sz) { static void get_exe_filename(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';
@ -41,7 +42,16 @@ static void get_exe_filename(const char *self, char *buf, int sz) {
unsigned size = sz; unsigned size = sz;
_NSGetExecutablePath(buf, &size); _NSGetExecutablePath(buf, &size);
#elif __FreeBSD__ #elif __FreeBSD__
realpath(self, buf); int items[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PATHNAME,
getpid()
};
size_t len;
(void) sysctl(items, 4, buf, &len, NULL, 0);
buf[len] = '\0';
#else #else
strcpy(buf, "./lite"); strcpy(buf, "./lite");
#endif #endif
@ -121,7 +131,7 @@ int main(int argc, char **argv) {
char exename[2048]; char exename[2048];
#endif #endif
get_exe_filename(argv[0], exename, sizeof(exename)); get_exe_filename(exename, sizeof(exename));
lua_pushstring(L, exename); lua_pushstring(L, exename);
lua_setglobal(L, "EXEFILE"); lua_setglobal(L, "EXEFILE");