1 /* 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)getpass.c 5.5 (Berkeley) 02/15/90"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/termios.h> 23 #include <sys/signal.h> 24 #include <stdio.h> 25 #include <pwd.h> 26 27 char * 28 getpass(prompt) 29 char *prompt; 30 { 31 struct termios term; 32 register int ch; 33 register char *p; 34 FILE *fp, *outfp; 35 long omask; 36 int echo; 37 static char buf[_PASSWORD_LEN + 1]; 38 39 /* 40 * read and write to /dev/tty if possible; else read from 41 * stdin and write to stderr. 42 */ 43 if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) { 44 outfp = stderr; 45 fp = stdin; 46 } 47 /* 48 * note - blocking signals isn't necessarily the 49 * right thing, but we leave it for now. 50 */ 51 omask = sigblock(sigmask(SIGINT)|sigmask(SIGTSTP)); 52 (void)tcgetattr(fileno(fp), &term); 53 if (echo = (term.c_lflag & ECHO)) { 54 term.c_lflag &= ~ECHO; 55 term.c_cflag |= CIGNORE; 56 (void)tcsetattr(fileno(fp), TCSADFLUSH, &term); 57 } 58 (void)fputs(prompt, outfp); 59 rewind(outfp); /* implied flush */ 60 for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) 61 if (p < buf + _PASSWORD_LEN) 62 *p++ = ch; 63 *p = '\0'; 64 (void)write(fileno(outfp), "\n", 1); 65 if (echo) { 66 term.c_lflag |= ECHO; 67 term.c_cflag |= CIGNORE; 68 tcsetattr(fileno(fp), TCSADFLUSH, &term); 69 } 70 (void)sigsetmask(omask); 71 if (fp != stdin) 72 (void)fclose(fp); 73 return(buf); 74 } 75