#include #include #include #define CR 0x0d //carriage return #define ESC 0x1b //Escape key #define TAB 0x09 #define LF 0x0a //Line feed #define BACKSPACE 0x08 #define NULL 0 //Empty character #define TRUE 1 #define FALSE 0 #define LENGTH 15 //size of the string void input(char *string, int length); void main() { char string[LENGTH]; char password[] = "please"; int result; printf("Enter your secret password:"); input(string,LENGTH); result = strcmp(string,password); if(result==0) printf("Entry granted!\n"); else { printf("Sorry. Wrong password.\n"); printf("Security has been notified.\n"); printf("We will find you...\n"); } } void input(char *string, int length) { int done = FALSE; int index = 0; char ch; string[0] = NULL; // init the buffer do { ch = getch(); /* Check to see if the buffer is full */ if (index == length) { switch(ch) { case CR: break; default: ch = NULL; break; } } /* process the keyboard input */ switch(ch) { case CR: //Enter key putchar(ch); putchar(LF); string[index] = NULL; done = TRUE; break; case BACKSPACE: if(index==0) { break; } else { putchar(ch); putchar(' '); putchar(ch); index--; break; } case NULL: break; default: //display & sto /* Do not display input */ putchar('*'); string[index] = ch; index++; break; } } while(!done); }