#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

void setEcho(bool echo);
void setBlocking(bool block);
bool hasInput();
void clearStdin();

using namespace std;

int main(void) {
 int i;
 char c;
 //turn off blocking
 fcntl(STDIN_FILENO, O_NONBLOCK, O_ASYNC);
 setBlocking(false);
 setEcho(false);


 cout << "set  your password: ";
 cout.flush();
 do {
   if(hasInput()) {
     c = cin.get();
     c++;
     cout.put(c+1);
     cout.flush();
   }
   usleep(1000);
 } while(c!='\n');


 setEcho(true);
 clearStdin();
 return 0;
}

void
setEcho(bool echo) {
 struct termios t;

 //get the current settings
 tcgetattr(STDIN_FILENO, &t);

 //toggle echo
 if(echo)
   t.c_lflag = t.c_lflag | ECHO;
 else
   t.c_lflag = t.c_lflag & ~ECHO;

 //set the settings
 tcsetattr(STDIN_FILENO, TCSANOW, &t);
}


void
setBlocking(bool block) {
 struct termios t;

 //get the current settings
 tcgetattr(STDIN_FILENO, &t);

 //toggle blocking
 if(block)
   t.c_lflag = t.c_lflag | ICANON;
 else
   t.c_lflag = t.c_lflag & ~ICANON;

 //set the settings
 tcsetattr(STDIN_FILENO, TCSANOW, &t);
}


bool
hasInput() {
 fd_set fs;
 timeval tv;
 int i;

 //set up the fd set
 FD_SET(STDIN_FILENO, &fs);

 //set up the timespec
 tv.tv_sec = 0;
 tv.tv_usec = 1;

 //select
 i=select(STDIN_FILENO + 1, &fs, NULL, NULL, &tv);

 //0 no input  available
 //-1  error
 if(i==0 || i==-1) {
   return false;
 }

 //there is input
 return true;

}


void
clearStdin()  {
 while(hasInput())
   cin.get();
}