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.3 (Berkeley) 09/22/88"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/ioctl.h> 23 #include <sys/signal.h> 24 #include <stdio.h> 25 26 char * 27 getpass(prompt) 28 char *prompt; 29 { 30 struct sgttyb ttyb; 31 register int ch; 32 register char *p; 33 FILE *fp, *outfp; 34 long omask; 35 int svflagval; 36 #define PASSWD_LEN 8 37 static char buf[PASSWD_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 (void)ioctl(fileno(fp), TIOCGETP, &ttyb); 49 svflagval = ttyb.sg_flags; 50 ttyb.sg_flags &= ~ECHO; 51 omask = sigblock(sigmask(SIGINT)); 52 (void)ioctl(fileno(fp), TIOCSETP, &ttyb); 53 54 fputs(prompt, outfp); 55 rewind(outfp); /* implied flush */ 56 for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) 57 if (p < buf + PASSWD_LEN) 58 *p++ = ch; 59 *p = '\0'; 60 (void)write(fileno(outfp), "\n", 1); 61 62 ttyb.sg_flags = svflagval; 63 (void)ioctl(fileno(fp), TIOCSETP, &ttyb); 64 (void)sigsetmask(omask); 65 if (fp != stdin) 66 (void)fclose(fp); 67 return(buf); 68 } 69