- 论坛徽章:
- 0
|
- #include<stdio.h>
- #define PAGELEN 24
- #define LINELEN 512
- int do_more(FILE *fp){ //获取按键
- int c;
- printf("\033[7m more? \033[m");
- while((c=getc(fp))!=EOF){
- if(c==' '){
- return PAGELEN;
- }
- if(c=='\n'){
-
- return 1;
- }
- if(c=='q'){
- return 0;
- }
-
- }
- return 0;
- }
- void see_more(FILE *fp){ //按q退出,空格显示下一页,回车显示下一行。
- int line_num=0;
- int reply;
- char linech[LINELEN];
- FILE *fp_tty;
- fp_tty=fopen("/dev/tty","r"); //打开/dev/tty文件(在这相当于键盘)
- if(fp_tty==NULL){
- exit (1);
- }
- while(fgets(linech,LINELEN,fp)){
- if(fputs(linech,stdout)==EOF){
- exit(1);
- }
- line_num++;
- if(line_num==PAGELEN){
- reply=do_more(fp_tty);
- if(reply==0){
- break;
- }
- line_num-=reply;
- }
-
- }
- }
- int main(int ac,char *av[]){
- FILE *fp;
- if(ac==1)
- see_more(stdin);
- else
- {
- while(--ac)
- if((fp=fopen(*++av,"r"))!=NULL){
- see_more(fp);
- fclose(fp);
- }else
- exit(1);
- }
- return 0;
- }
复制代码 |
|