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

47 lines
931 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
2018-06-21 20:33:47 +00:00
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sgtty.h>
#include <fcntl.h>
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +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
2018-06-21 20:33:47 +00:00
int _stty(int, struct sgttyb*);
int _gtty(int, struct sgttyb*);
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
char* getpass(const char* prompt)
1989-12-18 14:40:54 +00:00
{
int i = 0;
struct sgttyb tty, ttysave;
static char pwdbuf[9];
int fd;
void (*savesig)(int);
2018-06-21 20:33:47 +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);
_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);
2018-06-21 20:33:47 +00:00
if (fd != 0)
_close(fd);
1989-12-18 14:40:54 +00:00
signal(SIGINT, savesig);
2018-06-21 20:33:47 +00:00
return (pwdbuf);
1989-12-18 14:40:54 +00:00
}