-
Notifications
You must be signed in to change notification settings - Fork 0
/
more01.c
66 lines (63 loc) · 1.24 KB
/
more01.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
/*
+----> show 24 lines from input
| +--> print [more?] message
| | input Enter, SPACE, or q
| +--> if Enter, advance next page
+ ---- if Space, advance one line
if q --> exit
*/
#include<stdio.h>
#include<stdlib.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *);
int see_more();
int main(int argc, char *argv[])
{
FILE *fp;
if(argc == 1)
do_more(stdin);
else
while(--argc)
{
if((fp = fopen(*(++argv),"r")) != NULL)
{
do_more(fp);
fclose(fp);
}
}
}
void do_more(FILE * fp)
{
char line[LINELEN];
int num_of_lines = 0;
int reply;
while(fgets(line,LINELEN,fp))
{
if(num_of_lines == PAGELEN) // full screen?
{
reply = see_more(); // 1, 24, or exit
if(reply == 0)
break;
num_of_lines -= reply;
}
if(fputs(line, stdout) == EOF)
exit(1);
num_of_lines++;
}
}
int see_more()
{
int c;
printf("\033[7m more?\033[m");
while((c=getchar())!=EOF)
{
if(c == 'q')
return 0;
if(c == ' ')
return PAGELEN;
if(c =='\n')
return 1;
}
return 0;
}