#include<stdio.h>
#define LINELEN 512
#define PAGELEN 24
void do_more(FILE*);
int see_more();
int main(int ac,char* av[])
{
FILE* fp;
if(ac==1){
do_more(stdin);
}
if((fp=fopen(*++av,"r"))!=NULL){
do_more(fp);
fclose(fp);
}
return 0;
}
void do_more(FILE* fp)
{
char line[LINELEN];
int reply=0;
int num_of_lines=0;
while(fgets(line,LINELEN,fp)!=NULL){
fputs(line,stdout);
if(num_of_lines==PAGELEN){
reply=see_more();
if(reply==0){
break;
}else{
num_of_lines-=reply;
}
}
num_of_lines++;
}
}
int see_more()
{
int c;
printf("more ?");
FILE* fin;
if((fin=fopen("/dev/tty","r"))!=NULL){
while((c=fgetc(fin))!=EOF){
if(c=='q'){
return 0;
}
if(c=='\n'){
return 1;
}
if(c==' '){
return PAGELEN;
}
}
}
fclose(fin);
return 0;
}