From 7b789fbeb628e7b1d30906f8e999e7b0e3c876c8 Mon Sep 17 00:00:00 2001 From: pabs3 Date: Wed, 15 Mar 2006 06:51:31 +0000 Subject: [PATCH] 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 --- Source/util.cpp | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/Source/util.cpp b/Source/util.cpp index 9def1437..252de218 100644 --- a/Source/util.cpp +++ b/Source/util.cpp @@ -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 }