Make get_executable_path more portable and correct.
git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@4592 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
parent
70f42f9901
commit
7b789fbeb6
1 changed files with 36 additions and 4 deletions
|
@ -644,13 +644,45 @@ string get_file_name(const string& path) {
|
|||
|
||||
string get_executable_path(const char* argv0) {
|
||||
#ifdef _WIN32
|
||||
char temp_buf[1024];
|
||||
char temp_buf[MAX_PATH+1];
|
||||
temp_buf[0] = '\0';
|
||||
int rc = GetModuleFileName(NULL,temp_buf,1024);
|
||||
int rc = GetModuleFileName(NULL,temp_buf,MAX_PATH);
|
||||
assert(rc != 0);
|
||||
return string(temp_buf);
|
||||
#else
|
||||
return get_full_path(argv0);
|
||||
#elif __APPLE__
|
||||
char temp_buf[MAXPATHLEN+1];
|
||||
unsigned long buf_len = MAXPATHLEN;
|
||||
int rc = _NSGetExecutablePath(temp_buf, &buf_len);
|
||||
assert(rc == 0);
|
||||
return string(temp_buf);
|
||||
#else /* Linux/BSD/POSIX/etc */
|
||||
const char *path = getenv("_");
|
||||
if( path != NULL ) return get_full_path( path );
|
||||
else {
|
||||
char* pathtmp;
|
||||
size_t len = 100;
|
||||
int nchars;
|
||||
while(1){
|
||||
pathtmp = (char*)realloc(path,len+1);
|
||||
if( pathtmp == NULL ){
|
||||
free(path);
|
||||
return get_full_path(argv0);
|
||||
}
|
||||
path = pathtmp;
|
||||
nchars = readlink("/proc/self/exe", path, len);
|
||||
if( nchars < 0 ){
|
||||
free(path);
|
||||
return get_full_path(argv0);
|
||||
}
|
||||
if( nchars < len ){
|
||||
path[nchars] = '\0';
|
||||
string result(path);
|
||||
free(path);
|
||||
return result;
|
||||
}
|
||||
len *= 2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue