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

Commit 85924ec

Browse files
committed
Fix for FreeBSD and Solaris
1 parent bb79cdc commit 85924ec

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

CMakeLists.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,18 @@ endif ( )
7878
# Setup needed variables and libraries
7979
if ( LUA_USE_POSIX )
8080
# On POSIX Lua links to standard math library "m"
81-
list ( APPEND LIBS m )
81+
find_library ( MATH_LIBRARY NAMES m )
82+
if ( MATH_LIBRARY )
83+
list ( APPEND LIBS ${MATH_LIBRARY} )
84+
endif ( )
8285
endif ( )
8386

8487
if ( LUA_USE_DLOPEN )
8588
# Link to dynamic linker library "dl"
86-
list ( APPEND LIBS dl )
89+
find_library ( DL_LIBRARY NAMES dl )
90+
if ( DL_LIBRARY )
91+
list ( APPEND LIBS ${DL_LIBRARY} )
92+
endif ( )
8793
endif ( )
8894

8995
if ( LUA_WIN )

src/loadlib_rel.c

+24-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static void setprogdir (lua_State *L);
4747
/*
4848
** {=========================================================================
4949
** This determines the location of the executable for relative module loading
50+
** Modified by the LuaDist project for UNIX platforms
5051
** ==========================================================================
5152
*/
5253
#if defined(_WIN32) || defined(__CYGWIN__)
@@ -56,7 +57,7 @@ static void setprogdir (lua_State *L);
5657
#define _PATH_MAX PATH_MAX
5758
#endif
5859

59-
#if defined(__linux__)
60+
#if defined(__linux__) || defined(__sun)
6061
#include <unistd.h> /* readlink */
6162
#endif
6263

@@ -65,7 +66,12 @@ static void setprogdir (lua_State *L);
6566
#include <mach-o/dyld.h>
6667
#endif
6768

68-
static void setprogdir (lua_State *L) {
69+
#if defined(__FreeBSD__)
70+
#include <sys/types.h>
71+
#include <sys/sysctl.h>
72+
#endif
73+
74+
static void setprogdir(lua_State *L) {
6975
char progdir[_PATH_MAX + 1];
7076
char *lb;
7177
int nsize = sizeof(progdir)/sizeof(char);
@@ -80,7 +86,22 @@ static void setprogdir (lua_State *L) {
8086
#elif defined(__linux__)
8187
n = readlink("/proc/self/exe", progdir, nsize);
8288
if (n > 0) progdir[n] = 0;
89+
#elif defined(__sun)
90+
pid_t pid = getpid();
91+
char linkname[256];
92+
sprintf(linkname, "/proc/%d/path/a.out", pid);
93+
n = readlink(linkname, progdir, nsize);
94+
if (n > 0) progdir[n] = 0;
8395
#elif defined(__FreeBSD__)
96+
int mib[4];
97+
mib[0] = CTL_KERN;
98+
mib[1] = KERN_PROC;
99+
mib[2] = KERN_PROC_PATHNAME;
100+
mib[3] = -1;
101+
size_t cb = sizeof(progdir);
102+
sysctl(mib, 4, progdir, &cb, NULL, 0);
103+
n = cb;
104+
#elif defined(__BSD__)
84105
n = readlink("/proc/curproc/file", progdir, nsize);
85106
if (n > 0) progdir[n] = 0;
86107
#elif defined(__APPLE__)
@@ -107,12 +128,9 @@ static void setprogdir (lua_State *L) {
107128
luaL_error(L, "unable to get process executable path");
108129
else {
109130
*lb = '\0';
110-
// Set progdir global
111-
lua_pushstring(L, progdir);
112-
lua_setglobal(L, "_PROGDIR");
113131

114132
// Replace the relative path placeholder
115-
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, progdir);
133+
luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, progdir);
116134
lua_remove(L, -2);
117135
}
118136
}

0 commit comments

Comments
 (0)