* Copyright 2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel DΓΆrfler, axeld@pinc-software.de
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
char *
getpass(const char *prompt)
{
static char password[128];
struct termios termios;
bool changed = false;
if (tcgetattr(fileno(stdin), &termios) == 0) {
struct termios noEchoTermios = termios;
noEchoTermios.c_lflag &= ~(ECHO | ISIG);
changed = tcsetattr(fileno(stdin), TCSAFLUSH, &noEchoTermios) == 0;
}
fputs(prompt, stdout);
fflush(stdout);
if (fgets(password, sizeof(password), stdin) != NULL) {
size_t length = strlen(password);
if (length > 0 && (password[length - 1] == '\n'))
password[length - 1] = '\0';
if (changed) {
putchar('\n');
}
}
if (changed)
tcsetattr(fileno(stdin), TCSAFLUSH, &termios);
return password;
}