自己实现more命令
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define PAGELEN 24 5 #define LINELEN 512 6 7 int see_more(FILE*); 8 void do_more(FILE*); 9 10 int main(int ac, char* av[]) 11 { 12 FILE *fp; 13 if(ac==1) 14 do_more(stdin); 15 // printf("%s","Please input file name.\n"); 16 else 17 { 18 while(--ac) 19 if((fp=fopen(*++av,"r"))!=NULL) 20 { 21 do_more(fp); 22 fclose(fp); 23 } 24 } 25 26 27 } 28 29 void do_more(FILE* fp) 30 { 31 int num_of_lines=0; 32 char line[LINELEN]; 33 int reply; 34 // int see_more(); 35 FILE* fp_tty; 36 fp_tty=fopen("/dev/tty","r"); 37 if(fp_tty==NULL) exit(1); 38 while(fgets(line,LINELEN,fp)) 39 { 40 if(num_of_lines==PAGELEN) 41 { 42 reply=see_more(fp_tty); 43 if(reply==0) 44 break; 45 num_of_lines-=reply; 46 } 47 if(fputs(line,stdout)==EOF) 48 exit(1); 49 num_of_lines++; 50 } 51 52 } 53 54 int see_more(FILE* cmd) 55 { 56 int c; 57 //printf("\033[7m more? \033[m"); 58 printf("\033[32;49;1m [more?] \033[39;49;0m"); 59 //033[32;49;1m [MORE?] \033[39;49;0m 60 while((c=getc(cmd))!=EOF) 61 { 62 if(c=='q') 63 return 0; 64 65 if(c=='\n') 66 return 1; 67 68 if(c==' ') 69 return PAGELEN; 70 } 71 }