Skip to content

Commit 1d75298

Browse files
committed
Karel Zakr's revised patch to fix psql prompt for local host connections.
1 parent 73f5b08 commit 1d75298

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.27 2000/03/01 21:09:56 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.28 2000/03/11 13:56:23 petere Exp $
33
Postgres documentation
44
-->
55

@@ -1966,8 +1966,8 @@ testdb=> <userinput>\set content `sed -e "s/'/\\\\\\'/g" < my_file.txt`</userinp
19661966
<variablelist>
19671967
<varlistentry>
19681968
<term><literal>%M</literal></term>
1969-
<listitem><para>The hostname of the database server (or <quote>.</quote>
1970-
if Unix domain socket).</para></listitem>
1969+
<listitem><para>The full hostname (with domainname) of the database server (or
1970+
<quote>localhost</quote> if hostname information is not available).</para></listitem>
19711971
</varlistentry>
19721972

19731973
<varlistentry>

src/bin/psql/prompt.c

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.10 2000/03/08 01:38:59 momjian Exp $
7-
*/
6+
* $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.11 2000/03/11 13:56:24 petere Exp $
7+
*/
88
#include "postgres.h"
99
#include "prompt.h"
1010

@@ -19,7 +19,10 @@
1919
#include <win32.h>
2020
#endif
2121

22-
#include <sys/utsname.h>
22+
#if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(__QNX__)
23+
#include <unistd.h>
24+
#include <netdb.h>
25+
#endif
2326

2427
/*--------------------------
2528
* get_prompt
@@ -29,9 +32,10 @@
2932
* (might not be completely multibyte safe)
3033
*
3134
* Defined interpolations are:
32-
* %M - database server hostname (or "." if not TCP/IP)
33-
* %m - like %M but hostname truncated after first dot
34-
* %> - database server port number (or "." if not TCP/IP)
35+
* %M - database server "hostname.domainname" (or "localhost" if this
36+
* information is not available)
37+
* %m - like %M, but hostname only (before first dot)
38+
* %> - database server port number
3539
* %n - database user name
3640
* %/ - current database
3741
* %~ - like %/ but "~" when database name equals user name
@@ -56,6 +60,51 @@
5660
* will be empty (not NULL!).
5761
*--------------------------
5862
*/
63+
64+
/*
65+
* We need hostname information, only if connection is via UNIX socket
66+
*/
67+
#if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(__QNX__)
68+
69+
#define DOMAINNAME 1
70+
#define HOSTNAME 2
71+
72+
/*
73+
* Return full hostname for localhost.
74+
* - informations are init only in firts time - not queries DNS or NIS
75+
* for every localhost() call
76+
*/
77+
static char *
78+
localhost(int type, char *buf, int siz)
79+
{
80+
static struct hostent *hp = NULL;
81+
static int err = 0;
82+
83+
if (hp==NULL && err==0)
84+
{
85+
char hname[256];
86+
87+
if (gethostname(hname, 256) == 0)
88+
{
89+
if (!(hp = gethostbyname(hname)))
90+
err = 1;
91+
}
92+
else
93+
err = 1;
94+
}
95+
96+
if (hp==NULL)
97+
return strncpy(buf, "localhost", siz);
98+
99+
strncpy(buf, hp->h_name, siz); /* full aaa.bbb.ccc */
100+
101+
if (type==HOSTNAME)
102+
buf[strcspn(buf, ".")] = '\0';
103+
104+
return buf;
105+
}
106+
#endif
107+
59108
char *
60109
get_prompt(promptStatus_t status)
61110
{
@@ -115,23 +164,22 @@ get_prompt(promptStatus_t status)
115164
case 'm':
116165
if (pset.db)
117166
{
167+
/* INET socket */
118168
if (PQhost(pset.db))
119169
{
120170
strncpy(buf, PQhost(pset.db), MAX_PROMPT_SIZE);
121171
if (*p == 'm')
122172
buf[strcspn(buf, ".")] = '\0';
123173
}
124-
else if (*p == 'M')
125-
buf[0] = '.';
126-
else
127-
{
128-
struct utsname ubuf;
129-
130-
if (uname(&ubuf) < 0)
131-
buf[0] = '.';
132-
else
133-
strncpy(buf, ubuf.nodename, MAX_PROMPT_SIZE);
134-
}
174+
/* UNIX socket */
175+
#if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(__QNX__)
176+
else {
177+
if (*p == 'm')
178+
localhost(HOSTNAME, buf, MAX_PROMPT_SIZE);
179+
else
180+
localhost(DOMAINNAME, buf, MAX_PROMPT_SIZE);
181+
}
182+
#endif
135183
}
136184
break;
137185
/* DB server port number */

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