|
3 | 3 | *
|
4 | 4 | * Copyright 2000 by PostgreSQL Global Development Group
|
5 | 5 | *
|
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 | + */ |
8 | 8 | #include "postgres.h"
|
9 | 9 | #include "prompt.h"
|
10 | 10 |
|
|
19 | 19 | #include <win32.h>
|
20 | 20 | #endif
|
21 | 21 |
|
22 |
| -#include <sys/utsname.h> |
| 22 | +#if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(__QNX__) |
| 23 | +#include <unistd.h> |
| 24 | +#include <netdb.h> |
| 25 | +#endif |
23 | 26 |
|
24 | 27 | /*--------------------------
|
25 | 28 | * get_prompt
|
|
29 | 32 | * (might not be completely multibyte safe)
|
30 | 33 | *
|
31 | 34 | * 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 |
35 | 39 | * %n - database user name
|
36 | 40 | * %/ - current database
|
37 | 41 | * %~ - like %/ but "~" when database name equals user name
|
|
56 | 60 | * will be empty (not NULL!).
|
57 | 61 | *--------------------------
|
58 | 62 | */
|
| 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 | + |
59 | 108 | char *
|
60 | 109 | get_prompt(promptStatus_t status)
|
61 | 110 | {
|
@@ -115,23 +164,22 @@ get_prompt(promptStatus_t status)
|
115 | 164 | case 'm':
|
116 | 165 | if (pset.db)
|
117 | 166 | {
|
| 167 | + /* INET socket */ |
118 | 168 | if (PQhost(pset.db))
|
119 | 169 | {
|
120 | 170 | strncpy(buf, PQhost(pset.db), MAX_PROMPT_SIZE);
|
121 | 171 | if (*p == 'm')
|
122 | 172 | buf[strcspn(buf, ".")] = '\0';
|
123 | 173 | }
|
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 |
135 | 183 | }
|
136 | 184 | break;
|
137 | 185 | /* DB server port number */
|
|
0 commit comments