Added os.getpass
Prompt the user to enter a password and don't encho their entry.
This commit is contained in:
parent
a51a4cfd6b
commit
d5e53a0be0
38
src/host/os_getpass.c
Normal file
38
src/host/os_getpass.c
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* \file os_getpass.c
|
||||
* \brief Prompt and retrieve a password from the user.
|
||||
* \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
|
||||
*/
|
||||
|
||||
#include "premake.h"
|
||||
|
||||
|
||||
int os_getpass(lua_State* L)
|
||||
{
|
||||
const char* prompt = luaL_checkstring(L, 1);
|
||||
|
||||
#if PLATFORM_WINDOWS
|
||||
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD read_chars, mode, written_chars;
|
||||
char buffer[1024];
|
||||
const char* newline = "\n";
|
||||
|
||||
WriteConsoleA(hstdout, prompt, strlen(prompt), &written_chars, NULL);
|
||||
|
||||
GetConsoleMode(hstdin, &mode);
|
||||
SetConsoleMode(hstdin, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
|
||||
ReadConsoleA(hstdin, buffer, sizeof (buffer), &read_chars, NULL);
|
||||
SetConsoleMode(hstdin, mode);
|
||||
|
||||
WriteConsoleA(hstdout, newline, strlen(newline), &written_chars, NULL);
|
||||
|
||||
StrTrimA(buffer, "\r\n");
|
||||
|
||||
lua_pushstring(L, buffer);
|
||||
return 1;
|
||||
#else
|
||||
lua_pushstring(L, getpass(prompt));
|
||||
return 1;
|
||||
#endif
|
||||
}
|
@ -57,6 +57,7 @@ static const luaL_Reg os_functions[] = {
|
||||
{ "_is64bit", os_is64bit },
|
||||
{ "isdir", os_isdir },
|
||||
{ "getcwd", os_getcwd },
|
||||
{ "getpass", os_getpass },
|
||||
{ "getversion", os_getversion },
|
||||
{ "isfile", os_isfile },
|
||||
{ "islink", os_islink },
|
||||
|
@ -103,6 +103,7 @@ int os_chdir(lua_State* L);
|
||||
int os_chmod(lua_State* L);
|
||||
int os_copyfile(lua_State* L);
|
||||
int os_getcwd(lua_State* L);
|
||||
int os_getpass(lua_State* L);
|
||||
int os_getversion(lua_State* L);
|
||||
int os_is64bit(lua_State* L);
|
||||
int os_isdir(lua_State* L);
|
||||
|
Loading…
Reference in New Issue
Block a user