#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define ESC_KEY 0x1b
char table[255];
static int getch();
int main(int argc, char **argv)
{
int ch;
unsigned int length=0;
memset(table,'\0',255);
printf("\r\n->");
while( 1 )
{
//printf( "\r\nch_c=%c ,ch_x=%02x",ch,ch);
ch = getch();
if(ch == 0x08)
{
if(length > 0)
{
length--;
printf("%c",ch);
printf("%c",table[length] = 0x20);
printf("%c",ch);
printf("%c",table[length] = '\0');
}
}
else
{
printf("%c",table[length] = ch);
length++;
}
}
return 0;
}
static int getch(void)
{
struct termios oldt,newt;
int ch;
if (!isatty(STDIN_FILENO)) {
fprintf(stderr, "this problem should be run at a terminal\n");
exit(1);
}
// save terminal setting
if(tcgetattr(STDIN_FILENO, &oldt) < 0) {
perror("save the terminal setting");
exit(1);
}
// set terminal as need
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
if(tcsetattr(STDIN_FILENO,TCSANOW, &newt) < 0) {
perror("set terminal");
exit(1);
}
ch = getchar();
// restore termial setting
if(tcsetattr(STDIN_FILENO,TCSANOW,&oldt) < 0) {
perror("restore the termial setting");
exit(1);
}
return ch;
}