Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / misc / getpass.c
1 /*
2  * getpass - ask for a password
3  */
4 /* $Id: getpass.c,v 1.3 1994/06/24 11:45:18 ceriel Exp $ */
5
6 #include        <signal.h>
7 #include        <string.h>
8 #include        <sgtty.h>
9
10 #define O_RDONLY        0
11 int _open(const char *path, int flags);
12 int _write(int d, const char *buf, int nbytes);
13 int _read(int d, char *buf, int nbytes);
14 int _close(int d);
15
16 int _stty(int, struct sgttyb *);
17 int _gtty(int, struct sgttyb *);
18
19 char *
20 getpass(const char *prompt)
21 {
22         int i = 0;
23         struct sgttyb tty, ttysave;
24         static char pwdbuf[9];
25         int fd;
26         void (*savesig)(int);
27
28         if ((fd = _open("/dev/tty", O_RDONLY)) < 0) fd = 0;
29         savesig = signal(SIGINT, SIG_IGN);
30         _write(2, prompt, strlen(prompt));
31         _gtty(fd, &tty);
32         ttysave = tty;
33         tty.sg_flags &= ~ECHO;
34         _stty(fd, &tty);
35         i = _read(fd, pwdbuf, 9);
36         while (pwdbuf[i - 1] != '\n')
37                 _read(fd, &pwdbuf[i - 1], 1);
38         pwdbuf[i - 1] = '\0';
39         _stty(fd, &ttysave);
40         _write(2, "\n", 1);
41         if (fd != 0) _close(fd);
42         signal(SIGINT, savesig);
43         return(pwdbuf);
44 }