1 /* @(#)getpass.c 4.2 (Berkeley) 07/01/81 */ 2 #include <stdio.h> 3 #include <signal.h> 4 #include <sgtty.h> 5 6 char * 7 getpass(prompt) 8 char *prompt; 9 { 10 struct sgttyb ttyb; 11 int flags; 12 register char *p; 13 register c; 14 FILE *fi; 15 static char pbuf[9]; 16 int (*signal())(); 17 int (*sig)(); 18 19 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL) 20 fi = stdin; 21 else 22 setbuf(fi, (char *)NULL); 23 sig = signal(SIGINT, SIG_IGN); 24 gtty(fileno(fi), &ttyb); 25 flags = ttyb.sg_flags; 26 ttyb.sg_flags &= ~ECHO; 27 stty(fileno(fi), &ttyb); 28 fprintf(stderr, "%s", prompt); fflush(stderr); 29 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) { 30 if (p < &pbuf[8]) 31 *p++ = c; 32 } 33 *p = '\0'; 34 fprintf(stderr, "\n"); fflush(stderr); 35 ttyb.sg_flags = flags; 36 stty(fileno(fi), &ttyb); 37 signal(SIGINT, sig); 38 if (fi != stdin) 39 fclose(fi); 40 return(pbuf); 41 } 42