#include <stdio.h>
#include <iodef.h>

#define	CHECK(q, p)	if ( !((q) & 1) ) { printf("%s failed %d\n", (p),(q)); return NULL;}
#define DEL 127

/*
** 	Read a password from the user's terminal w/o echoing.
**
*/

char * get_password ()
{
		int	i = 0, status;
		short	chan, iosb[4];
		char	*term = "TT:", ch;
		static  char	password[256];
		int	t_des[2] = {strlen(term), (int)term};
		int	func = IO$_READVBLK | IO$M_NOFILTR | IO$M_TRMNOECHO | IO$M_NOECHO;
		char	*prompt = "Enter password";

	bzero(password, sizeof(password));
	status = sys$assign(t_des, &chan, 0, 0);
	CHECK(status, "get_password: sys$assign");
		
	printf("\nEnter password: ");
	for (;;) {
       		status = sys$qiow(0, chan, func, iosb, 0, 0, &ch, 1, 0, 0, 0, 0);
		CHECK(status, "get_password: sys$qiow");
		CHECK(iosb[0], "get_password: sys$qiow-iosb");
		if ( ch == '\032') return(NULL);
		if ( ch == '\r'  || ch  == '\n'  || i > sizeof(password))
			break;
		if ( ch !=  DEL)
			password[i++] = ch;
		else if ( i > 0 )			
			--i;
		/* Incase the DEL key is hit, we've already backed up over
		 * the char, but we want to erase it [& NULL terminate the
		 * string anyways]
		 */
		password[i] = '\0';
	}	
	sys$dassgn(chan);
	printf("\n");
	if ( i  >= sizeof(password)-1 )  /* Too big */
		return(NULL);
	else 
		return(password);
}
