diff options
Diffstat (limited to 'src/loadlib_rel.c')
-rw-r--r-- | src/loadlib_rel.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/loadlib_rel.c b/src/loadlib_rel.c index 5ba8d23..2c7d59d 100644 --- a/src/loadlib_rel.c +++ b/src/loadlib_rel.c @@ -47,6 +47,7 @@ static void setprogdir (lua_State *L); /* ** {========================================================================= ** This determines the location of the executable for relative module loading +** Modified by the LuaDist project for UNIX platforms ** ========================================================================== */ #if defined(_WIN32) || defined(__CYGWIN__) @@ -56,7 +57,7 @@ static void setprogdir (lua_State *L); #define _PATH_MAX PATH_MAX #endif -#if defined(__linux__) +#if defined(__linux__) || defined(__sun) #include <unistd.h> /* readlink */ #endif @@ -65,7 +66,12 @@ static void setprogdir (lua_State *L); #include <mach-o/dyld.h> #endif -static void setprogdir (lua_State *L) { +#if defined(__FreeBSD__) + #include <sys/types.h> + #include <sys/sysctl.h> +#endif + +static void setprogdir(lua_State *L) { char progdir[_PATH_MAX + 1]; char *lb; int nsize = sizeof(progdir)/sizeof(char); @@ -80,7 +86,22 @@ static void setprogdir (lua_State *L) { #elif defined(__linux__) n = readlink("/proc/self/exe", progdir, nsize); if (n > 0) progdir[n] = 0; +#elif defined(__sun) + pid_t pid = getpid(); + char linkname[256]; + sprintf(linkname, "/proc/%d/path/a.out", pid); + n = readlink(linkname, progdir, nsize); + if (n > 0) progdir[n] = 0; #elif defined(__FreeBSD__) + int mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; + size_t cb = sizeof(progdir); + sysctl(mib, 4, progdir, &cb, NULL, 0); + n = cb; +#elif defined(__BSD__) n = readlink("/proc/curproc/file", progdir, nsize); if (n > 0) progdir[n] = 0; #elif defined(__APPLE__) @@ -107,12 +128,9 @@ static void setprogdir (lua_State *L) { luaL_error(L, "unable to get process executable path"); else { *lb = '\0'; - // Set progdir global - lua_pushstring(L, progdir); - lua_setglobal(L, "_PROGDIR"); // Replace the relative path placeholder - luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, progdir); + luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, progdir); lua_remove(L, -2); } } |