aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2013-06-29 06:39:02 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2013-06-29 06:39:02 (GMT)
commitf464f24eb3a5872404f60356009f466d5f79f2b1 (patch)
tree285557bd93d1f1825f21ced9766e1bcfeb2f90fc
parent2efd4b823082efb45cb351c7185d510ccb1dd32a (diff)
downloadfluxbox-f464f24eb3a5872404f60356009f466d5f79f2b1.zip
fluxbox-f464f24eb3a5872404f60356009f466d5f79f2b1.tar.bz2
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.
-rw-r--r--src/FbTk/StringUtil.cc34
-rw-r--r--src/main.cc11
-rw-r--r--src/tests/StringUtiltest.cc15
-rw-r--r--util/fluxbox-update_configs.cc3
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 @@
55 #include <errno.h> 55 #include <errno.h>
56#endif 56#endif
57 57
58#ifndef _WIN32
59#include <unistd.h>
60#include <sys/types.h>
61#include <pwd.h>
62#endif
63
58#include <memory> 64#include <memory>
59#include <algorithm> 65#include <algorithm>
60#include <string> 66#include <string>
@@ -109,6 +115,28 @@ int extractUnsignedNumber(const std::string& in, T& out) {
109} 115}
110 116
111 117
118std::string getHomePath() {
119
120 std::string home;
121 const char* h = NULL;
122#ifdef _WIN32
123 h = getenv("USERPROFILE");
124#else
125 h = getenv("HOME");
126#endif
127 if (h) {
128 home.assign(h);
129 } else {
130#ifndef _WIN32
131 uid_t uid = geteuid();
132 struct passwd* pw = getpwuid(uid);
133 if (pw) {
134 home.assign(pw->pw_dir);
135 }
136#endif
137 }
138 return home;
139}
112 140
113} 141}
114 142
@@ -240,11 +268,7 @@ string expandFilename(const string &filename) {
240 string retval; 268 string retval;
241 size_t pos = filename.find_first_not_of(" \t"); 269 size_t pos = filename.find_first_not_of(" \t");
242 if (pos != string::npos && filename[pos] == '~') { 270 if (pos != string::npos && filename[pos] == '~') {
243#ifdef _WIN32 271 retval = getHomePath();
244 retval = getenv("USERPROFILE");
245#else
246 retval = getenv("HOME");
247#endif
248 if (pos + 1 < filename.size()) { 272 if (pos + 1 < filename.size()) {
249 // copy from the character after '~' 273 // copy from the character after '~'
250 retval += static_cast<const char *>(filename.c_str() + pos + 1); 274 retval += static_cast<const char *>(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 {
239 if (env && strlen(env) > 0) { 239 if (env && strlen(env) > 0) {
240 session_display.assign(env); 240 session_display.assign(env);
241 } 241 }
242#ifdef _WIN32 242
243 env = getenv("USERPROFILE"); 243 rc_path = FbTk::StringUtil::expandFilename(std::string("~/.") + realProgramName("fluxbox"));
244#else 244
245 env = getenv("HOME"); 245 if (!rc_path.empty()) {
246#endif
247 if (env && strlen(env) > 0) {
248 rc_path.assign(std::string(env) + "/." + realProgramName("fluxbox"));
249 rc_file = rc_path + "/init"; 246 rc_file = rc_path + "/init";
250 } 247 }
251 } 248 }
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() {
46void testExpandFilename() { 46void testExpandFilename() {
47 string filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4")); 47 string filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4"));
48 cerr<<"test "; 48 cerr<<"test ";
49 string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4"; 49 const char* home = getenv("HOME");
50 if (test == filename) 50 if (home) {
51 cerr<<"ok."; 51 string test = string(home)+"/filename/~filename2/file3~/file4";
52 else 52 if (test == filename)
53 cerr<<"faild"; 53 cerr<<"ok.";
54 else
55 cerr<<"failed";
56 } else {
57 cerr << "failed, can't get $HOME.";
58 }
54 cerr<<endl; 59 cerr<<endl;
55} 60}
56 61
diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc
index a8052a2..8eff8e4 100644
--- a/util/fluxbox-update_configs.cc
+++ b/util/fluxbox-update_configs.cc
@@ -64,7 +64,6 @@ using std::set;
64using std::map; 64using std::map;
65using std::list; 65using std::list;
66using std::exit; 66using std::exit;
67using std::getenv;
68 67
69string read_file(const string& filename); 68string read_file(const string& filename);
70void write_file(const string& filename, const string &contents); 69void write_file(const string& filename, const string &contents);
@@ -615,7 +614,7 @@ int main(int argc, char **argv) {
615 } 614 }
616 615
617 if (rc_filename.empty()) 616 if (rc_filename.empty())
618 rc_filename = getenv("HOME") + string("/.fluxbox/init"); 617 rc_filename = FbTk::StringUtil::expandFilename("~/.fluxbox/init");
619 618
620 FbTk::ResourceManager resource_manager(rc_filename.c_str(),false); 619 FbTk::ResourceManager resource_manager(rc_filename.c_str(),false);
621 if (!resource_manager.load(rc_filename.c_str())) { 620 if (!resource_manager.load(rc_filename.c_str())) {