Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Fix for FreeBSD and Solaris
Browse files Browse the repository at this point in the history
  • Loading branch information
drahosp committed Apr 1, 2013
1 parent bb79cdc commit 85924ec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,18 @@ endif ( )
# Setup needed variables and libraries
if ( LUA_USE_POSIX )
# On POSIX Lua links to standard math library "m"
list ( APPEND LIBS m )
find_library ( MATH_LIBRARY NAMES m )
if ( MATH_LIBRARY )
list ( APPEND LIBS ${MATH_LIBRARY} )
endif ( )
endif ( )

if ( LUA_USE_DLOPEN )
# Link to dynamic linker library "dl"
list ( APPEND LIBS dl )
find_library ( DL_LIBRARY NAMES dl )
if ( DL_LIBRARY )
list ( APPEND LIBS ${DL_LIBRARY} )
endif ( )
endif ( )

if ( LUA_WIN )
Expand Down
30 changes: 24 additions & 6 deletions src/loadlib_rel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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

Expand All @@ -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);
Expand All @@ -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__)
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 85924ec

Please sign in to comment.