我有一个代码来获取stty设置到用户终端的内容。然后更改term.c_cc[VERASE]
。只要用户使用bash,这就可以正常工作。我的问题是当用户使用默认shell登录或使用/bin/sh
(只有#的shell)时。虽然我设置了term.c_c[VERASE]
,但它仍然不适用。为什么会这样? tcsetattr
不适用于/ bin / sh?
这是我的代码的一部分:
int tty_raw(int fd)
{
struct termios attr[1];
if (tcgetattr(fd, attr) == -1)
return -1;
attr->c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
attr->c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
attr->c_cflag &= ~(CSIZE | PARENB);
attr->c_cflag |= (CS8);
attr->c_oflag &= ~(OPOST);
attr->c_cc[VMIN] = 1;
attr->c_cc[VTIME] = 0;
return tcsetattr(fd, TCSANOW, attr);
}
void get_and_set_tty()
{
struct termios term,oldterm;
unsigned char buf[BUFSIZ];
static char buf2[BUFSIZ];
static char buf3[BUFSIZ];
static char end_command[BUFSIZ];
char exit[5] = "exit";
char enter = '\x0d';
char tab = '\x09';
char end = ';';
char delete = '\x7f';
char default_delete;
int nread;
int ret;
int status;
int restricted = 0;
int size = 0;
int i,n;
if(tcgetattr(STDIN_FILENO, &term) < 0)
{
log_debug("Error to get terminal attr");
}
default_delete = oldterm.c_cc[VERASE];
i=0;
tty_raw(STDIN_FILENO);
term.c_cc[VERASE] = delete;
tcsetattr(STDIN_FILENO,TCSANOW,&term);
while(size != 0)
{
end_command[i]=delete;
size--;
i++;
}
end_command[i]='\x0d';
i++;
end_command[i]='\0';
size = strlen(end_command);
bufferevent_enable(master_buffer, EV_WRITE);
log_debug("sending [%s]",end_command);
if (bufferevent_write(master_buffer, end_command, size)!=0){
log_error("bufferevent_write failed");
}
tty_raw(STDIN_FILENO);
oldterm.c_cc[VERASE] = default_delete;
tcsetattr(STDIN_FILENO,TCSANOW,&oldterm);
}