]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Implement readpassphrase function, which read a passphrase from console, on Windows.
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Mon, 13 Oct 2014 07:25:28 +0000 (16:25 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Mon, 13 Oct 2014 07:25:28 +0000 (16:25 +0900)
libarchive_fe/passphrase.c

index 0fe630e7e602dc22f6a5eb418757bd82b35f6549..f6f76278cb17ee177ecf6b364d21a6f1e6cacbd3 100644 (file)
@@ -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__ */