diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fileops.c | 55 |
1 files changed, 25 insertions, 30 deletions
diff --git a/src/fileops.c b/src/fileops.c index 3f9e987f0..6342b1679 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -404,44 +404,39 @@ int git_futils_find_system_file(git_buf *path, const char *filename) int git_futils_find_global_file(git_buf *path, const char *filename) { - const char *home = getenv("HOME"); - #ifdef GIT_WIN32 struct win32_path root; - - if (home != NULL) { - if (git_buf_joinpath(path, home, filename) < 0) - return -1; - - if (git_path_exists(path->ptr)) { + static const wchar_t *tmpls[4] = { + L"%HOME%\\", + L"%HOMEDRIVE%%HOMEPATH%\\", + L"%USERPROFILE%\\", + NULL, + }; + const wchar_t **tmpl; + + for (tmpl = tmpls; *tmpl != NULL; tmpl++) { + /* try to expand environment variable, skipping if not set */ + if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%') + continue; + + /* try to look up file under path */ + if (!win32_find_file(path, &root, filename)) return 0; - } - } - if (getenv("HOMEPATH") != NULL) { - if (win32_expand_path(&root, L"%HOMEDRIVE%%HOMEPATH%\\") < 0 || - root.path[0] == L'%') /* i.e. no expansion happened */ - { - giterr_set(GITERR_OS, "Cannot locate the user's profile directory"); - return GIT_ENOTFOUND; - } - } else { - if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 || - root.path[0] == L'%') /* i.e. no expansion happened */ - { - giterr_set(GITERR_OS, "Cannot locate the user's profile directory"); - return GIT_ENOTFOUND; - } + /* No error if file not found under %HOME%, b/c we don't trust it, + * but do error if another var is set and yet file is not found. + */ + if (tmpl != tmpls) + break; } - if (win32_find_file(path, &root, filename) < 0) { - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; - } + giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); + git_buf_clear(path); - return 0; + return GIT_ENOTFOUND; #else + const char *home = getenv("HOME"); + if (home == NULL) { giterr_set(GITERR_OS, "Global file lookup failed. " "Cannot locate the user's home directory"); |