client: Factor set_echo_if_tty() out of get_password()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2020-12-27 11:32:48 +01:00
parent e551b6198d
commit ea762c1f08

View file

@ -42,6 +42,31 @@
#endif #endif
#include "misc.h" #include "misc.h"
#ifndef HAVE_GETPASS
static int
set_echo_if_tty(int on)
{
#ifdef _WIN32
DWORD mode;
HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
if (!GetConsoleMode(input_handle, &mode))
return 0;
if (on)
mode |= ENABLE_ECHO_INPUT;
else
mode &= ~ENABLE_ECHO_INPUT;
if (!SetConsoleMode(input_handle, mode))
return -1;
return 1;
#else
return 0;
#endif
}
#endif /* !HAVE_GETPASS */
char * char *
get_password(const char *prompt) get_password(const char *prompt)
{ {
@ -51,22 +76,19 @@ get_password(const char *prompt)
static char buf[128]; static char buf[128];
char *p; char *p;
size_t len; size_t len;
#ifdef _WIN32 int echo_set;
DWORD mode;
HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
if (GetConsoleMode(input_handle, &mode)) echo_set = set_echo_if_tty(0);
SetConsoleMode(input_handle, mode & ~ENABLE_ECHO_INPUT); if (echo_set <= 0)
else
#endif
printf("Note: your input is echoed to the screen\n"); printf("Note: your input is echoed to the screen\n");
printf("%s", prompt); printf("%s", prompt);
fflush(stdout); fflush(stdout);
p = fgets(buf, sizeof(buf), stdin); p = fgets(buf, sizeof(buf), stdin);
#ifdef _WIN32
if (GetConsoleMode(input_handle, &mode)) if (echo_set > 0)
SetConsoleMode(input_handle, mode | ENABLE_ECHO_INPUT); set_echo_if_tty(1);
#endif
if (!p) if (!p)
return NULL; return NULL;
len = strlen(p); len = strlen(p);