-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore02.c
70 lines (67 loc) · 1.35 KB
/
more02.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
/*
+----> 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(FILE*);
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;
FILE* fp_tty;
fp_tty = fopen("/dev/tty","r");
if(fp_tty == NULL)
exit(1);
while(fgets(line,LINELEN,fp))
{
if(num_of_lines == PAGELEN) // full screen?
{
reply = see_more(fp_tty); // 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(FILE* fp)
{
int c;
printf("\033[7m more?\033[m");
while((c=getc(fp))!=EOF)
{
if(c == 'q')
return 0;
if(c == ' ')
return PAGELEN;
if(c =='\n')
return 1;
}
return 0;
}