mirror of https://github.com/xzeldon/htop.git
Move .htoprc to XDG-compliant path ~/.config/htop/htoprc,
respecting $XDG_CONFIG_HOME (thanks to Hadzhimurad Ustarkhan for the suggestion.)
This commit is contained in:
parent
5b0b2255ef
commit
93233a67ea
60
Settings.c
60
Settings.c
|
@ -5,6 +5,7 @@ Released under the GNU GPL, see the COPYING file
|
||||||
in the source distribution for its full text.
|
in the source distribution for its full text.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include "String.h"
|
#include "String.h"
|
||||||
#include "ProcessList.h"
|
#include "ProcessList.h"
|
||||||
|
@ -56,13 +57,10 @@ static void Settings_readMeterModes(Settings* this, char* line, HeaderSide side)
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool Settings_read(Settings* this, char* fileName, int cpuCount) {
|
static bool Settings_read(Settings* this, char* fileName, int cpuCount) {
|
||||||
// TODO: implement File object and make
|
FILE* fd = fopen(fileName, "r");
|
||||||
// file I/O object-oriented.
|
if (!fd)
|
||||||
FILE* fd;
|
|
||||||
fd = fopen(fileName, "r");
|
|
||||||
if (fd == NULL) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
const int maxLine = 2048;
|
const int maxLine = 2048;
|
||||||
char buffer[maxLine];
|
char buffer[maxLine];
|
||||||
bool readMeters = false;
|
bool readMeters = false;
|
||||||
|
@ -212,21 +210,47 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
|
||||||
Settings* this = malloc(sizeof(Settings));
|
Settings* this = malloc(sizeof(Settings));
|
||||||
this->pl = pl;
|
this->pl = pl;
|
||||||
this->header = header;
|
this->header = header;
|
||||||
const char* home;
|
char* legacyDotfile = NULL;
|
||||||
char* rcfile;
|
char* rcfile = getenv("HTOPRC");
|
||||||
home = getenv("HOME_ETC");
|
if (rcfile) {
|
||||||
if (!home) home = getenv("HOME");
|
this->userSettings = strdup(rcfile);
|
||||||
if (!home) home = "";
|
} else {
|
||||||
rcfile = getenv("HTOPRC");
|
const char* home = getenv("HOME");
|
||||||
if (!rcfile)
|
if (!home) home = "";
|
||||||
this->userSettings = String_cat(home, "/.htoprc");
|
const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
|
||||||
else
|
char* configDir = NULL;
|
||||||
this->userSettings = String_copy(rcfile);
|
char* htopDir = NULL;
|
||||||
|
if (xdgConfigHome) {
|
||||||
|
this->userSettings = String_cat(xdgConfigHome, "/htop/htoprc");
|
||||||
|
configDir = strdup(xdgConfigHome);
|
||||||
|
htopDir = String_cat(xdgConfigHome, "/htop");
|
||||||
|
} else {
|
||||||
|
this->userSettings = String_cat(home, "/.config/htop/htoprc");
|
||||||
|
configDir = String_cat(home, "/.config");
|
||||||
|
htopDir = String_cat(home, "/.config/htop");
|
||||||
|
}
|
||||||
|
legacyDotfile = String_cat(home, "/.htoprc");
|
||||||
|
mkdir(configDir, 0700);
|
||||||
|
mkdir(htopDir, 0700);
|
||||||
|
free(htopDir);
|
||||||
|
free(configDir);
|
||||||
|
if (access(legacyDotfile, R_OK) != 0) {
|
||||||
|
free(legacyDotfile);
|
||||||
|
legacyDotfile = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
this->colorScheme = 0;
|
this->colorScheme = 0;
|
||||||
this->changed = false;
|
this->changed = false;
|
||||||
this->delay = DEFAULT_DELAY;
|
this->delay = DEFAULT_DELAY;
|
||||||
bool ok = Settings_read(this, this->userSettings, cpuCount);
|
bool ok = Settings_read(this, legacyDotfile ? legacyDotfile : this->userSettings, cpuCount);
|
||||||
if (!ok) {
|
if (ok) {
|
||||||
|
if (legacyDotfile) {
|
||||||
|
// Transition to new location and delete old configuration file
|
||||||
|
if (Settings_write(this))
|
||||||
|
unlink(legacyDotfile);
|
||||||
|
free(legacyDotfile);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
this->changed = true;
|
this->changed = true;
|
||||||
// TODO: how to get SYSCONFDIR correctly through Autoconf?
|
// TODO: how to get SYSCONFDIR correctly through Autoconf?
|
||||||
char* systemSettings = String_cat(SYSCONFDIR, "/htoprc");
|
char* systemSettings = String_cat(SYSCONFDIR, "/htoprc");
|
||||||
|
|
Loading…
Reference in New Issue