UNIXLINUX编程实践教程>第五章>实例代码注解>echostate.c

一 问题

  读取驱动设置并显示回显位的状态。

二 分析

  标准输入的文件描述符为0
  使用tcgetattr()函数和termios结构体可以读取到设备的属性
  回显位状态放置在termios.c_lflag中

三 实现

#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
main()
{
        struct termios info;
        int rv;
    /*读取终端设置*/
        rv = tcgetattr(0,&info);
        if(rv == -1)
        {
                perror("togetattr");
                exit(1);
        }
    /*判断回显位是否被设置*/
        if(info.c_lflag&ECHO)
                printf("echo is on, since its bit is 1\n");
        else
                printf("echo is OFF,since its bit is 0\n");
}

四 相关函数与结构体

1 tcgetattr() 函数
读取tty驱动程序的属性
头文件:#include <termios.h>  #include <unistd.h>
函数原型: int tcgetattr(int fd,struct termios *info)
参数:  fd    与终端相关联的文件描述符
       info   指向终端结构的指针
返回值: -1    遇到错误
     0     成功返回

2 termios结构体
struct termios
{
  tcflag_t c_iflag;  /*input mode flags*/
  tcflag_t c_oflag;  /*output mode flags*/
  tcflag_t c_cflag;  /*control mode flags*/
  tcflag_t c_lflag;  /*local mode flags*/
  cc_t c_c[NCCS];  /*control characters*/
  speed_t c_ispeed;  /*input speed*/
  speed_t c_ospeed;  /*output speed*/
}

原文地址:https://www.cnblogs.com/cation/p/2944453.html