[PATCH updated] Fallback on _NSGetExecutablePath to get the executable path if using argv[0] fails
- From
Jeremy Huddleston <jeremyhu@apple.com>
- Date
- Nov 29, 2010, 18:29 UTC
- Message-ID
- <DD9829B7-8851-41CF-BCC6-2676B6015028@apple.com>
- In-Reply-To
- <20101129171211.GL8037@burratino>
This adds better support for RUNTIME_PREFIX on Mac OS X. The previous codepath would only work if argv[0] contained the full path to the executable or $PATH already contained /path/to/libexec/git-core. We use _NSGetExecutablePath here to find the full path (and thus prepend the correct libexec/git-core to $PATH) in the case where argv[0] does not contain the full path to the executable.
Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com> Reviewed-by: Matt Wright <mww@apple.com> --- exec_cmd.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/exec_cmd.c b/exec_cmd.c index bf22570..182fd3a 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -3,6 +3,10 @@ #include "quote.h" #define MAX_ARGS 32 +#if defined(__APPLE__) && defined(RUNTIME_PREFIX) +#include <mach-o/dyld.h> +#endif + extern char **environ; static const char *argv_exec_path; static const char *argv0_path; @@ -53,6 +57,19 @@ const char *git_extract_argv0_path(const char *argv0) if (slash >= argv0) { argv0_path = xstrndup(argv0, slash - argv0); return slash + 1; +#if defined(__APPLE__) + } else { + char new_argv0[PATH_MAX]; + uint32_t new_argv0_s = PATH_MAX; + if(_NSGetExecutablePath(new_argv0, &new_argv0_s) == 0) { + slash = new_argv0 + strlen(new_argv0); + while (new_argv0 <= slash && !is_dir_sep(*slash)) + slash--; + + if (slash >= new_argv0) + argv0_path = xstrndup(new_argv0, slash - new_argv0); + } +#endif } return argv0;
-- 1.7.3.2