aboutsummaryrefslogtreecommitdiff
path: root/src/port/path.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-01-09 19:19:02 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2022-01-09 19:19:02 -0500
commit376ce3e404b75d267089c4bc6dc7c18d0b38728b (patch)
tree197a5ab6a9a66e7ada4fd1d027e1e5e5147c717a /src/port/path.c
parent6867f963e319934cbdafeb4bd5beaea5797c7be2 (diff)
downloadpostgresql-376ce3e404b75d267089c4bc6dc7c18d0b38728b.tar.gz
postgresql-376ce3e404b75d267089c4bc6dc7c18d0b38728b.zip
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 <pwd.h> 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
Diffstat (limited to 'src/port/path.c')
-rw-r--r--src/port/path.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/port/path.c b/src/port/path.c
index ee4227ec983..5ac26f4bcf7 100644
--- a/src/port/path.c
+++ b/src/port/path.c
@@ -807,14 +807,25 @@ bool
get_home_path(char *ret_path)
{
#ifndef WIN32
- char pwdbuf[BUFSIZ];
- struct passwd pwdstr;
- struct passwd *pwd = NULL;
+ /*
+ * We first consult $HOME. If that's unset, try to get the info from
+ * <pwd.h>.
+ */
+ const char *home;
- (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
- if (pwd == NULL)
- return false;
- strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
+ 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(ret_path, home, MAXPGPATH);
return true;
#else
char *tmppath;