@@ -47,6 +47,7 @@ static void setprogdir (lua_State *L);
47
47
/*
48
48
** {=========================================================================
49
49
** This determines the location of the executable for relative module loading
50
+ ** Modified by the LuaDist project for UNIX platforms
50
51
** ==========================================================================
51
52
*/
52
53
#if defined(_WIN32 ) || defined(__CYGWIN__ )
@@ -56,7 +57,7 @@ static void setprogdir (lua_State *L);
56
57
#define _PATH_MAX PATH_MAX
57
58
#endif
58
59
59
- #if defined(__linux__ )
60
+ #if defined(__linux__ ) || defined( __sun )
60
61
#include <unistd.h> /* readlink */
61
62
#endif
62
63
@@ -65,7 +66,12 @@ static void setprogdir (lua_State *L);
65
66
#include <mach-o/dyld.h>
66
67
#endif
67
68
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 ) {
69
75
char progdir [_PATH_MAX + 1 ];
70
76
char * lb ;
71
77
int nsize = sizeof (progdir )/sizeof (char );
@@ -80,7 +86,22 @@ static void setprogdir (lua_State *L) {
80
86
#elif defined(__linux__ )
81
87
n = readlink ("/proc/self/exe" , progdir , nsize );
82
88
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 ;
83
95
#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__ )
84
105
n = readlink ("/proc/curproc/file" , progdir , nsize );
85
106
if (n > 0 ) progdir [n ] = 0 ;
86
107
#elif defined(__APPLE__ )
@@ -107,12 +128,9 @@ static void setprogdir (lua_State *L) {
107
128
luaL_error (L , "unable to get process executable path" );
108
129
else {
109
130
* lb = '\0' ;
110
- // Set progdir global
111
- lua_pushstring (L , progdir );
112
- lua_setglobal (L , "_PROGDIR" );
113
131
114
132
// 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 );
116
134
lua_remove (L , -2 );
117
135
}
118
136
}
0 commit comments