Here's my version (based on MacGyver's getch() function):
Code:
/* prototypes */
char mygetch(void);
void getpassword(char password[], int maxlength);
/* input password from user */
void getpassword(char password[], int maxlength)
{
int i=0, done=0;
char c;
printf("Please input password (max. %d characters): ", maxlength);
fflush(stdout);
while (!done)
{
c = mygetch();
switch(c)
{
case '\r':
case '\n':
done = 1;
break;
case '\b':
if (i > 0) /* perform backspace only if characters have already been input */
{
printf("\b \b");
i--;
}
break;
default:
if (i<maxlength && isprint(c)) /* save character in password as long as it's not a none-printing character */
{
password[i++] = c;
putchar('*');
}
break;
}
fflush(stdout);
}
password[i] = '\0';
}
/* like getchar() except without echo */
char mygetch(void)
{
static HANDLE hIn;
static DWORD NumRead;
static INPUT_RECORD InRec;
static char c;
hIn = GetStdHandle(STD_INPUT_HANDLE);
if(hIn == INVALID_HANDLE_VALUE)
{
printf("GetStdHandle poop");
}
else
{
do
{
ReadConsoleInput(hIn, &InRec, 5, &NumRead);
if (InRec.EventType == KEY_EVENT)
{
if (InRec.Event.KeyEvent.bKeyDown == 1)
{
c = InRec.Event.KeyEvent.uChar.AsciiChar;
/* testing print
printf(" KDown Repeat KeyCode ScanCode Unicode Ascii Ctrl-State \n");
printf(" %3x %3x %3x %3x %3x %3x %3lx \n",
InRec.Event.KeyEvent.bKeyDown,
InRec.Event.KeyEvent.wRepeatCount,
InRec.Event.KeyEvent.wVirtualKeyCode,
InRec.Event.KeyEvent.wVirtualScanCode,
InRec.Event.KeyEvent.uChar.UnicodeChar,
InRec.Event.KeyEvent.uChar.AsciiChar,
InRec.Event.KeyEvent.dwControlKeyState );
*/
/* printf("|if %c %02x ", c, c); testing*/
break;
}
}
}
while( c != '\r' );
}
return c;
}
Thanks to all the people who helped me out with it (from this forum)