Skip to content

Commit 66b77db

Browse files
committed
Prompt for password from /dev/tty and fall back to stdin/stderr.
1 parent cdce507 commit 66b77db

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

src/bin/pg_dump/pg_backup_db.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.26 2001/09/21 21:58:30 petere Exp $
8+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.27 2001/10/15 16:40:27 momjian Exp $
99
*
1010
* NOTES
1111
*
@@ -49,53 +49,73 @@ static void notice_processor(void *arg, const char *message);
4949
* simple_prompt
5050
*
5151
* Generalized function especially intended for reading in usernames and
52-
* password interactively. Reads from stdin.
52+
* password interactively. Reads from /dev/tty or stdin/stderr.
5353
*
5454
* prompt: The prompt to print
5555
* maxlen: How many characters to accept
5656
* echo: Set to false if you want to hide what is entered (for passwords)
5757
*
5858
* Returns a malloc()'ed string with the input (w/o trailing newline).
5959
*/
60+
static bool prompt_state;
61+
6062
char *
6163
simple_prompt(const char *prompt, int maxlen, bool echo)
6264
{
6365
int length;
6466
char *destination;
67+
static FILE *termin = NULL, *termout;
6568

6669
#ifdef HAVE_TERMIOS_H
6770
struct termios t_orig,
6871
t;
69-
7072
#endif
7173

7274
destination = (char *) malloc(maxlen + 2);
7375
if (!destination)
7476
return NULL;
77+
78+
prompt_state = true;
79+
80+
/* initialize the streams */
81+
if (!termin)
82+
{
83+
if ((termin = termout = fopen("/dev/tty", "w+")) == NULL)
84+
{
85+
termin = stdin;
86+
termout = stderr;
87+
}
88+
}
89+
7590
if (prompt)
76-
fputs(gettext(prompt), stderr);
91+
{
92+
fputs(gettext(prompt), termout);
93+
rewind(termout); /* does flush too */
94+
}
7795

7896
#ifdef HAVE_TERMIOS_H
7997
if (!echo)
8098
{
81-
tcgetattr(0, &t);
99+
tcgetattr(fileno(termin), &t);
82100
t_orig = t;
83101
t.c_lflag &= ~ECHO;
84-
tcsetattr(0, TCSADRAIN, &t);
102+
tcsetattr(fileno(termin), TCSADRAIN, &t);
85103
}
86104
#endif
87105

88-
if (fgets(destination, maxlen, stdin) == NULL)
106+
if (fgets(destination, maxlen, termin) == NULL)
89107
destination[0] = '\0';
90108

91109
#ifdef HAVE_TERMIOS_H
92110
if (!echo)
93111
{
94-
tcsetattr(0, TCSADRAIN, &t_orig);
95-
fputs("\n", stderr);
112+
tcsetattr(fileno(termin), TCSADRAIN, &t_orig);
113+
fputs("\n", termout);
96114
}
97115
#endif
98116

117+
prompt_state = false;
118+
99119
length = strlen(destination);
100120
if (length > 0 && destination[length - 1] != '\n')
101121
{
@@ -105,11 +125,12 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
105125

106126
do
107127
{
108-
if (fgets(buf, sizeof(buf), stdin) == NULL)
128+
if (fgets(buf, sizeof(buf), termin) == NULL)
109129
break;
110130
buflen = strlen(buf);
111131
} while (buflen > 0 && buf[buflen - 1] != '\n');
112132
}
133+
113134
if (length > 0 && destination[length - 1] == '\n')
114135
/* remove trailing newline */
115136
destination[length - 1] = '\0';
@@ -118,6 +139,7 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
118139
}
119140

120141

142+
121143
static int
122144
_parse_version(ArchiveHandle *AH, const char* versionString)
123145
{

src/bin/psql/common.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.34 2001/06/08 23:53:48 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.35 2001/10/15 16:40:27 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -161,7 +161,7 @@ NoticeProcessor(void *arg, const char *message)
161161
* simple_prompt
162162
*
163163
* Generalized function especially intended for reading in usernames and
164-
* password interactively. Reads from stdin.
164+
* password interactively. Reads from /dev/tty or stdin/stderr.
165165
*
166166
* prompt: The prompt to print
167167
* maxlen: How many characters to accept
@@ -176,39 +176,53 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
176176
{
177177
int length;
178178
char *destination;
179+
static FILE *termin = NULL, *termout;
179180

180181
#ifdef HAVE_TERMIOS_H
181182
struct termios t_orig,
182183
t;
183-
184184
#endif
185185

186186
destination = (char *) malloc(maxlen + 2);
187187
if (!destination)
188188
return NULL;
189-
if (prompt)
190-
fputs(gettext(prompt), stderr);
191189

192190
prompt_state = true;
193191

192+
/* initialize the streams */
193+
if (!termin)
194+
{
195+
if ((termin = termout = fopen("/dev/tty", "w+")) == NULL)
196+
{
197+
termin = stdin;
198+
termout = stderr;
199+
}
200+
}
201+
202+
if (prompt)
203+
{
204+
fputs(gettext(prompt), termout);
205+
rewind(termout); /* does flush too */
206+
}
207+
194208
#ifdef HAVE_TERMIOS_H
195209
if (!echo)
196210
{
197-
tcgetattr(0, &t);
211+
tcgetattr(fileno(termin), &t);
198212
t_orig = t;
199213
t.c_lflag &= ~ECHO;
200-
tcsetattr(0, TCSADRAIN, &t);
214+
tcsetattr(fileno(termin), TCSADRAIN, &t);
201215
}
202216
#endif
203217

204-
if (fgets(destination, maxlen, stdin) == NULL)
218+
if (fgets(destination, maxlen, termin) == NULL)
205219
destination[0] = '\0';
206220

207221
#ifdef HAVE_TERMIOS_H
208222
if (!echo)
209223
{
210-
tcsetattr(0, TCSADRAIN, &t_orig);
211-
fputs("\n", stderr);
224+
tcsetattr(fileno(termin), TCSADRAIN, &t_orig);
225+
fputs("\n", termout);
212226
}
213227
#endif
214228

@@ -223,7 +237,7 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
223237

224238
do
225239
{
226-
if (fgets(buf, sizeof(buf), stdin) == NULL)
240+
if (fgets(buf, sizeof(buf), termin) == NULL)
227241
break;
228242
buflen = strlen(buf);
229243
} while (buflen > 0 && buf[buflen - 1] != '\n');

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