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*49225Sbostic static char sccsid[] = "@(#)getpass.c 5.9 (Berkeley) 05/06/91"; 1035669Sbostic #endif /* LIBC_SCCS and not lint */ 1122094Smckusick 1240101Smarc #include <sys/termios.h> 1335669Sbostic #include <sys/signal.h> 142017Swnj #include <stdio.h> 1546597Sdonn #include <unistd.h> 1640101Smarc #include <pwd.h> 172017Swnj 182017Swnj char * 192017Swnj getpass(prompt) 2046597Sdonn 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; 46*49225Sbostic (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term); 4740101Smarc } 4837901Sbostic (void)fputs(prompt, outfp); 4935669Sbostic rewind(outfp); /* implied flush */ 5035669Sbostic for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) 5140101Smarc if (p < buf + _PASSWORD_LEN) 5235669Sbostic *p++ = ch; 532017Swnj *p = '\0'; 5435669Sbostic (void)write(fileno(outfp), "\n", 1); 5540101Smarc if (echo) { 5640101Smarc term.c_lflag |= ECHO; 57*49225Sbostic tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term); 5840101Smarc } 5935669Sbostic (void)sigsetmask(omask); 6035669Sbostic if (fp != stdin) 6135669Sbostic (void)fclose(fp); 6235669Sbostic return(buf); 632017Swnj } 64