BSD 3 development
[unix-history] / usr / src / libc / stdio / getpass.c
#include <stdio.h>
#include <signal.h>
#include <sgtty.h>
char *
getpass(prompt)
char *prompt;
{
struct sgttyb ttyb;
int flags;
register char *p;
register c;
FILE *fi;
static char pbuf[9];
int (*signal())();
int (*sig)();
if ((fi = fopen("/dev/tty", "r")) == NULL)
fi = stdin;
else
setbuf(fi, (char *)NULL);
sig = signal(SIGINT, SIG_IGN);
gtty(fileno(fi), &ttyb);
flags = ttyb.sg_flags;
ttyb.sg_flags &= ~ECHO;
stty(fileno(fi), &ttyb);
fprintf(stderr, prompt);
for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
if (p < &pbuf[8])
*p++ = c;
}
*p = '\0';
fprintf(stderr, "\n");
ttyb.sg_flags = flags;
stty(fileno(fi), &ttyb);
signal(SIGINT, sig);
if (fi != stdin)
fclose(fi);
return(pbuf);
}