把代码先贴上来先. 参考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;
}