@@ -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