From af53e170b9927599110ff42842a8d52eabe00bef Mon Sep 17 00:00:00 2001 From: Earnestly Date: Wed, 11 Jan 2017 19:18:39 +0000 Subject: [PATCH] top: Add unobtrusive XDG support By default the file HOME/.toprc will be prefered. This ensures there should be minimal breakage even if this file is later created by some other means. Otherwise we will follow the new behaviour described by the XDG Base Directory Specification: If the XDG_CONFIG_HOME environment variable is available we will attempt to use this as XDG_CONFIG_HOME/procps/toprc otherwise we will fall-back to HOME/.config/procps/toprc instead. Signed-off-by: Earnestly --- top/top.1 | 15 +++++++++++---- top/top.c | 25 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/top/top.1 b/top/top.1 index 23e3a8f6..a14347d8 100644 --- a/top/top.1 +++ b/top/top.1 @@ -69,7 +69,7 @@ . .\" Document ///////////////////////////////////////////////////////////// .\" ---------------------------------------------------------------------- -.TH TOP 1 "November 2016" "procps-ng" "User Commands" +.TH TOP 1 "January 2017" "procps-ng" "User Commands" .\" ---------------------------------------------------------------------- .\" ---------------------------------------------------------------------- @@ -2195,7 +2195,14 @@ Here is an example of the contents of\fI /etc/toprc\fR: .\" ...................................................................... .SS 6b. PERSONAL Configuration File .\" ---------------------------------------------------------------------- -This file is written as `$HOME/.your\-name\-4\-\*(We' + `rc'. +The legacy version is written as `$HOME/.your\-name\-4\-\*(We' + `rc' +with a leading period. + +A newly created \*(CF is written as procps/your\-name\-4\-\*(We' + `rc' +without a leading period. +The procps directory will be subordinate to either $XDG_CONFIG_HOME when +set as an absolute path or the $HOME/.config directory. + Use the `W' \*(CI to create it or update it. Here is the general layout: @@ -2211,8 +2218,8 @@ Here is the general layout: " # discussed below .fi -If the $HOME variable is not present, \*(We will try to write the -personal \*(CF to the current directory, subject to permissions. +If the $HOME and $XDG_CONFIG_HOME variables are not present, \*(We will try +to write the personal \*(CF in the current directory, subject to permissions. .\" ...................................................................... .SS 6c. ADDING INSPECT Entries diff --git a/top/top.c b/top/top.c index 4427f89f..3de6510f 100644 --- a/top/top.c +++ b/top/top.c @@ -43,6 +43,7 @@ #include #include #include // also available via +#include #include #include // also available via @@ -3458,13 +3459,10 @@ static int config_cvt (WIN_t *q) { static void configs_read (void) { float tmp_delay = DEF_DELAY; char fbuf[LRGBUFSIZ]; - const char *p; + const char *p, *p_home; FILE *fp; int i; - p = getenv("HOME"); - snprintf(Rc_name, sizeof(Rc_name), "%s/.%src", (p && *p) ? p : ".", Myname); - fp = fopen(SYS_RCFILESPEC, "r"); if (fp) { if (fgets(fbuf, sizeof(fbuf), fp)) { // sys rc file, line 1 @@ -3475,6 +3473,25 @@ static void configs_read (void) { fclose(fp); } + // attempt to use the legacy file first, if we cannot access that file, use + // the new XDG basedir locations (XDG_CONFIG_HOME or HOME/.config) instead. + p_home = getenv("HOME"); + if (!p_home || p_home[0] == '\0') + p_home = "."; + snprintf(Rc_name, sizeof(Rc_name), "%s/.%src", p_home, Myname); + + if (access(Rc_name, F_OK)) { + p = getenv("XDG_CONFIG_HOME"); + // ensure the path we get is absolute, fallback otherwise. + if (!p || p[0] != '/') { + p = fmtmk("%s/.config", p_home); + (void)mkdir(p, 0700); + } + snprintf(Rc_name, sizeof(Rc_name), "%s/procps", p); + (void)mkdir(Rc_name, 0700); + snprintf(Rc_name, sizeof(Rc_name), "%s/procps/%src", p, Myname); + } + fp = fopen(Rc_name, "r"); if (fp) { int tmp_whole, tmp_fract;