Skip to content

Commit 64f86fb

Browse files
committed
Reimplement 9f80f48 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.
1 parent 2cda889 commit 64f86fb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/bin/psql/command.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,33 @@ exec_command(const char *cmd,
302302
else if (strcmp(cmd, "conninfo") == 0)
303303
{
304304
char *db = PQdb(pset.db);
305-
char *host = PQhost(pset.db);
306305

307306
if (db == NULL)
308307
printf(_("You are currently not connected to a database.\n"));
309308
else
310309
{
310+
char *host;
311+
PQconninfoOption *connOptions;
312+
PQconninfoOption *option;
313+
314+
host = PQhost(pset.db);
311315
if (host == NULL)
312316
host = DEFAULT_PGSOCKET_DIR;
317+
/* A usable "hostaddr" overrides the basic sense of host. */
318+
connOptions = PQconninfo(pset.db);
319+
if (connOptions == NULL)
320+
{
321+
psql_error("out of memory\n");
322+
exit(EXIT_FAILURE);
323+
}
324+
for (option = connOptions; option && option->keyword; option++)
325+
if (strcmp(option->keyword, "hostaddr") == 0)
326+
{
327+
if (option->val != NULL && option->val[0] != '\0')
328+
host = option->val;
329+
break;
330+
}
331+
313332
/* If the host is an absolute path, the connection is via socket */
314333
if (is_absolute_path(host))
315334
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,
318337
printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
319338
db, PQuser(pset.db), host, PQport(pset.db));
320339
printSSLInfo();
340+
341+
PQconninfoFree(connOptions);
321342
}
322343
}
323344

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy