summaryrefslogtreecommitdiff
path: root/libmysql/get_password.c
diff options
context:
space:
mode:
Diffstat (limited to 'libmysql/get_password.c')
-rw-r--r--libmysql/get_password.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/libmysql/get_password.c b/libmysql/get_password.c
index 16f6b25822d..e704aec8337 100644
--- a/libmysql/get_password.c
+++ b/libmysql/get_password.c
@@ -62,13 +62,35 @@
/* were just going to fake it here and get input from the keyboard */
void get_tty_password_buff(const char *opt_message, char *to, size_t length)
{
+ HANDLE consoleinput;
+ DWORD oldstate;
char *pos=to,*end=to+length-1;
int i=0;
+
+ consoleinput= CreateFile("CONIN$", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ ,
+ NULL, OPEN_EXISTING, 0, NULL);
+ if (consoleinput == NULL || consoleinput == INVALID_HANDLE_VALUE)
+ {
+ /* This is a GUI application or service without console input, bail out. */
+ *to= 0;
+ return;
+ }
_cputs(opt_message ? opt_message : "Enter password: ");
+
+ /*
+ Switch to raw mode (no line input, no echo input).
+ Allow Ctrl-C handler with ENABLE_PROCESSED_INPUT.
+ */
+ GetConsoleMode(consoleinput, &oldstate);
+ SetConsoleMode(consoleinput, ENABLE_PROCESSED_INPUT);
for (;;)
{
- int tmp;
- tmp=_getch();
+ char tmp;
+ DWORD chars_read;
+ if (!ReadConsole(consoleinput, &tmp, 1, &chars_read, NULL))
+ break;
+ if (chars_read == 0)
+ break;
if (tmp == '\b' || tmp == 127)
{
if (pos != to)
@@ -78,13 +100,16 @@ void get_tty_password_buff(const char *opt_message, char *to, size_t length)
continue;
}
}
- if (tmp == -1 || tmp == '\n' || tmp == '\r' || tmp == 3)
+ if (tmp == '\n' || tmp == '\r')
break;
if (iscntrl(tmp) || pos == end)
continue;
_cputs("*");
- *(pos++) = (char)tmp;
+ *(pos++) = tmp;
}
+ /* Reset console mode after password input. */
+ SetConsoleMode(consoleinput, oldstate);
+ CloseHandle(consoleinput);
*pos=0;
_cputs("\n");
}