diff options
author | Christian Göttsche <cgzones@googlemail.com> | 2022-08-14 19:58:03 +0200 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2022-08-22 21:32:49 +0200 |
commit | 62d59403d92abaf256604bfc17e76c85ce64ea35 (patch) | |
tree | d863afd15fb7c091d826907f9b310803a6035e7f | |
parent | 5aa207f9af6622583fd60edebaef1c3ed19ab91d (diff) |
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.
-rw-r--r-- | Settings.c | 8 | ||||
-rw-r--r-- | pcp/PCPDynamicColumn.c | 7 | ||||
-rw-r--r-- | pcp/PCPDynamicMeter.c | 10 |
3 files changed, 21 insertions, 4 deletions
@@ -10,6 +10,7 @@ in the source distribution for its full text. #include <ctype.h> #include <errno.h> #include <limits.h> +#include <pwd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -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 <ctype.h> #include <dirent.h> #include <errno.h> +#include <pwd.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -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 <ctype.h> #include <dirent.h> #include <errno.h> -#include <pcp/pmapi.h> +#include <pwd.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <pcp/pmapi.h> + #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 */ |