From 376ce3e404b75d267089c4bc6dc7c18d0b38728b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 9 Jan 2022 19:19:02 -0500 Subject: Prefer $HOME when looking up the current user's home directory. When we need to identify the home directory on non-Windows, first consult getenv("HOME"). If that's empty or unset, fall back on our previous method of checking the database. Preferring $HOME allows the user to intentionally point at some other directory, and it seems to be in line with the behavior of most other utilities. However, we shouldn't rely on it completely, as $HOME is likely to be unset when running as a daemon. Anders Kaseorg Discussion: https://postgr.es/m/1634252654444.90107@mit.edu --- src/interfaces/libpq/fe-connect.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/interfaces/libpq/fe-connect.c') diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 72914116ee..a12e0180fd 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7267,14 +7267,21 @@ bool pqGetHomeDirectory(char *buf, int bufsize) { #ifndef WIN32 - char pwdbuf[BUFSIZ]; - struct passwd pwdstr; - struct passwd *pwd = NULL; + const char *home; - (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd); - if (pwd == NULL) - return false; - strlcpy(buf, pwd->pw_dir, bufsize); + home = getenv("HOME"); + if (home == NULL || home[0] == '\0') + { + char pwdbuf[BUFSIZ]; + struct passwd pwdstr; + struct passwd *pwd = NULL; + + (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd); + if (pwd == NULL) + return false; + home = pwd->pw_dir; + } + strlcpy(buf, home, bufsize); return true; #else char tmppath[MAX_PATH]; -- cgit v1.2.1