aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/command.c')
-rw-r--r--src/bin/psql/command.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 053332e7af5..f5904748553 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -558,19 +558,25 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
else
{
#ifndef WIN32
- struct passwd *pw;
- uid_t user_id = geteuid();
-
- errno = 0; /* clear errno before call */
- pw = getpwuid(user_id);
- if (!pw)
+ /* This should match get_home_path() */
+ dir = getenv("HOME");
+ if (dir == NULL || dir[0] == '\0')
{
- pg_log_error("could not get home directory for user ID %ld: %s",
- (long) user_id,
- errno ? strerror(errno) : _("user does not exist"));
- exit(EXIT_FAILURE);
+ uid_t user_id = geteuid();
+ struct passwd *pw;
+
+ errno = 0; /* clear errno before call */
+ pw = getpwuid(user_id);
+ if (pw)
+ dir = pw->pw_dir;
+ else
+ {
+ pg_log_error("could not get home directory for user ID %ld: %s",
+ (long) user_id,
+ errno ? strerror(errno) : _("user does not exist"));
+ success = false;
+ }
}
- dir = pw->pw_dir;
#else /* WIN32 */
/*
@@ -581,7 +587,8 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
#endif /* WIN32 */
}
- if (chdir(dir) == -1)
+ if (success &&
+ chdir(dir) < 0)
{
pg_log_error("\\%s: could not change directory to \"%s\": %m",
cmd, dir);