From 2dc357ea288152090ea36019a0279cdf9c4f6ef1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 15 Nov 2003 15:42:40 +0000 Subject: [PATCH] expand the value of HOME env var in wxGetHomeDir() because it may contain env vars inside it git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@24568 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/utils.cpp | 103 +++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 89c2b89a0b..7da8065ba3 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -34,6 +34,8 @@ #include "wx/apptrait.h" #include "wx/dynload.h" +#include "wx/confbase.h" // for wxExpandEnvVars() + #include "wx/msw/private.h" // includes #include "wx/msw/missing.h" // CHARSET_HANGUL @@ -92,6 +94,11 @@ #endif #endif +// 260 was taken from windef.h +#ifndef MAX_PATH + #define MAX_PATH 260 +#endif + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -354,8 +361,9 @@ error: const wxChar* wxGetHomeDir(wxString *pstr) { - wxString& strDir = *pstr; + wxString& strDir = *pstr; + // first branch is for Cygwin #if defined(__UNIX__) const wxChar *szHome = wxGetenv("HOME"); if ( szHome == NULL ) { @@ -371,34 +379,33 @@ const wxChar* wxGetHomeDir(wxString *pstr) strDir << wxT('/'); #ifdef __CYGWIN__ - // Cygwin returns unix type path but that does not work well - static wxChar windowsPath[MAX_PATH]; - cygwin_conv_to_full_win32_path(strDir, windowsPath); - strDir = windowsPath; + // Cygwin returns unix type path but that does not work well + static wxChar windowsPath[MAX_PATH]; + cygwin_conv_to_full_win32_path(strDir, windowsPath); + strDir = windowsPath; #endif #elif defined(__WXWINCE__) // Nothing #else - #ifdef __WIN32__ - strDir.clear(); + strDir.clear(); - // If we have a valid HOME directory, as is used on many machines that - // have unix utilities on them, we should use that. - const wxChar *szHome = wxGetenv(wxT("HOME")); + // If we have a valid HOME directory, as is used on many machines that + // have unix utilities on them, we should use that. + const wxChar *szHome = wxGetenv(wxT("HOME")); - if ( szHome != NULL ) - { + if ( szHome != NULL ) + { strDir = szHome; - } - else // no HOME, try HOMEDRIVE/PATH - { - szHome = wxGetenv(wxT("HOMEDRIVE")); - if ( szHome != NULL ) + } + else // no HOME, try HOMEDRIVE/PATH + { + szHome = wxGetenv(wxT("HOMEDRIVE")); + if ( szHome != NULL ) strDir << szHome; - szHome = wxGetenv(wxT("HOMEPATH")); + szHome = wxGetenv(wxT("HOMEPATH")); - if ( szHome != NULL ) - { + if ( szHome != NULL ) + { strDir << szHome; // the idea is that under NT these variables have default values @@ -408,44 +415,38 @@ const wxChar* wxGetHomeDir(wxString *pstr) // to set HOMEPATH to something other than "\\", we suppose that he // knows what he is doing and use the supplied value. if ( wxStrcmp(szHome, wxT("\\")) == 0 ) - strDir.clear(); - } - } + strDir.clear(); + } + } - if ( strDir.empty() ) - { - // If we have a valid USERPROFILE directory, as is the case in - // Windows NT, 2000 and XP, we should use that as our home directory. - szHome = wxGetenv(wxT("USERPROFILE")); + if ( strDir.empty() ) + { + // If we have a valid USERPROFILE directory, as is the case in + // Windows NT, 2000 and XP, we should use that as our home directory. + szHome = wxGetenv(wxT("USERPROFILE")); - if ( szHome != NULL ) + if ( szHome != NULL ) strDir = szHome; - } - - if ( !strDir.empty() ) - { - return strDir.c_str(); - } - //else: fall back to the prograrm directory - #else // Win16 - // Win16 has no idea about home, so use the executable directory instead - #endif // WIN16/32 - - // 260 was taken from windef.h - #ifndef MAX_PATH - #define MAX_PATH 260 - #endif - - wxString strPath; - ::GetModuleFileName(::GetModuleHandle(NULL), - wxStringBuffer(strPath, MAX_PATH), MAX_PATH); + } - // extract the dir name - wxSplitPath(strPath, &strDir, NULL, NULL); + if ( !strDir.empty() ) + { + // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the + // value once again, it shouldn't hurt anyhow + strDir = wxExpandEnvVars(strDir); + } + else // fall back to the program directory + { + wxString strPath; + ::GetModuleFileName(::GetModuleHandle(NULL), + wxStringBuffer(strPath, MAX_PATH), MAX_PATH); + // extract the dir name + wxSplitPath(strPath, &strDir, NULL, NULL); + } #endif // UNIX/Win - return strDir.c_str(); + return strDir.c_str(); } wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) -- 2.45.2