From f464f24eb3a5872404f60356009f466d5f79f2b1 Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Sat, 29 Jun 2013 08:39:02 +0200 Subject: fix detection of $HOME folder usually $HOME is set when fluxbox runs. in some rare scenarios (eg., fuzzying binaries to detect bugs) one could launch fluxbox by using 'env -i' and thus eliminating $HOME from the environment. to prevent crashes fluxbox uses now 'getpwuid()' when $HOME is not set to detect the home folder. --- src/FbTk/StringUtil.cc | 34 +++++++++++++++++++++++++++++----- src/main.cc | 11 ++++------- src/tests/StringUtiltest.cc | 15 ++++++++++----- util/fluxbox-update_configs.cc | 3 +-- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index fa809be..19bc861 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 0639e30..435e9f4 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 e5e8419..a821184 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<