From eb02fdde90c6a5d4eccb48b5a66a6ad9f40cd408 Mon Sep 17 00:00:00 2001 From: Martin Herkt Date: Thu, 12 Sep 2013 17:44:20 +0200 Subject: mpvcore/path: Fix config path handling on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, mpv incorrectly used the %HOME% environment variable on MinGW to determine the current user’s home directory. This is wrong; the correct variable to use would be %HOMEPATH%, which would however still be wrong since application data goes into the application data directory, not the user’s home. This patch makes it use the local AppData path instead of reading an environment variable. This however exposed another problem (which also affected users who actually had the %HOME% variable set): b2c2fe7a3782 (discussed in issue #95) introduced some changes that make mpv load user config files from the executable path on Windows. The problem with this change is that config_dir was still declared static, so once a config file had been found in the executable path, it would set config_dir to an empty string, so mpv would dump e.g. watch_later data straight into the user’s home. This commit also fixes that. One side effect of this is that mpv no longer considers the “mpv” subdirectory in the executable path (that behavior resulted from the homedir variable always being empty), unless it is somehow unable to determine the local AppData path. --- mpvcore/path.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/mpvcore/path.c b/mpvcore/path.c index 5d9b7841a4..6d7927e621 100644 --- a/mpvcore/path.c +++ b/mpvcore/path.c @@ -40,6 +40,7 @@ #if defined(__MINGW32__) #include +#include #elif defined(__CYGWIN__) #include #include @@ -78,9 +79,20 @@ char *mp_find_user_config_file(const char *filename) { char *homedir = NULL, *buff = NULL; #ifdef __MINGW32__ - static char *config_dir = "mpv"; + char *config_dir = "mpv"; + static char *homepath = NULL; + + if (homepath == NULL) { + char buf[MAX_PATH]; + if (SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, NULL, + SHGFP_TYPE_CURRENT, buf) == S_OK) { + + homepath = buf; + } + } #else - static char *config_dir = ".mpv"; + char *config_dir = ".mpv"; + static char *homepath = getenv("HOME"); #endif #if defined(__MINGW32__) || defined(__CYGWIN__) char *temp = NULL; @@ -88,15 +100,19 @@ char *mp_find_user_config_file(const char *filename) /* Hack to get fonts etc. loaded outside of Cygwin environment. */ int i, imax = 0; int len = (int)GetModuleFileNameA(NULL, exedir, 260); - for (i = 0; i < len; i++) + + for (i = 0; i < len; i++) { if (exedir[i] == '\\') { exedir[i] = '/'; imax = i; } + } + exedir[imax] = '\0'; - if (filename) + if (filename) { temp = mp_path_join(NULL, bstr0(exedir), bstr0(filename)); + } if (temp && mp_path_exists(temp) && !mp_path_isdir(temp)) { homedir = exedir; @@ -106,7 +122,7 @@ char *mp_find_user_config_file(const char *filename) #endif if ((homedir = getenv("MPV_HOME")) != NULL) { config_dir = ""; - } else if ((homedir = getenv("HOME")) == NULL) { + } else if ((homedir = homepath) == NULL) { #if defined(__MINGW32__) || defined(__CYGWIN__) homedir = exedir; #else -- cgit v1.2.3