如何使用c检查用户在stty中设置擦除/退格的内容

时间:2014-02-20 03:25:42

标签: c linux termios stty

在Unix中,每个平台的某些键的默认设置是不同的。例如,Ubuntu中的擦除可能是erase = ^?。但是,对于AIX,它可能与示例erase = ^H完全不同。如何检查C中的stty设置?

这是我试图写的

#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<unistd.h>

int main()
{
  struct termios term;

  if(tcgetattr(STDIN_FILENO, &term) < 0)
  {
     printf("Error to get terminal attr\n");
  }

  printf("The value for Erase is %s\n",term.c_cc[ERASE]);

  return 0;
}

使用gcc编译后。它说ERASE未宣布。那么实际上我应该使用哪个正确的选项或变量呢?

1 个答案:

答案 0 :(得分:2)

printf("The value for Erase is %s\n",term.c_cc[ERASE]);应为printf("The value for Erase is %d\n",term.c_cc[VERASE]);,有关详细信息,请参阅termios(3)

擦除字符的符号索引是VERASE; c_cc[VERASE]的类型为cc_t,在我的系统中,cc_tunsigned char,因此应使用%c%d进行打印。< / p>