[comp.sys.ibm.pc] Password Entry Routines

schaut@cat9.cs.wisc.edu (Rick Schaut) (02/08/90)

In article <V&5S`+@rpi.edu> mcintyre@turing.cs.rpi.edu (David McIntyre) writes:
| In article <1036@spocm2.UUCP> lhv@spocm2.UUCP (Leo Vermeulen) writes:
| >
| >I have the following problem: I'm trying to write a C-program, using
| >Microsoft-C 5.1, that reads a character string from the keyboard and
| >assigns it to an character array. Since this string is supposed to be
| >a password, I do not want it echoed to the display.
| >
| >Any responses, by e-mail, would be greatly appreciated.
| >
| 
| I sent Leo a response by email, but in case anyone else is wondering
| about this:  MSC5.1 has a function called getch(), which gets a
| character from the console without echo.  

I don't know about MSC, but Borland's Turbo C 2.0 has a getpass routine
that takes a pointer to a prompt and returns the characters entered without
echoing those characters to the screen.

--
Rick (schaut@garfield.cs.wisc.edu)

Peace and Prejudice Don't Mix! (unknown add copy)

tcm@srhqla.SR.COM (Tim Meighan) (02/08/90)

In article <V&5S`+@rpi.edu> mcintyre@turing.cs.rpi.edu
(David McIntyre) writes:

> I sent Leo a response by email, but in case anyone else is wondering
> about this:  MSC5.1 has a function called getch(), which gets a
> character from the console without echo.  

getc() is part of the standard C library.  getch() is a library function to
invoke getc() using standard input.  Both of these should be available in all
commerical C compiliers.  The getc() family does not echo characters.

kbhit() is NOT a standard C function.  It is included with the MSC library.
It allows you to find out if there are any characters waiting in the keyboard
input buffer.  This can be very helpful because if you call getch() and there
are no characters available from the source of input your program will hang
in the getch() routine until one is received.

An extra pitfall of IBM-PCs is that function keys return TWO characters,
not one.  A call to getch() that returns a 0 indicates that there is another
character waiting.  You can write a routine that makes a second call to getch()
under such circumstances to get the function key value.  Adding a constant to
this second call will yield a value above the normal character return 
values of 0 to 255, returning to you a single unique integer value regardless
of what key (if any) was pressed.  Example:

/*
 *  GET_KEY() checks for IBM-PC keyboard input, returning a -1 if no character
 *  is available, 0 to 255 for regular keypresses, and values above 255 for
 *  function keypresses.  Note that the function kbhit() is not from the
 *  standard library but is part of MSC.  Use this function ONLY when
 *  standard input will be the PC keyboard.
 */

int get_key()
{
	int c;

	if(kbhit())
	{
		if((c = getch()) == 0)
		{
			c = (getch() + 197);
			return(c);
		}
	}
	return(-1);
}

Tim Meighan
Silent Radio