xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/getpass.c (revision 2eeaed14a5e2ed9bd811643ad5bffc3510ca0310)
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"
32 
33 /*LINTLIBRARY*/
34 #include <stdio.h>
35 #include <signal.h>
36 #include <termios.h>
37 #include <unistd.h>
38 
39 extern void setbuf();
40 extern int	fclose(FILE *);
41 extern int	fprintf(FILE *, char *, ...);
42 extern int	findiop();
43 extern int ioctl();
44 static int intrupt;
45 
46 static void	catch(void);
47 
48 #define	MAXPASSWD	8	/* max significant characters in password */
49 
50 char *
51 getpass(char *prompt)
52 {
53 	struct termios ttyb;
54 	long flags;
55 	char *p;
56 	int c;
57 	FILE	*fi;
58 	static char pbuf[ MAXPASSWD + 1 ];
59 	struct sigvec osv, sv;
60 
61 	if((fi = fopen("/dev/tty", "r")) == NULL)
62 #ifdef S5EMUL
63 		return((char*)NULL);
64 #else
65 		fi = stdin;
66 #endif
67 	else
68 		setbuf(fi, (char*)NULL);
69 	sv.sv_handler = catch;
70 	sv.sv_mask = 0;
71 	sv.sv_flags = SV_INTERRUPT;
72 	(void) sigvec(SIGINT, &sv, &osv);
73 	intrupt = 0;
74 	(void) ioctl(fileno(fi), TCGETS, &ttyb);
75 	flags = ttyb.c_lflag;
76 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
77 	(void) ioctl(fileno(fi), TCSETSF, &ttyb);
78 	(void) fputs(prompt, stderr);
79 	p = pbuf;
80 	while( !intrupt  &&
81 		(c = getc(fi)) != '\n'  &&  c != '\r'  &&  c != EOF ) {
82 		if(p < &pbuf[ MAXPASSWD ])
83 			*p++ = c;
84 	}
85 	*p = '\0';
86 	ttyb.c_lflag = flags;
87 	(void) ioctl(fileno(fi), TCSETSW, &ttyb);
88 	(void) putc('\n', stderr);
89 	(void) sigvec(SIGINT, &osv, (struct sigvec *)NULL);
90 	if(fi != stdin)
91 		(void) fclose(fi);
92 #ifdef S5EMUL	/* XXX - BOTH versions should probably do this! */
93 	if(intrupt)
94 		(void) kill(getpid(), SIGINT);
95 #endif
96 	return(pbuf);
97 }
98 
99 static void
100 catch(void)
101 {
102 	++intrupt;
103 }
104