From b327f90c786f5fb574b362995922cfee3b4375bb Mon Sep 17 00:00:00 2001 From: Michihiro NAKAJIMA Date: Mon, 13 Oct 2014 16:25:28 +0900 Subject: [PATCH] Implement readpassphrase function, which read a passphrase from console, on Windows. --- libarchive_fe/passphrase.c | 41 +++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/libarchive_fe/passphrase.c b/libarchive_fe/passphrase.c index 0fe630e7e..f6f76278c 100644 --- a/libarchive_fe/passphrase.c +++ b/libarchive_fe/passphrase.c @@ -76,16 +76,47 @@ __FBSDID("$FreeBSD$"); #if defined(_WIN32) && !defined(__CYGWIN__) +#include static char * readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) { - (void)prompt; - (void)buf; - (void)bufsiz; + HANDLE hStdin, hStdout; + DWORD mode, rbytes; + BOOL success; + (void)flags; - errno = EINVAL; - return (NULL); + + hStdin = GetStdHandle(STD_INPUT_HANDLE); + if (hStdin == INVALID_HANDLE_VALUE) + return (NULL); + hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (hStdout == INVALID_HANDLE_VALUE) + return (NULL); + + success = GetConsoleMode(hStdin, &mode); + if (!success) + return (NULL); + mode &= ~ENABLE_ECHO_INPUT; + mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; + success = SetConsoleMode(hStdin, mode); + if (!success) + return (NULL); + + success = WriteFile(hStdout, prompt, (DWORD)strlen(prompt), + NULL, NULL); + if (!success) + return (NULL); + success = ReadFile(hStdin, buf, (DWORD)bufsiz - 1, &rbytes, NULL); + if (!success) + return (NULL); + WriteFile(hStdout, "\r\n", 2, NULL, NULL); + buf[rbytes] = '\0'; + /* Remove trailing carriage return(s). */ + if (rbytes > 2 && buf[rbytes - 2] == '\r' && buf[rbytes - 1] == '\n') + buf[rbytes - 2] = '\0'; + + return (buf); } #else /* _WIN32 && !__CYGWIN__ */ -- 2.47.2