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

46 lines
935 B
C
Raw Normal View History

1989-12-18 14:40:54 +00:00
/*
* getpass - ask for a password
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-12-18 14:40:54 +00:00
#include <stdlib.h>
1989-12-18 14:40:54 +00:00
#include <signal.h>
#include <string.h>
#include <sgtty.h>
#include <fcntl.h>
1989-12-18 14:40:54 +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
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);
}