the window strings is NOT quoted at this point; other cleanups
[unix-history] / usr / src / lib / libc / gen / getpass.c
CommitLineData
2ce81398
DS
1#if defined(LIBC_SCCS) && !defined(lint)
2static char sccsid[] = "@(#)getpass.c 5.2 (Berkeley) %G%";
3#endif LIBC_SCCS and not lint
b8f253e8 4
6a4d2ab3
BJ
5#include <stdio.h>
6#include <signal.h>
7#include <sgtty.h>
8
9char *
10getpass(prompt)
11char *prompt;
12{
13 struct sgttyb ttyb;
14 int flags;
15 register char *p;
16 register c;
17 FILE *fi;
18 static char pbuf[9];
19 int (*signal())();
20 int (*sig)();
21
22 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
23 fi = stdin;
24 else
25 setbuf(fi, (char *)NULL);
26 sig = signal(SIGINT, SIG_IGN);
dbb176a2 27 ioctl(fileno(fi), TIOCGETP, &ttyb);
6a4d2ab3
BJ
28 flags = ttyb.sg_flags;
29 ttyb.sg_flags &= ~ECHO;
dbb176a2 30 ioctl(fileno(fi), TIOCSETP, &ttyb);
790acfed 31 fprintf(stderr, "%s", prompt); fflush(stderr);
6a4d2ab3
BJ
32 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
33 if (p < &pbuf[8])
34 *p++ = c;
35 }
36 *p = '\0';
790acfed 37 fprintf(stderr, "\n"); fflush(stderr);
6a4d2ab3 38 ttyb.sg_flags = flags;
dbb176a2 39 ioctl(fileno(fi), TIOCSETP, &ttyb);
6a4d2ab3
BJ
40 signal(SIGINT, sig);
41 if (fi != stdin)
42 fclose(fi);
43 return(pbuf);
44}