aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Misch <noah@leadboat.com>2014-11-29 12:31:43 -0500
committerNoah Misch <noah@leadboat.com>2014-11-29 12:31:43 -0500
commit64f86fb11e20b55fb742af72d55806f8bdd9cd2d (patch)
tree38b070d2fd501040de4e26bff991ce7e475279c7
parent2cda889984a6f5ad405318f9e91202d258383b66 (diff)
downloadpostgresql-64f86fb11e20b55fb742af72d55806f8bdd9cd2d.tar.gz
postgresql-64f86fb11e20b55fb742af72d55806f8bdd9cd2d.zip
Reimplement 9f80f4835a55a1cbffcda5d23a617917f3286c14 with PQconninfo().
Apart from ignoring "hostaddr" set to the empty string, this behaves identically to its predecessor. Back-patch to 9.4, where the original commit first appeared. Reviewed by Fujii Masao.
-rw-r--r--src/bin/psql/command.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index eb281a3dcbc..c7a17d7469f 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -302,14 +302,33 @@ exec_command(const char *cmd,
else if (strcmp(cmd, "conninfo") == 0)
{
char *db = PQdb(pset.db);
- char *host = PQhost(pset.db);
if (db == NULL)
printf(_("You are currently not connected to a database.\n"));
else
{
+ char *host;
+ PQconninfoOption *connOptions;
+ PQconninfoOption *option;
+
+ host = PQhost(pset.db);
if (host == NULL)
host = DEFAULT_PGSOCKET_DIR;
+ /* A usable "hostaddr" overrides the basic sense of host. */
+ connOptions = PQconninfo(pset.db);
+ if (connOptions == NULL)
+ {
+ psql_error("out of memory\n");
+ exit(EXIT_FAILURE);
+ }
+ for (option = connOptions; option && option->keyword; option++)
+ if (strcmp(option->keyword, "hostaddr") == 0)
+ {
+ if (option->val != NULL && option->val[0] != '\0')
+ host = option->val;
+ break;
+ }
+
/* If the host is an absolute path, the connection is via socket */
if (is_absolute_path(host))
printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
@@ -318,6 +337,8 @@ exec_command(const char *cmd,
printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
db, PQuser(pset.db), host, PQport(pset.db));
printSSLInfo();
+
+ PQconninfoFree(connOptions);
}
}