xref: /titanic_44/usr/src/lib/libast/common/uwin/getpass.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include "FEATURE/uwin"
23 
24 #if !_UWIN || _lib_getpass
25 
26 void _STUB_getpass(){}
27 
28 #else
29 
30 #pragma prototyped
31 
32 #define getpass	______getpass
33 
34 #include	<ast.h>
35 #include	<termios.h>
36 #include	<signal.h>
37 
38 #undef	getpass
39 
40 #if defined(__EXPORT__)
41 #define extern		__EXPORT__
42 #endif
43 
44 static int interrupt;
45 static void handler(int sig)
46 {
47 	interrupt++;
48 }
49 
50 extern char*	getpass(const char *prompt)
51 {
52 	struct termios told,tnew;
53 	Sfio_t *iop;
54 	static char *cp, passwd[32];
55 	void (*savesig)(int);
56 	if(!(iop = sfopen((Sfio_t*)0, "/dev/tty", "r")))
57 		return(0);
58 	if(tcgetattr(sffileno(iop),&told) < 0)
59 		return(0);
60 	interrupt = 0;
61 	tnew = told;
62 	tnew.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
63 	if(tcsetattr(sffileno(iop),TCSANOW,&tnew) < 0)
64 		return(0);
65 	savesig = signal(SIGINT, handler);
66 	sfputr(sfstderr,prompt,-1);
67 	if(cp = sfgetr(iop,'\n',1))
68 		strncpy(passwd,cp,sizeof(passwd)-1);
69 	tcsetattr(sffileno(iop),TCSANOW,&told);
70 	sfputc(sfstderr,'\n');
71 	sfclose(iop);
72 	signal(SIGINT, savesig);
73 	if(interrupt)
74 		kill(getpid(),SIGINT);
75 	return(cp?passwd:0);
76 }
77 
78 
79 #endif
80