summaryrefslogtreecommitdiff
path: root/libarchive_fe
diff options
context:
space:
mode:
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>2014-10-13 16:25:28 +0900
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>2014-10-13 16:25:28 +0900
commitb327f90c786f5fb574b362995922cfee3b4375bb (patch)
tree6c2cfaa2b742588d7b47fe276d1b5af6a1c89569 /libarchive_fe
parent178bf9b890c71f8a64e1960e5449faa84a84c4eb (diff)
downloadlibarchive-b327f90c786f5fb574b362995922cfee3b4375bb.tar.gz
Implement readpassphrase function, which read a passphrase from console, on Windows.
Diffstat (limited to 'libarchive_fe')
-rw-r--r--libarchive_fe/passphrase.c41
1 files changed, 36 insertions, 5 deletions
diff --git a/libarchive_fe/passphrase.c b/libarchive_fe/passphrase.c
index 0fe630e7..f6f76278 100644
--- a/libarchive_fe/passphrase.c
+++ b/libarchive_fe/passphrase.c
@@ -76,16 +76,47 @@ __FBSDID("$FreeBSD$");
#if defined(_WIN32) && !defined(__CYGWIN__)
+#include <Windows.h>
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__ */