title: linux terminal output
body: <p>Hi I wrote a simple c prog to just accept a password while diplaying <strong>*</strong> to hide the input. But the * for the last character entered is not appearing at the right place.
   the code is below</p>
  
  <pre><code>int main(){  
  int choice = 0;  
  char pass[8];  
  FILE *input;  
  FILE *output;  
  struct termios initial_settings, new_settings;  
  
  if(!isatty(fileno(stdout))){  
      fprintf(stderr,"Not a terminal \n");  
  }  
  input = fopen("/dev/tty","r");  
  output = fopen("/dev/tty","w");  
  if(!input || !output){  
  fprintf(stderr,"error opening");  
  exit(1);  
  }  
  tcgetattr(fileno(input),&amp;initial_settings);  
  new_settings = initial_settings;  
  new_settings.c_lflag &amp;= ~ICANON;  
  new_settings.c_lflag &amp;= ~ECHO;  
  new_settings.c_cc[VMIN] = 1;  
  new_settings.c_cc[VTIME] = 0;  
  new_settings.c_lflag &amp;= ~ISIG;  
  if(tcsetattr(fileno(input), TCSANOW, &amp;new_settings) != 0) {  
  fprintf(stderr,"could not set attributes\n");  
  }
  
  int count = 0;  
  char ch;  
  printf("Please enter the password: ");  
  while (count&lt;8){  
  ch = fgetc(input);  
  
  if(ch == '\n' || ch == '\r'){  
  break;  
  }else{  
  fputc('*',stdout);  
  pass[count] = ch;  
  count++;  
  }  
  tcdrain(fileno(stdout));  
  }  
  
  
  fprintf(output,"you have entered :%s \n",pass);  
  tcsetattr(fileno(input),TCSANOW,&amp;initial_settings);  
  exit(0);  
  }  
  </code></pre>
  
  <p>The output is as follows:<br>
  Please enter the password:* * * * * * *<br>
  you have entered :12345678<br>
  * pasman@pasman-laptop:~$</p>
  
  <p>Its an 8 character password &amp; Notice that 7 *s appear as expected but the last * is appearing at the end of main.</p>