Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101919521
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-16 17:12:53

作者:CNL  出处:Unix爱好者家园unix-cd.com   
#include  
#include  
#include  
main(int argc,char *argv[]) 

long scr_no; 
int fd; 
char desname[20]; 
char tmp[40]; 
int retval; 
FILE *fp; 
if (argc==1 && isatty(1)) { 
strcpy(desname,""); 
strcpy(desname,ttyname(1)); 

else { 
if (argc!=2 || ((scr_no=atoi(argv[1])) < 1 || scr_no>12)) { 
printf("Usage: ./switch_scr screen_no(1-12)\n"); 
exit(1); 

sprintf(desname,"/dev/tty%02d",scr_no); 

scr_no=atoi(&desname[strlen(desname)-2]); 
printf("switch to %s ... No:%d \n",desname,scr_no); 
fd=open(desname,O_RDONLY|O_NDELAY); 
if (fd<0) { 
sprintf(tmp,"open tty%02d",scr_no); 
perror(tmp); 
exit(1); 

scr_no--; 
retval=ioctl(fd,VT_ACTIVATE,scr_no); 
if (retval<0) { 
sprintf(tmp,"ioctl tty%02d active",1+scr_no); 
perror(tmp); 
exit(1); 

fp=freopen(desname,"r",stdin); 
if (fp==NULL) { 
sprintf(tmp,"reopen tty%02d as stdin",1+scr_no); 
perror(tmp); 
exit(1); 

fp=freopen(desname,"w",stdout); 
if (fp==NULL) { 
sprintf(tmp,"reopen tty%02d as stdout",1+scr_no); 
perror(tmp); 
exit(1); 

fp=freopen(desname,"w",stderr); 
if (fp==NULL) { 
sprintf(tmp,"reopen tty%02d as stderr",1+scr_no); 
perror(tmp); 
exit(1); 

printf(""); 
retval=getchar(); 
printf("press %d\n",retval); 

阅读(218) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~