xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/getpass.c (revision 84ab085a13f931bc78e7415e7ce921dbaa14fcb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R3 1.10 */
32 
33 /*LINTLIBRARY*/
34 #include <stdio.h>
35 #include <signal.h>
36 #include <termios.h>
37 
38 extern void setbuf();
39 extern FILE *fopen();
40 extern int fclose(), fprintf(), findiop();
41 extern int kill(), ioctl(), getpid();
42 static int intrupt;
43 
44 #define	MAXPASSWD	8	/* max significant characters in password */
45 
46 char *
47 getpass(prompt)
48 char	*prompt;
49 {
50 	struct termios ttyb;
51 	unsigned long flags;
52 	register char *p;
53 	register int c;
54 	FILE	*fi;
55 	static char pbuf[ MAXPASSWD + 1 ];
56 	struct sigvec osv, sv;
57 	void	catch();
58 
59 	if((fi = fopen("/dev/tty", "r")) == NULL)
60 #ifdef S5EMUL
61 		return((char*)NULL);
62 #else
63 		fi = stdin;
64 #endif
65 	else
66 		setbuf(fi, (char*)NULL);
67 	sv.sv_handler = catch;
68 	sv.sv_mask = 0;
69 	sv.sv_flags = SV_INTERRUPT;
70 	(void) sigvec(SIGINT, &sv, &osv);
71 	intrupt = 0;
72 	(void) ioctl(fileno(fi), TCGETS, &ttyb);
73 	flags = ttyb.c_lflag;
74 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
75 	(void) ioctl(fileno(fi), TCSETSF, &ttyb);
76 	(void) fputs(prompt, stderr);
77 	p = pbuf;
78 	while( !intrupt  &&
79 		(c = getc(fi)) != '\n'  &&  c != '\r'  &&  c != EOF ) {
80 		if(p < &pbuf[ MAXPASSWD ])
81 			*p++ = c;
82 	}
83 	*p = '\0';
84 	ttyb.c_lflag = flags;
85 	(void) ioctl(fileno(fi), TCSETSW, &ttyb);
86 	(void) putc('\n', stderr);
87 	(void) sigvec(SIGINT, &osv, (struct sigvec *)NULL);
88 	if(fi != stdin)
89 		(void) fclose(fi);
90 #ifdef S5EMUL	/* XXX - BOTH versions should probably do this! */
91 	if(intrupt)
92 		(void) kill(getpid(), SIGINT);
93 #endif
94 	return(pbuf);
95 }
96 
97 static void
98 catch()
99 {
100 	++intrupt;
101 }
102