xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/getpass.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*5d54f3d8Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <termios.h>
37*5d54f3d8Smuffin #include <unistd.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern void setbuf();
40*5d54f3d8Smuffin extern int	fclose(FILE *);
41*5d54f3d8Smuffin extern int	fprintf(FILE *, char *, ...);
42*5d54f3d8Smuffin extern int	findiop();
43*5d54f3d8Smuffin extern int ioctl();
447c478bd9Sstevel@tonic-gate static int intrupt;
457c478bd9Sstevel@tonic-gate 
46*5d54f3d8Smuffin static void	catch(void);
47*5d54f3d8Smuffin 
487c478bd9Sstevel@tonic-gate #define	MAXPASSWD	8	/* max significant characters in password */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate char *
51*5d54f3d8Smuffin getpass(char *prompt)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	struct termios ttyb;
54*5d54f3d8Smuffin 	long flags;
55*5d54f3d8Smuffin 	char *p;
56*5d54f3d8Smuffin 	int c;
577c478bd9Sstevel@tonic-gate 	FILE	*fi;
587c478bd9Sstevel@tonic-gate 	static char pbuf[ MAXPASSWD + 1 ];
597c478bd9Sstevel@tonic-gate 	struct sigvec osv, sv;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if((fi = fopen("/dev/tty", "r")) == NULL)
627c478bd9Sstevel@tonic-gate #ifdef S5EMUL
637c478bd9Sstevel@tonic-gate 		return((char*)NULL);
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate 		fi = stdin;
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 	else
687c478bd9Sstevel@tonic-gate 		setbuf(fi, (char*)NULL);
697c478bd9Sstevel@tonic-gate 	sv.sv_handler = catch;
707c478bd9Sstevel@tonic-gate 	sv.sv_mask = 0;
717c478bd9Sstevel@tonic-gate 	sv.sv_flags = SV_INTERRUPT;
727c478bd9Sstevel@tonic-gate 	(void) sigvec(SIGINT, &sv, &osv);
737c478bd9Sstevel@tonic-gate 	intrupt = 0;
747c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCGETS, &ttyb);
757c478bd9Sstevel@tonic-gate 	flags = ttyb.c_lflag;
767c478bd9Sstevel@tonic-gate 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
777c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETSF, &ttyb);
787c478bd9Sstevel@tonic-gate 	(void) fputs(prompt, stderr);
797c478bd9Sstevel@tonic-gate 	p = pbuf;
807c478bd9Sstevel@tonic-gate 	while( !intrupt  &&
817c478bd9Sstevel@tonic-gate 		(c = getc(fi)) != '\n'  &&  c != '\r'  &&  c != EOF ) {
827c478bd9Sstevel@tonic-gate 		if(p < &pbuf[ MAXPASSWD ])
837c478bd9Sstevel@tonic-gate 			*p++ = c;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	*p = '\0';
867c478bd9Sstevel@tonic-gate 	ttyb.c_lflag = flags;
877c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(fi), TCSETSW, &ttyb);
887c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
897c478bd9Sstevel@tonic-gate 	(void) sigvec(SIGINT, &osv, (struct sigvec *)NULL);
907c478bd9Sstevel@tonic-gate 	if(fi != stdin)
917c478bd9Sstevel@tonic-gate 		(void) fclose(fi);
927c478bd9Sstevel@tonic-gate #ifdef S5EMUL	/* XXX - BOTH versions should probably do this! */
937c478bd9Sstevel@tonic-gate 	if(intrupt)
947c478bd9Sstevel@tonic-gate 		(void) kill(getpid(), SIGINT);
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 	return(pbuf);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static void
100*5d54f3d8Smuffin catch(void)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	++intrupt;
1037c478bd9Sstevel@tonic-gate }
104