top: adapt the master branch XDG specification support

This patch adapts a master branch commit to our newlib
branch. Shown below was that original commit msg text.

------------------------------------------------------
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 <zibeon@gmail.com>
------------------------------------------------------

Reference(s):
. master branch original
commit 0a0f7d60e309c13c8a399bc2187bed6e3e156b43
. discussion thread
https://gitlab.com/procps-ng/procps/merge_requests/38

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2017-01-26 00:00:00 -06:00
committed by Craig Small
parent 9252a04eae
commit a81a74a1df
2 changed files with 32 additions and 8 deletions

View File

@ -40,6 +40,7 @@
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/select.h> // also available via <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> // also available via <stdlib.h>
@ -2964,13 +2965,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
@ -2981,6 +2979,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;