From 4e3ac55ef3d73e10b0587bdfccbb49139827a332 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 2 Jun 2010 00:41:33 +0200 Subject: [PATCH] Add a Windows-specific fallback to getenv("HOME"); This fixes msysGit issue 482 properly. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 18 ++++++++++++++++++ compat/mingw.h | 3 +++ git-compat-util.h | 4 ++++ path.c | 4 ++-- shell.c | 2 +- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 528a0d49f2..38b5807f4b 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1831,3 +1831,21 @@ pid_t waitpid(pid_t pid, int *status, int options) errno = EINVAL; return -1; } + +const char *get_windows_home_directory(void) +{ + static const char *home_directory = NULL; + struct strbuf buf = STRBUF_INIT; + + if (home_directory) + return home_directory; + + home_directory = getenv("HOME"); + if (home_directory && *home_directory) + return home_directory; + + strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH")); + home_directory = strbuf_detach(&buf, NULL); + + return home_directory; +} diff --git a/compat/mingw.h b/compat/mingw.h index 75524124a9..2fcc935b78 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -350,3 +350,6 @@ static int mingw_main(c,v) * Used by Pthread API implementation for Windows */ extern int err_win_to_posix(DWORD winerr); + +extern const char *get_windows_home_directory(); +#define get_home_directory() get_windows_home_directory() diff --git a/git-compat-util.h b/git-compat-util.h index b701a63926..4de61f04d1 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -648,4 +648,8 @@ struct passwd *xgetpwuid_self(void); #define mark_as_git_dir(x) /* noop */ #endif +#ifndef get_home_directory +#define get_home_directory() getenv("HOME") +#endif + #endif diff --git a/path.c b/path.c index cbbdf7d6ba..c42a6876eb 100644 --- a/path.c +++ b/path.c @@ -132,7 +132,7 @@ char *git_path(const char *fmt, ...) void home_config_paths(char **global, char **xdg, char *file) { char *xdg_home = getenv("XDG_CONFIG_HOME"); - char *home = getenv("HOME"); + const char *home = get_home_directory(); char *to_free = NULL; if (!home) { @@ -273,7 +273,7 @@ char *expand_user_path(const char *path) const char *username = path + 1; size_t username_len = first_slash - username; if (username_len == 0) { - const char *home = getenv("HOME"); + const char *home = get_home_directory(); if (!home) goto return_null; strbuf_add(&user_path, home, strlen(home)); diff --git a/shell.c b/shell.c index 84b237fef3..06a394c053 100644 --- a/shell.c +++ b/shell.c @@ -54,7 +54,7 @@ static char *make_cmd(const char *prog) static void cd_to_homedir(void) { - const char *home = getenv("HOME"); + const char *home = get_home_directory(); if (!home) die("could not determine user's home directory; HOME is unset"); if (chdir(home) == -1) -- 2.11.4.GIT