Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char Wahl = 0;
struct termios old = { 0 };
fflush(stdout);
if (tcgetattr(0, &old) < 0){
perror("tcsetattr()");
}
old.c_lflag &= ~ICANON; /* deactivate */
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1; /* send each byte */
old.c_cc[VTIME] = 0;/* don't wait */
if (tcsetattr(0, TCSANOW, &old) < 0){
perror("tcsetattr ICANON");
}
if (read(0, &Wahl, 1) < 0){
perror("read()");
}
old.c_lflag |= ICANON; /* activate */
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0){
perror("tcsetattr ~ICANON");
}
//printf("%c\n", Wahl);
return Wahl;
}
int main(void) {
char Passwort[10];
int i=0,j;
do{
Passwort[i]=getch();
printf("%c",Passwort[i]);
if(Passwort[i]!=8) i++; // normal input
else{ // Special case: Backspace
printf(" ");
if(i>0) i--;
}
}while(Passwort[i-1]!=13);
for(j=0;j<i-1;j++)printf("*"); // Pressing of Enter will overwrite the output
return 0;
}
getch() is working now but the Problem is now. By pressing Backspace it wont delete a letter but creating a weird output on screen. Same goes with the function of Enter...