您的位置:首页 > 运维架构 > Linux

如何真正清空Linux终端的已显示Buffer

2012-10-25 16:53 323 查看
把代码先贴上来先. 参考ncurses-5.9/progs/tset.c简化, 能实现和"reset"命令一样的功能. 

ncurses包位置: http://www.gnu.org/software/ncurses/
#include <stdio.h>
#include <stdlib.h> //  getenv
#include <unistd.h> //  STDOUT_FILENO
#include <term.h>   //  TTY definition

#define KTRUE  1
#define KFALSE 0
#define reset_1string                  CUR Strings[122]
#define reset_2string                  CUR Strings[123]

static inline int outc(int c) { return putc(c, stderr); }

int main(void)
{
struct termios mode, oldmode;
char *ttype, *p;

if (tcgetattr(2, &mode) < 0) {
printf("standard error.\n");
goto EXIT;
}
oldmode = mode;

tcsetattr(2, 1, &mode);

if ((ttype = getenv("TERM")) == NULL)
goto EXIT;

if (ttype[0] == '?')
goto EXIT;

if (setupterm(ttype, STDOUT_FILENO, (int*)0) != 0)
goto EXIT;

if ((p = reset_1string) != 0) {
tputs(p, 0, outc);
}
if ((p = reset_2string) != 0) {
tputs(p, 0, outc);
}

if (memcmp(&mode, &oldmode, sizeof(mode))) {
tcsetattr(2, 1, &mode);
}

return KTRUE;

EXIT:
return KFALSE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: