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

Commit50485b3

Browse files
committed
Fix simple_prompt() to disable echo on Windows when stdin != terminal.
If echo = false, simple_prompt() is supposed to prevent echoing theinput (for password input). However, the Windows implementation appliedthe mode change to STD_INPUT_HANDLE. That would not have the desiredeffect if stdin isn't actually the terminal, for instance if the useris piping something into psql. Fix it to apply the mode change tothe correct input file, so that passwords do not echo in such cases.In passing, shorten and de-uglify this code by using #elif rather thanan #if nest and removing some duplicated code.Back-patch to all supported versions. To simplify that, also back-patchthe portions of commit9daec77 that got rid of an unnecessarymalloc/free in the same area.Matthew Stickney (cosmetic changes by me)Discussion:https://postgr.es/m/502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com
1 parentb929614 commit50485b3

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

‎src/port/sprompt.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
4040
FILE*termin,
4141
*termout;
4242

43-
#ifdefHAVE_TERMIOS_H
43+
#if defined(HAVE_TERMIOS_H)
4444
structtermiost_orig,
4545
t;
46-
#else
47-
#ifdefWIN32
46+
#elif defined(WIN32)
4847
HANDLEt=NULL;
4948
DWORDt_orig=0;
5049
#endif
51-
#endif
5250

5351
#ifdefWIN32
5452

@@ -66,8 +64,11 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
6664
*
6765
* XXX fgets() still receives text in the console's input code page. This
6866
* makes non-ASCII credentials unportable.
67+
*
68+
* Unintuitively, we also open termin in mode "w+", even though we only
69+
* read it; that's needed for SetConsoleMode() to succeed.
6970
*/
70-
termin=fopen("CONIN$","r");
71+
termin=fopen("CONIN$","w+");
7172
termout=fopen("CONOUT$","w+");
7273
#else
7374

@@ -99,29 +100,25 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
99100
termout=stderr;
100101
}
101102

102-
#ifdefHAVE_TERMIOS_H
103103
if (!echo)
104104
{
105+
#if defined(HAVE_TERMIOS_H)
106+
/* disable echo via tcgetattr/tcsetattr */
105107
tcgetattr(fileno(termin),&t);
106108
t_orig=t;
107109
t.c_lflag &= ~ECHO;
108110
tcsetattr(fileno(termin),TCSAFLUSH,&t);
109-
}
110-
#else
111-
#ifdefWIN32
112-
if (!echo)
113-
{
114-
/* get a new handle to turn echo off */
115-
t=GetStdHandle(STD_INPUT_HANDLE);
111+
#elif defined(WIN32)
112+
/* need the file's HANDLE to turn echo off */
113+
t= (HANDLE)_get_osfhandle(_fileno(termin));
116114

117115
/* save the old configuration first */
118116
GetConsoleMode(t,&t_orig);
119117

120118
/* set to the new mode */
121119
SetConsoleMode(t,ENABLE_LINE_INPUT |ENABLE_PROCESSED_INPUT);
122-
}
123-
#endif
124120
#endif
121+
}
125122

126123
if (prompt)
127124
{
@@ -151,24 +148,19 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
151148
/* remove trailing newline */
152149
destination[length-1]='\0';
153150

154-
#ifdefHAVE_TERMIOS_H
155151
if (!echo)
156152
{
153+
/* restore previous echo behavior, then echo \n */
154+
#if defined(HAVE_TERMIOS_H)
157155
tcsetattr(fileno(termin),TCSAFLUSH,&t_orig);
158156
fputs("\n",termout);
159157
fflush(termout);
160-
}
161-
#else
162-
#ifdefWIN32
163-
if (!echo)
164-
{
165-
/* reset to the original console mode */
158+
#elif defined(WIN32)
166159
SetConsoleMode(t,t_orig);
167160
fputs("\n",termout);
168161
fflush(termout);
169-
}
170-
#endif
171162
#endif
163+
}
172164

173165
if (termin!=stdin)
174166
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp