-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymore.c
71 lines (61 loc) · 1.1 KB
/
mymore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *f,long fsize);
int see_more(FILE *cmd,int per);
int main(int argc,char ** argv)
{
FILE * fd;
struct stat buf;
fd=fopen(argv[1],"r");
if(argc==1)
do_more(stdin,0);
else if((fd=fopen(argv[1],"r"))!=NULL)
{
stat(argv[1],&buf);
//printf("fsize=%ld\n",(long)buf.st_size);
do_more(fd,(long)buf.st_size);
fclose(fd);
}
return 1;
}
void do_more(FILE *f,long fsize)
{
char buffline[LINELEN];
struct stat buf;
int curline=0,replay;
FILE *tty;
int per;
while(fgets(buffline,LINELEN,f))
{
tty=fopen("/dev/tty","r");
if(curline==PAGELEN){
//printf("%ld\n",ftell(f));
per=(int)((float)ftell(f)/(float)fsize*100);
replay=see_more(tty,per);
if(replay==0)
break;
curline-=replay;
}
if(fputs(buffline,stdout)==EOF)
exit(1);
curline++;
}
}
int see_more(FILE *cmd,int per)
{
char c;
printf("\033[7m --more--?%d%\033[m",per);
if( (c=getc(cmd))!=EOF)
{
if(c=='q')
return 0;
if(c=='y')
return PAGELEN;
if(c=='\n')
return 1;
}
return 0;
}