diff -up ./top/top.c.ori ./top/top.c --- ./top/top.c.ori 2019-02-27 17:23:48.060408215 +0100 +++ ./top/top.c 2019-02-28 15:28:11.321927877 +0100 @@ -3437,6 +3437,20 @@ static int config_cvt (WIN_t *q) { } // end: config_cvt +static int snprintf_Rc_name (const char *const format, ...) __attribute__((format(printf,1,2))); +static int snprintf_Rc_name (const char *const format, ...) { + int len; + va_list ap; + va_start(ap, format); + len = vsnprintf(Rc_name, sizeof(Rc_name), format, ap); + va_end(ap); + if (len <= 0 || (size_t)len >= sizeof(Rc_name)) { + Rc_name[0] = '\0'; + return 0; + } + return len; +} + /* * Build the local RC file name then try to read both of 'em. * 'SYS_RCFILESPEC' contains two lines consisting of the secure @@ -3458,8 +3472,18 @@ static void configs_read (void) { FILE *fp; int i; + Rc_name[0] = '\0'; // "fopen() shall fail if pathname is an empty string." p = getenv("HOME"); - snprintf(Rc_name, sizeof(Rc_name), "%s/.%src", (p && *p) ? p : ".", Myname); + + if (!p || p[0] != '/') { + const struct passwd *const pwd = getpwuid(getuid()); + if (!pwd || !(p = pwd->pw_dir) || p[0] != '/') { + p = NULL; + } + } + if (p) { + snprintf_Rc_name("%s/.%src", p, Myname); + } fp = fopen(SYS_RCFILESPEC, "r"); if (fp) {