xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/getpass.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
31*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*LINTLIBRARY*/
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <termios.h>
37*722Smuffin #include <unistd.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate extern void setbuf();
40*722Smuffin extern int	fclose(FILE *);
41*722Smuffin extern int	fprintf(FILE *, char *, ...);
42*722Smuffin extern int	findiop();
43*722Smuffin extern int ioctl();
440Sstevel@tonic-gate static int intrupt;
450Sstevel@tonic-gate 
46*722Smuffin static void	catch(void);
47*722Smuffin 
480Sstevel@tonic-gate #define	MAXPASSWD	8	/* max significant characters in password */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate char *
getpass(char * prompt)51*722Smuffin getpass(char *prompt)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	struct termios ttyb;
54*722Smuffin 	long flags;
55*722Smuffin 	char *p;
56*722Smuffin 	int c;
570Sstevel@tonic-gate 	FILE	*fi;
580Sstevel@tonic-gate 	static char pbuf[ MAXPASSWD + 1 ];
590Sstevel@tonic-gate 	struct sigvec osv, sv;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if((fi = fopen("/dev/tty", "r")) == NULL)
620Sstevel@tonic-gate #ifdef S5EMUL
630Sstevel@tonic-gate 		return((char*)NULL);
640Sstevel@tonic-gate #else
650Sstevel@tonic-gate 		fi = stdin;
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate 	else
680Sstevel@tonic-gate 		setbuf(fi, (char*)NULL);
690Sstevel@tonic-gate 	sv.sv_handler = catch;
700Sstevel@tonic-gate 	sv.sv_mask = 0;
710Sstevel@tonic-gate 	sv.sv_flags = SV_INTERRUPT;
720Sstevel@tonic-gate 	(void) sigvec(SIGINT, &sv, &osv);
730Sstevel@tonic-gate 	intrupt = 0;
740Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCGETS, &ttyb);
750Sstevel@tonic-gate 	flags = ttyb.c_lflag;
760Sstevel@tonic-gate 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
770Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETSF, &ttyb);
780Sstevel@tonic-gate 	(void) fputs(prompt, stderr);
790Sstevel@tonic-gate 	p = pbuf;
800Sstevel@tonic-gate 	while( !intrupt  &&
810Sstevel@tonic-gate 		(c = getc(fi)) != '\n'  &&  c != '\r'  &&  c != EOF ) {
820Sstevel@tonic-gate 		if(p < &pbuf[ MAXPASSWD ])
830Sstevel@tonic-gate 			*p++ = c;
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	*p = '\0';
860Sstevel@tonic-gate 	ttyb.c_lflag = flags;
870Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETSW, &ttyb);
880Sstevel@tonic-gate 	(void) putc('\n', stderr);
890Sstevel@tonic-gate 	(void) sigvec(SIGINT, &osv, (struct sigvec *)NULL);
900Sstevel@tonic-gate 	if(fi != stdin)
910Sstevel@tonic-gate 		(void) fclose(fi);
920Sstevel@tonic-gate #ifdef S5EMUL	/* XXX - BOTH versions should probably do this! */
930Sstevel@tonic-gate 	if(intrupt)
940Sstevel@tonic-gate 		(void) kill(getpid(), SIGINT);
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 	return(pbuf);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static void
catch(void)100*722Smuffin catch(void)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	++intrupt;
1030Sstevel@tonic-gate }
104