分享

设置终端控制属性(ubuntu的命令终端显示属性)

 书永夜 2013-12-02
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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


int main(void)
{
#if 0
    int n = 10;
    while(1)
    {
        if((n--) == 0)
        {
            printf("over n=%d\n", n);
            break;
        }
        printf("n=%d\n", n);
    }
#endif

    unsigned char buf[1024];
    bzero(buf, 1024);
    read(0, buf, 1024);
    printf("buf:%s\n", buf);    

#if 1
    //终端属性描述结构体
    struct termios oldattr, newattr;

    //获取当前终端的属性
    tcgetattr(0, &oldattr);
    //将newattr设置为原始属性
    cfmakeraw(&newattr);

    //将终端设置为原始属性
    tcsetattr(0, TCSANOW, &newattr);

    while(1)
    {
        bzero(buf, 1024);
        read(0, buf, 1024);
        printf("%u\t%u\t%u\t%u\n", buf[0], buf[1], buf[2],buf[3]);
    }
    
    //将终端恢复原来的属性
    tcsetattr(0, TCSANOW, &oldattr);
    sleep(1);
    bzero(buf, 3);
    read(0, buf, 3);
    printf("%c\t%c\t%c\n", buf[0], buf[1], buf[2]);
#endif

#if 0
    //终端属性描述结构体
    struct termios oldattr, newattr;

    //获取当前终端的属性
    tcgetattr(0, &oldattr);
    //将newattr设置为原始属性
    cfmakeraw(&newattr);

    //将终端设置为原始属性
    tcsetattr(0, TCSANOW, &newattr);
    bzero(buf, 1024);
    read(0, buf, 1024);
    printf("%c\t%c\t%c\t%c\n", buf[0], buf[1], buf[2], buf[3]);
    
    //将终端恢复原来的属性
    tcsetattr(0, TCSANOW, &oldattr);
    sleep(1);
    bzero(buf, 1024);
    read(0, buf, 1024);
    printf("%c\t%c\t%c\t%c\n", buf[0], buf[1], buf[2], buf[3]);
#endif



    return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多