forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.4.c
47 lines (38 loc) · 896 Bytes
/
15.4.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define PAGER "${PAGER:-more}"
#define MAXLINE 1024
int main(int argc, char *argv[])
{
char line[MAXLINE];
FILE *fpin, *fpout;
if (2 != argc) {
printf("usage: a.out <pathname>");
exit(-1);
}
if ((fpin = fopen(argv[1], "r")) == NULL) {
printf("can't open %s\n", argv[1]);
exit(-1);
}
if ((fpout = fopen(PAGER, "w")) == NULL) {
printf("popen error\n");
exit(-1);
}
while (fgets(line, MAXLINE, fpin) != NULL) {
if (fputs(line, fpout) == EOF) {
printf("fputs error to pipe\n");
exit(-1);
}
}
if (ferror(fpin)) {
printf("fgets error\n");
exit(-1);
}
if (pclose(fpout) == -1) {
printf("pclose error");
exit(-1);
}
return 0;
}