xref: /csrg-svn/lib/libc/gen/getpass.c (revision 35669)
1*35669Sbostic /*
2*35669Sbostic  * Copyright (c) 1988 The Regents of the University of California.
3*35669Sbostic  * All rights reserved.
4*35669Sbostic  *
5*35669Sbostic  * Redistribution and use in source and binary forms are permitted
6*35669Sbostic  * provided that the above copyright notice and this paragraph are
7*35669Sbostic  * duplicated in all such forms and that any documentation,
8*35669Sbostic  * advertising materials, and other materials related to such
9*35669Sbostic  * distribution and use acknowledge that the software was developed
10*35669Sbostic  * by the University of California, Berkeley.  The name of the
11*35669Sbostic  * University may not be used to endorse or promote products derived
12*35669Sbostic  * from this software without specific prior written permission.
13*35669Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35669Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35669Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*35669Sbostic  */
17*35669Sbostic 
1826559Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*35669Sbostic static char sccsid[] = "@(#)getpass.c	5.3 (Berkeley) 09/22/88";
20*35669Sbostic #endif /* LIBC_SCCS and not lint */
2122094Smckusick 
22*35669Sbostic #include <sys/ioctl.h>
23*35669Sbostic #include <sys/signal.h>
242017Swnj #include <stdio.h>
252017Swnj 
262017Swnj char *
272017Swnj getpass(prompt)
28*35669Sbostic 	char *prompt;
292017Swnj {
302017Swnj 	struct sgttyb ttyb;
31*35669Sbostic 	register int ch;
322017Swnj 	register char *p;
33*35669Sbostic 	FILE *fp, *outfp;
34*35669Sbostic 	long omask;
35*35669Sbostic 	int svflagval;
36*35669Sbostic #define	PASSWD_LEN	8
37*35669Sbostic 	static char buf[PASSWD_LEN + 1];
382017Swnj 
39*35669Sbostic 	/*
40*35669Sbostic 	 * read and write to /dev/tty if possible; else read from
41*35669Sbostic 	 * stdin and write to stderr.
42*35669Sbostic 	 */
43*35669Sbostic 	if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) {
44*35669Sbostic 		outfp = stderr;
45*35669Sbostic 		fp = stdin;
46*35669Sbostic 	}
47*35669Sbostic 
48*35669Sbostic 	(void)ioctl(fileno(fp), TIOCGETP, &ttyb);
49*35669Sbostic 	svflagval = ttyb.sg_flags;
502017Swnj 	ttyb.sg_flags &= ~ECHO;
51*35669Sbostic 	omask = sigblock(sigmask(SIGINT));
52*35669Sbostic 	(void)ioctl(fileno(fp), TIOCSETP, &ttyb);
53*35669Sbostic 
54*35669Sbostic 	fputs(prompt, outfp);
55*35669Sbostic 	rewind(outfp);			/* implied flush */
56*35669Sbostic 	for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';)
57*35669Sbostic 		if (p < buf + PASSWD_LEN)
58*35669Sbostic 			*p++ = ch;
592017Swnj 	*p = '\0';
60*35669Sbostic 	(void)write(fileno(outfp), "\n", 1);
61*35669Sbostic 
62*35669Sbostic 	ttyb.sg_flags = svflagval;
63*35669Sbostic 	(void)ioctl(fileno(fp), TIOCSETP, &ttyb);
64*35669Sbostic 	(void)sigsetmask(omask);
65*35669Sbostic 	if (fp != stdin)
66*35669Sbostic 		(void)fclose(fp);
67*35669Sbostic 	return(buf);
682017Swnj }
69