diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index fa809bec..19bc8610 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -55,6 +55,12 @@ #include #endif +#ifndef _WIN32 +#include +#include +#include +#endif + #include #include #include @@ -109,6 +115,28 @@ int extractUnsignedNumber(const std::string& in, T& out) { } +std::string getHomePath() { + + std::string home; + const char* h = NULL; +#ifdef _WIN32 + h = getenv("USERPROFILE"); +#else + h = getenv("HOME"); +#endif + if (h) { + home.assign(h); + } else { +#ifndef _WIN32 + uid_t uid = geteuid(); + struct passwd* pw = getpwuid(uid); + if (pw) { + home.assign(pw->pw_dir); + } +#endif + } + return home; +} } @@ -240,11 +268,7 @@ string expandFilename(const string &filename) { string retval; size_t pos = filename.find_first_not_of(" \t"); if (pos != string::npos && filename[pos] == '~') { -#ifdef _WIN32 - retval = getenv("USERPROFILE"); -#else - retval = getenv("HOME"); -#endif + retval = getHomePath(); if (pos + 1 < filename.size()) { // copy from the character after '~' retval += static_cast(filename.c_str() + pos + 1); diff --git a/src/main.cc b/src/main.cc index 0639e304..435e9f42 100644 --- a/src/main.cc +++ b/src/main.cc @@ -239,13 +239,10 @@ struct Options { if (env && strlen(env) > 0) { session_display.assign(env); } -#ifdef _WIN32 - env = getenv("USERPROFILE"); -#else - env = getenv("HOME"); -#endif - if (env && strlen(env) > 0) { - rc_path.assign(std::string(env) + "/." + realProgramName("fluxbox")); + + rc_path = FbTk::StringUtil::expandFilename(std::string("~/.") + realProgramName("fluxbox")); + + if (!rc_path.empty()) { rc_file = rc_path + "/init"; } } diff --git a/src/tests/StringUtiltest.cc b/src/tests/StringUtiltest.cc index e5e84193..a821184e 100644 --- a/src/tests/StringUtiltest.cc +++ b/src/tests/StringUtiltest.cc @@ -46,11 +46,16 @@ void testStringtok() { void testExpandFilename() { string filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4")); cerr<<"test "; - string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4"; - if (test == filename) - cerr<<"ok."; - else - cerr<<"faild"; + const char* home = getenv("HOME"); + if (home) { + string test = string(home)+"/filename/~filename2/file3~/file4"; + if (test == filename) + cerr<<"ok."; + else + cerr<<"failed"; + } else { + cerr << "failed, can't get $HOME."; + } cerr<