From 85924ec3dc53f4545cee5fcfaa62f1a8f2e507a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Drahos=CC=8C?= Date: Mon, 1 Apr 2013 15:12:55 +0200 Subject: Fix for FreeBSD and Solaris --- CMakeLists.txt | 10 ++++++++-- src/loadlib_rel.c | 30 ++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5551629..5ce0881 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) 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 /* readlink */ #endif @@ -65,7 +66,12 @@ static void setprogdir (lua_State *L); #include #endif -static void setprogdir (lua_State *L) { +#if defined(__FreeBSD__) + #include + #include +#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); } } -- cgit v1.2.1