1989-12-18 14:40:54 +00:00
|
|
|
/*
|
|
|
|
* getpass - ask for a password
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sgtty.h>
|
|
|
|
|
|
|
|
#define O_RDONLY 0
|
1990-01-22 11:44:21 +00:00
|
|
|
int _open(const char *path, int flags);
|
|
|
|
int _write(int d, const char *buf, int nbytes);
|
|
|
|
int _read(int d, char *buf, int nbytes);
|
|
|
|
int _close(int d);
|
1989-12-18 14:40:54 +00:00
|
|
|
|
1990-01-22 11:44:21 +00:00
|
|
|
int _stty(int, struct sgttyb *);
|
|
|
|
int _gtty(int, struct sgttyb *);
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
getpass(const char *prompt)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
struct sgttyb tty, ttysave;
|
|
|
|
static char pwdbuf[9];
|
|
|
|
int fd;
|
|
|
|
void (*savesig)(int);
|
|
|
|
|
1990-01-22 11:44:21 +00:00
|
|
|
if ((fd = _open("/dev/tty", O_RDONLY)) < 0) fd = 0;
|
1989-12-18 14:40:54 +00:00
|
|
|
savesig = signal(SIGINT, SIG_IGN);
|
1990-01-22 11:44:21 +00:00
|
|
|
_write(2, prompt, strlen(prompt));
|
|
|
|
_gtty(fd, &tty);
|
1989-12-18 14:40:54 +00:00
|
|
|
ttysave = tty;
|
|
|
|
tty.sg_flags &= ~ECHO;
|
1990-01-22 11:44:21 +00:00
|
|
|
_stty(fd, &tty);
|
|
|
|
i = _read(fd, pwdbuf, 9);
|
1989-12-18 14:40:54 +00:00
|
|
|
while (pwdbuf[i - 1] != '\n')
|
1990-01-22 11:44:21 +00:00
|
|
|
_read(fd, &pwdbuf[i - 1], 1);
|
1989-12-18 14:40:54 +00:00
|
|
|
pwdbuf[i - 1] = '\0';
|
1990-01-22 11:44:21 +00:00
|
|
|
_stty(fd, &ttysave);
|
|
|
|
_write(2, "\n", 1);
|
|
|
|
if (fd != 0) _close(fd);
|
1989-12-18 14:40:54 +00:00
|
|
|
signal(SIGINT, savesig);
|
|
|
|
return(pwdbuf);
|
|
|
|
}
|