1 /**
2 * \file os_getpass.c
3 * \brief Prompt and retrieve a password from the user.
4 * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
5 */
6
7 #include "premake.h"
8
9
os_getpass(lua_State * L)10 int os_getpass(lua_State* L)
11 {
12 const char* prompt = luaL_checkstring(L, 1);
13
14 #if PLATFORM_WINDOWS
15 HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
16 HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
17 DWORD read_chars, mode, written_chars;
18 char buffer[1024];
19 const char* newline = "\n";
20
21 WriteConsoleA(hstdout, prompt, (DWORD)strlen(prompt), &written_chars, NULL);
22
23 GetConsoleMode(hstdin, &mode);
24 SetConsoleMode(hstdin, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
25 ReadConsoleA(hstdin, buffer, sizeof (buffer), &read_chars, NULL);
26 SetConsoleMode(hstdin, mode);
27
28 WriteConsoleA(hstdout, newline, (DWORD)strlen(newline), &written_chars, NULL);
29
30 buffer[strcspn(buffer, "\r\n")] = '\0';
31
32 lua_pushstring(L, buffer);
33 return 1;
34 #else
35 lua_pushstring(L, getpass(prompt));
36 return 1;
37 #endif
38 }
39