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.
main
Christian Göttsche 4 years ago committed by BenBE
parent 5aa207f9af
commit 62d59403d9
  1. 8
      Settings.c
  2. 7
      pcp/PCPDynamicColumn.c
  3. 10
      pcp/PCPDynamicMeter.c

@ -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;

@ -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 */

@ -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 */

Loading…
Cancel
Save