135669Sbostic /* 235669Sbostic * Copyright (c) 1988 The Regents of the University of California. 335669Sbostic * All rights reserved. 435669Sbostic * 542624Sbostic * %sccs.include.redist.c% 635669Sbostic */ 735669Sbostic 826559Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46597Sdonn static char sccsid[] = "@(#)getpass.c 5.8 (Berkeley) 02/23/91"; 1035669Sbostic #endif /* LIBC_SCCS and not lint */ 1122094Smckusick 1240101Smarc #include <sys/termios.h> 1335669Sbostic #include <sys/signal.h> 142017Swnj #include <stdio.h> 15*46597Sdonn #include <unistd.h> 1640101Smarc #include <pwd.h> 172017Swnj 182017Swnj char * 192017Swnj getpass(prompt) 20*46597Sdonn const char *prompt; 212017Swnj { 2240101Smarc struct termios term; 2335669Sbostic register int ch; 242017Swnj register char *p; 2535669Sbostic FILE *fp, *outfp; 2635669Sbostic long omask; 2740101Smarc int echo; 2840101Smarc static char buf[_PASSWORD_LEN + 1]; 292017Swnj 3035669Sbostic /* 3135669Sbostic * read and write to /dev/tty if possible; else read from 3235669Sbostic * stdin and write to stderr. 3335669Sbostic */ 3435669Sbostic if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) { 3535669Sbostic outfp = stderr; 3635669Sbostic fp = stdin; 3735669Sbostic } 3840101Smarc /* 3940101Smarc * note - blocking signals isn't necessarily the 4040101Smarc * right thing, but we leave it for now. 4140101Smarc */ 4240101Smarc omask = sigblock(sigmask(SIGINT)|sigmask(SIGTSTP)); 4340101Smarc (void)tcgetattr(fileno(fp), &term); 4440101Smarc if (echo = (term.c_lflag & ECHO)) { 4540101Smarc term.c_lflag &= ~ECHO; 4640101Smarc term.c_cflag |= CIGNORE; 4743200Sbostic (void)tcsetattr(fileno(fp), TCSAFLUSH, &term); 4840101Smarc } 4937901Sbostic (void)fputs(prompt, outfp); 5035669Sbostic rewind(outfp); /* implied flush */ 5135669Sbostic for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) 5240101Smarc if (p < buf + _PASSWORD_LEN) 5335669Sbostic *p++ = ch; 542017Swnj *p = '\0'; 5535669Sbostic (void)write(fileno(outfp), "\n", 1); 5640101Smarc if (echo) { 5740101Smarc term.c_lflag |= ECHO; 5840101Smarc term.c_cflag |= CIGNORE; 5943200Sbostic tcsetattr(fileno(fp), TCSAFLUSH, &term); 6040101Smarc } 6135669Sbostic (void)sigsetmask(omask); 6235669Sbostic if (fp != stdin) 6335669Sbostic (void)fclose(fp); 6435669Sbostic return(buf); 652017Swnj } 66