From 62d59403d92abaf256604bfc17e76c85ce64ea35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 14 Aug 2022 19:58:03 +0200 Subject: Add fallback for HOME environment variable If the environment variable HOME is not set, try to get the home directory from the systems password database. --- Settings.c | 8 +++++--- pcp/PCPDynamicColumn.c | 7 +++++++ pcp/PCPDynamicMeter.c | 10 +++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Settings.c b/Settings.c index 7d6fca47..aefd9e3c 100644 --- a/Settings.c +++ b/Settings.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -702,9 +703,10 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns) this->filename = xStrdup(rcfile); } else { const char* home = getenv("HOME"); - if (!home) - home = ""; - + if (!home) { + const struct passwd* pw = getpwuid(getuid()); + home = pw ? pw->pw_dir : ""; + } const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); char* configDir = NULL; char* htopDir = NULL; diff --git a/pcp/PCPDynamicColumn.c b/pcp/PCPDynamicColumn.c index aab25253..33c6d72a 100644 --- a/pcp/PCPDynamicColumn.c +++ b/pcp/PCPDynamicColumn.c @@ -14,6 +14,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -194,6 +195,12 @@ void PCPDynamicColumns_init(PCPDynamicColumns* columns) { const char* home = getenv("HOME"); char* path; + if (!xdgConfigHome && !home) { + const struct passwd* pw = getpwuid(getuid()); + if (pw) + home = pw->pw_dir; + } + columns->table = Hashtable_new(0, true); /* developer paths - PCP_HTOP_DIR=./pcp ./pcp-htop */ diff --git a/pcp/PCPDynamicMeter.c b/pcp/PCPDynamicMeter.c index 7c55e4bf..e8999881 100644 --- a/pcp/PCPDynamicMeter.c +++ b/pcp/PCPDynamicMeter.c @@ -12,12 +12,14 @@ in the source distribution for its full text. #include #include #include -#include +#include #include #include #include #include +#include + #include "Macros.h" #include "Platform.h" #include "RichString.h" @@ -251,6 +253,12 @@ void PCPDynamicMeters_init(PCPDynamicMeters* meters) { const char* home = getenv("HOME"); char* path; + if (!xdgConfigHome && !home) { + const struct passwd* pw = getpwuid(getuid()); + if (pw) + home = pw->pw_dir; + } + meters->table = Hashtable_new(0, true); /* developer paths - PCP_HTOP_DIR=./pcp ./pcp-htop */ -- cgit v1.2.3