Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66b77db

Browse files
committed
Prompt for password from /dev/tty and fall back to stdin/stderr.
1 parentcdce507 commit66b77db

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 orstdin/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+
staticboolprompt_state;
61+
6062
char*
6163
simple_prompt(constchar*prompt,intmaxlen,boolecho)
6264
{
6365
intlength;
6466
char*destination;
67+
staticFILE*termin=NULL,*termout;
6568

6669
#ifdefHAVE_TERMIOS_H
6770
structtermiost_orig,
6871
t;
69-
7072
#endif
7173

7274
destination= (char*)malloc(maxlen+2);
7375
if (!destination)
7476
returnNULL;
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
#ifdefHAVE_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
#ifdefHAVE_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
staticint
122144
_parse_version(ArchiveHandle*AH,constchar*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 orstdin/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
intlength;
178178
char*destination;
179+
staticFILE*termin=NULL,*termout;
179180

180181
#ifdefHAVE_TERMIOS_H
181182
structtermiost_orig,
182183
t;
183-
184184
#endif
185185

186186
destination= (char*)malloc(maxlen+2);
187187
if (!destination)
188188
returnNULL;
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
#ifdefHAVE_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
#ifdefHAVE_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)

[8]ページ先頭

©2009-2025 Movatter.jp