Linux---More命令 初级实现

Linux: more
已实现:more filename , quit不需要回车
未实现:command | more 重定向 ,显示百分比
Waiting。。。

 /*
Linux: more
已实现:more filename , quit不需要回车
未实现:command | more 重定向 ,显示百分比
Waiting。。。
*/
#include<stdio.h>
#define PAGELEN 24
#define LINELEN 512
void do_more( FILE * );
int see_more();
int main( int ac,char *av[] ){
FILE * fp;
if( ac== )
do_more( stdin );
else{
while( --ac )
if( (fp=fopen(* ++av,"r"))!=NULL ){
do_more( fp );
fclose( fp );
}
else{
exit( );
}
}
return ;
} void do_more( FILE *fp ){
char line[ LINELEN ];
int num_of_lines = ;
int see_more(),reply;
while( fgets( line,LINELEN,fp ) ){
if( num_of_lines == PAGELEN ){
reply = see_more();
if( reply == ){
break;
}
num_of_lines -= reply ;
}
if( fputs( line,stdout )==EOF ){
exit( );
}/* 标准输入输出 */
num_of_lines ++;
}
} int see_more(){
int c;
system ("stty -F /dev/tty cbreak");/*打开/dev/tty作为输入终端,并且控制属性为不需要回车*/
printf("\033[7m more?\033[m");
while( (c=getchar())!=EOF ){
if( c=='q' ){
return ;
}
if( c==' ' ){
return PAGELEN;
}
if( c=='\n' ){
return ;
}
}
system ("stty -F /dev/tty -cbreak");/*恢复终端属性为需要回车*/
return ;
}
上一篇:APP设计师拿到APP产品原型开始,七步搞定APP设计(转)


下一篇:ETH Dapp 体验报告