ack/lang/cem/libcc.ansi/misc/getpass.c

45 lines
913 B
C
Raw Normal View History

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
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
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);
if ((fd = _open("/dev/tty", O_RDONLY)) < 0) fd = 0;
1989-12-18 14:40:54 +00:00
savesig = signal(SIGINT, SIG_IGN);
_write(2, prompt, strlen(prompt));
_gtty(fd, &tty);
1989-12-18 14:40:54 +00:00
ttysave = tty;
tty.sg_flags &= ~ECHO;
_stty(fd, &tty);
i = _read(fd, pwdbuf, 9);
1989-12-18 14:40:54 +00:00
while (pwdbuf[i - 1] != '\n')
_read(fd, &pwdbuf[i - 1], 1);
1989-12-18 14:40:54 +00:00
pwdbuf[i - 1] = '\0';
_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);
}